jquery / javascript:未捕获的TypeError?(jquery / javascript: Uncaught TypeError?)

编程入门 行业动态 更新时间:2024-10-22 16:23:08
jquery / javascript:未捕获的TypeError?(jquery / javascript: Uncaught TypeError?)

嘿伙计们,我写了一个小的自定义动画滚动功能......

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();

?

更多推荐

本文发布于:2023-07-25 06:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1257253.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:javascript   jquery   Uncaught   TypeError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!