javascript jQuery .offset() 未定义 var 对象的类`top`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .offset() getting undefined for class `top` of var object
提问by Brian
I'm working on the following code, and a bit stumped why I'm getting an error in my debugger. apparently in the line that starts with var skillsLimit = ...
the class .top
of var skillsOffset
is undefined. Am I setting the var for skillsOffset
incorrectly here?
我正在处理以下代码,并且有点困惑为什么我的调试器出现错误。显然,在以 varvar skillsLimit = ...
类开头的行中是未定义的。我在这里错误地设置了 var吗?.top
skillsOffset
skillsOffset
// Set Pie graph to position fixed during a specified range
var $window = $(window);
var windowHeight = $(window).height();
var pos = $window.scrollTop(); //position of the scrollbar
var $this = $(this);
$window.bind('scroll', function(){ //when the user is scrolling...
var pos = $window.scrollTop(); //position of the scrollbar
var skillsOffset = $('#skills').offset();
var skillsLimit = $('#skills').skillsOffset.top + $('#skills').outerHeight();
if ( pos > skillsOffset ) {
$('.chartwell-pies').css({ 'position' : 'fixed' });
}
});
回答by Rob Cooper
Remove the $('#skills')
from the assignment to skillsLimit
, you've already got the object in the line above:
$('#skills')
从分配到 中删除skillsLimit
,您已经在上面的行中获得了对象:
var pos = $window.scrollTop(); //position of the scrollbar
var skillsOffset = $('#skills').offset();
var skillsLimit = skillsOffset.top + $('#skills').outerHeight();