访问来自另一个函数的函数结果
Accessing function results from another function?
我有这些函数
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = new Image;
img.src = reader.result;
var imgwidth = img.width;
var imgheight = img.height;
console.log(imgwidth);
console.log(imgheight);
$('#imgcrop').attr('src', e.target.result);
$('#preview').attr('src', e.target.result);
$('.crop').Jcrop({
onSelect: updateCoords,
boxWidth: 300,
boxHeight: 300,
onChange: showPreview,
aspectRatio: 1,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ]
});
};
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function() {
console.log(this);
readURL(this);
});
function updateCoords(c) {
console.log(c);
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
}
function showPreview(coords) {
var rx = 100 / coords.w;
var ry = 100 / coords.h;
$('#preview').css({
width: Math.round(rx * imgwidth) + 'px',
height: Math.round(ry * imgheight) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
}
我需要从文件读取器函数访问imgwidth和imgheight,以便在showpreview函数中使用,但我在这些函数的范围方面遇到了问题。我如何访问这些值,或者至少将它们作为参数传递给其他函数?
谢谢!
将它们作为参数传递给showPreview
:
function showPreview(coords, imgheight, imgwidth) {
var rx = 100 / coords.w;
var ry = 100 / coords.h;
$('#preview').css({
width: Math.round(rx * imgwidth) + 'px',
height: Math.round(ry * imgheight) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
}
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = new Image;
img.src = reader.result;
var imgwidth = img.width;
var imgheight = img.height;
console.log(imgwidth);
console.log(imgheight);
$('#imgcrop').attr('src', e.target.result);
$('#preview').attr('src', e.target.result);
$('.crop').Jcrop({
onSelect: updateCoords,
boxWidth: 300,
boxHeight: 300,
onChange: function(coords) {
showPreview(coords, imgheight, imgwidth);
},
aspectRatio: 1,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ]
});
};
reader.readAsDataURL(input.files[0]);
}
}
相关文章:
- JScript 错误: 无法分配给函数结果
- 显示基于javascript函数结果的HTML代码
- 访问来自另一个函数的函数结果
- 将函数结果传递给Javascript中的另一个函数变量
- NaN作为函数结果而不是必需的整数出现问题
- 更改javascript更改函数结果的颜色
- 从函数结果中检测三角函数
- 根据函数结果使用 JavaScript 更改图像
- 如何返回函数结果并将它们设置为标记的内部 HTML
- JavaScript 如何将函数结果与数组中的值一起传递到同一个函数中
- 用
标记包装 JQuery 函数结果
- 节点函数结果未定义
- 使用Cheerio和Response for Node web scraper,将响应函数结果传递给视图
- 在所有项目javascript中显示函数结果
- 平板电脑友好的悬停工具提示,显示基于javascript函数结果的动态文本
- 在机器时间更改后立即更新Date()函数结果
- 屈服函数结果
- 如何在JavaScript中处理异步函数结果
- 声明和定义全局变量作为函数结果有时有效
- Jquery&函数结果,作为属性名称