Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $(li).width()返回包含填充的值?_Javascript_Jquery_Css_Width - Fatal编程技术网

Javascript $(li).width()返回包含填充的值?

Javascript $(li).width()返回包含填充的值?,javascript,jquery,css,width,Javascript,Jquery,Css,Width,我有下面的jsFiddle。其思想是,当显示过长的上下文菜单项时,它会以省略号呈现,但在鼠标上方它会平移文本 有关守则: //Provides helper methods for non-specific functionality. define('helpers', [], function () { 'use strict'; return { scrollElementInsideParent: function(element, parent) {

我有下面的jsFiddle。其思想是,当显示过长的上下文菜单项时,它会以省略号呈现,但在鼠标上方它会平移文本

有关守则:

//Provides helper methods for non-specific functionality.
define('helpers', [], function () {
    'use strict';

    return {
        scrollElementInsideParent: function(element, parent) {
            //  Scroll the element if its too long to read.
            $(element).mouseover(function () {

                var distanceToMove = $(this).width() - $(parent).width();

                console.log("My width and parent width:", $(this).width(), $(parent).width());

                $(this).animate({
                    marginLeft: "-" + distanceToMove + "px"
                }, {
                    //  Just a feel good value; scales as the text gets longer
                    duration: 15 * distanceToMove,
                    easing: 'linear'
                });

            }).mouseout(function () {
                $(this).stop(true).animate({ marginLeft: 0 });
            });
        }
    };
在这里,我记录正在滚动的元素的宽度及其父元素的宽度。控制台输出:

我的宽度和父宽度:360 230

但从指标来看,这似乎是不正确的:


这是为什么?

在您的代码中,
父项
参数指的是
。使用
$(this.parent()
获取

  • 演示:

    你有
    框大小:边框框
    吗?尝试使用
    outerWidth()
    @bfavaretto你可以看到jsfiddle。没有框大小:框上有边框框。Terry outerWidth()返回与.width()和.innerWidth()以及.outerWidth(true)相同的值。你在开玩笑吧
    指的是
    元素,其宽度为
    auto
    ,最终宽度为
    230px
    。你为什么看《代码》呢?@SeanAnderson也许我又误会了。让我再看看,汉克斯。就在我鼻子底下。我很感激。:)@SeanAnderson一点问题都没有,下次再考虑调试这种东西:)我很高兴你得到了一个解决它的答案!哦,是的。。我昨晚很晚才开始做这件事,我想我已经探索了所有的选择。显然,今天早上我发现这一点的想法是错误的。干杯。@SeanAnderson嘿,这就是这个地方的目的:)