嘿伙计们,我写了一个小的自定义动画滚动功能......
function scroll(selector, animate, viewOffset) { pageOffset = selector.offset(); scrollPos = pageOffset.top - viewOffset; if (animate) { $('html, body').animate({scrollTop : scrollPos + 'px'}, { duration: 'slow', // how fast we are animating easing: 'easeOutQuint', // the type of easing complete: function() { } }); } else { $('html, body').scrollTop( scrollPos ); } }我用scroll($('#something'), false, 30);调用它scroll($('#something'), false, 30);
它实际上是有效的,但有时它有点儿马车,功能不正常工作。 我总是在click事件上调用scroll()函数。
我遇到的最大问题是在页面加载时我的控制台告诉我以下行。
未捕获的TypeError:无法读取null的属性'top'(script.js第191行,这是我上面示例中的第3行)
知道什么可能导致这个错误吗? 在dom-ready或on-load上甚至不调用scroll()函数。 它只是在特定的点击事件上调用。
感谢您的帮助
hey guys, i wrote a little custom animated scroll function...
function scroll(selector, animate, viewOffset) { pageOffset = selector.offset(); scrollPos = pageOffset.top - viewOffset; if (animate) { $('html, body').animate({scrollTop : scrollPos + 'px'}, { duration: 'slow', // how fast we are animating easing: 'easeOutQuint', // the type of easing complete: function() { } }); } else { $('html, body').scrollTop( scrollPos ); } }I call it with scroll($('#something'), false, 30);
It actually works, however sometimes it's a little bit buggy and the function doesn't properly work. I always call the scroll() function on a click-event.
The biggest problem I have is that on page load my console tells me the following line.
Uncaught TypeError: Cannot read property 'top' of null (script.js line 191 which is line 3 in my example above)
Any idea what could cause this error? the scroll() function is not even called on dom-ready or on-load. It's just called on specific click-events.
thank you for your help
最满意答案
不应该是:
pageOffset = $(selector).offset();?
Shouldn't it be:
pageOffset = $(selector).offset();?
更多推荐
发布评论