jquery - 【javascript】 selection.width || 1 这是什么意思? 谢谢
黄舟
黄舟 2017-04-10 17:36:52
0
2
383

imgAreaSelect插件中的var scaleX = 100 / (selection.width || 1);
其中,selection.width || 1 这是什么意思?

marginLeft与margin-left一样吗?

这里 marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',为什么要求反?

谢谢

function preview(img, selection) {
    var scaleX = 100 / (selection.width || 1);
    var scaleY = 100 / (selection.height || 1);
  
    $('#ferret + p > img').css({
        width: Math.round(scaleX * 400) + 'px',
        height: Math.round(scaleY * 300) + 'px',
        marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
        marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
    });
}

$(document).ready(function () {
    $('<p><img src="ferret.jpg" style="position: relative;" /><p>')
        .css({
            float: 'left',
            position: 'relative',
            overflow: 'hidden',
            width: '100px',
            height: '100px'
        })
        .insertAfter($('#ferret'));

    $('#ferret').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview });
});
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(2)
洪涛

selection.width || 1 这是什么意思?
var scaleX = 100 / (selection.width || 1); 的意思就是先检查 selection.width 有没有值,有的话就用 100 / 该值再付给 scaleX,没的话就用 100 / 1 来赋值;

marginLeft与margin-left一样吗?
两者用的地方不同,在 jQuery 的 css 方法里用前者,后者用在 css 代码中

这里 marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',为什么要求反?
正是左边距增大,反是左边距减小

希望有所帮助~ :)

黄舟

先对||前面的进行布尔运算,如果结果是true(即width存在且不是0),就使用width,否则使用||后的变量(1)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!