未捕获的类型错误:无法读取未定义错误的属性“top”

Uncaught TypeError: Cannot read property 'top' of undefined error

本文关键字:错误 属性 未定义 读取 top 类型      更新时间:2023-09-26

我想更改我的类名称,我的页面从功能动画div顶部滚动400,但我收到此错误。

请参阅此链接以获取我的代码

https://jsbin.com/zafove/edit?html,js,output

捕获的类型错误:无法读取未定义错误的属性"top"

 if(wScroll > $('.feature-animate').offset().top - 400 ){
            $('.feature-animate').each(function(j){
                setTimeout(function(){
                $('.feature-animate').eq(j).addClass('isShowing');
                },100 * (i+1));

错误消息表明 jquery 找不到类 .feature-animate 的 html 节点。第一个也是最可能的原因:您的 html 代码中有拼写错误。确保它看起来像这样:

<div class="feature-animate">...your other html code </div>

那么它应该可以工作。

顺便说一句:如果您有多个具有相同类名的div 容器,$('.feature-animate').offset().top 将获得 DOM 中具有该类的第一个元素的top值。

确保在运行 javascript 之前已经初始化了具有 feature-animate 类的元素。执行此操作的一个好方法是将其放在$(document).ready()标签中。

jQuery无法找到"feature-animate"元素,因为在html中,它是按照下面编写的。

    <div classs="row feature-animate"> 

(请注意"类"中的3个"s")

它应该如下:

    <div class="row feature-animate"> 

(删除多余的"s"并使其成为"类")