繁体   English   中英

拖动时jQuery UI舍入元素的宽度

[英]jQuery UI rounding width of element when dragging

当我检查页面上的元素时,我看到它的宽度是83.2像素

我使用jQuery UI使其“可拖动”:

$el.draggable({
    start: function (event, ui) {
        //$(ui.helper).css('margin', 0); //prevent jumping
        console.log('width:', $(ui.helper).css('width'));
        //console.log('width:', $(ui.helper).width());
        //console.log('width:', $(event.target).css('width'));
    }
});

拖动元素后输出到控制台的width: 83px 这导致稍后元素文本中的换行符。

jQuery UI是否围绕宽度和高度? 我如何获得更准确的价值?

我在这个问题中找到了答案: https//stackoverflow.com/a/16072668/426266

当拖动元素时,jQueryUI似乎将元素的宽度设置为整数值。 此外,$(element).width()将始终返回舍入的整数值。

我解决了这个问题:

$el.draggable({
    start: function (event, ui) {

        var width = event.target.getBoundingClientRect().width;

        $(ui.helper).css({
            'width': Math.ceil(width)
        });
    }
});

现在,当拖动元素时,元素中不再存在换行符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM