http://jsfiddle.net/MeoMix/8zg2V/83/
我有以下 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 });
});
}
};
在这里,我记录了被滚动的元素的宽度及其父元素的宽度。控制台输出:
My width and parent width: 360 230
但是从指标来看这似乎是不正确的:
这是为什么?
最佳答案
在您的代码中,parent
参数指的是<ul>
.使用 $(this).parent()
得到<li>
关于javascript - $(li).width() 返回值包括填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17728411/