javascript - 未捕获的类型错误 : Cannot read property 'top' of undefined . offset().top-500;

标签 javascript jquery html css

我是测试新手,滚动时我的页面出现控制台错误,它说的是

main.js:18

Uncaught TypeError: Cannot read property 'top' of undefined

at main.js:18
at dispatch (jquery-1.9.js:3)
at v.handle (jquery-1.9.js:3)

我尝试了其他解决方案,例如将选择器更改为变量但错误没有解决,我想我想搜索整个页面以进行更正但我没有确定这是我的页面 main.js

/**
Visionmax v 1.0
Available on ThemeForest - http://themeforest.net/user/nasirwd/portfolio
Author: NasirWd - http://www.nasfactor.com
**/

/*global $:false */
(function(){

  "use strict";
    $(window).load(function(){
      $("#nav-sticky").sticky({ topSpacing: 0 });
    });
/* home page soild features animation*/
$(window).scroll(function(){ // scroll event 
    var windowTop = $(window).scrollTop(); // returns number
    var nav=$('.solid-visuals');
    var solid_action = nav.offset().top-500;
    if (windowTop >= solid_action)
    {
        $('.solid-iphone,.solid-imac').addClass('animated fadeInUp');
    }

});

  /*smooth scrolling*/

  $(document).ready(function() {

//    $("html").niceScroll();  // The document page (html)

    $("body").niceScroll({touchbehavior:false,cursorcolor:"#000",cursoropacitymax:1,cursorwidth:8,background:"#333",autohidemode:true});

  });

$(function(){var nice=$(":nicescroll").getNiceScroll(0);$("#div1").html($("#div1").html()+' '+nice.version+' ($:'+$().jquery+')')})


  /*home page showcase javascript*/
$(function(){

        $(window).on("resize", function () {

        var etwrapper_height= $('.show-case-visual img').height();
        $('.et-wrapper').height(etwrapper_height);

        var etwrapper_basic_height= $('.folio-visual').height();
        $('.et-wrapper-basic').height(etwrapper_basic_height+100);

        var showcase_hover= $('.show-case-visual img').width();

         $('.show-case-visual').width(showcase_hover);
        $('.showcase-hover').width(showcase_hover-5);

        var $container = $('.masonry-container');
        $container.masonry({
          itemSelector: '.post-unit'
        });

      }).resize();

    /*flicker plugin*/

    $('.flicker').jflickrfeed({
        limit: 6,
        qstrings: {
            /*add your id below*/
            id: '52617155@N08'
        },
        itemTemplate: '<li><a href="{{image_b}}"><img alt="{{title}}" src="{{image_s}}" /></a></li>'
    });

    /*twitter plugin*/
    $('#tweets').tweetable({
        /*add your username below*/
        username: 'envato', 
        time: true,
        rotate: true,
        speed: 4000, 
        limit: 4,
        replies: false,
        position: 'append',
        loading: 'loading...' ,
        failed: "Sorry, twitter is currently unavailable for this user.",
        html5: true
    });


    /*testimonial slider*/
    $('.testi-inner').flexslider({
            animation: "slide",
            directionNav: false,
            controlNav: true,
            pauseOnHover: true, 
            slideshow: false,
            direction: "horizontal", //Direction of slides
    });

/*Flex blog slider*/

  $('.blog-slider').flexslider({
    animation: "slide",
    controlNav: false,
  });

/*Flex slider*/

  $('.flexslider').flexslider({
    animation: "slide"
  });

/* Light Box*/
$(document).ready(function() {
  $('.image-link').magnificPopup({type:'image'});
});


/*footer icons*/ 

    $('.head-icon').hover(function(){
        $(this).addClass('animated tada');
    },function(){
        $(this).removeClass('animated tada');
    });
    });  
})();

请帮我解决我的错误...

最佳答案

像这样在使用 top 之前检查元素是否存在。

var nav = $('.solid-visuals');
if (nav.length) {
 var solid_action = nav.offset().top-500;
}

关于javascript - 未捕获的类型错误 : Cannot read property 'top' of undefined . offset().top-500;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072855/

相关文章:

javascript - 使用 AngularJS require 选项调用另一个指令

javascript - 将服务器响应数据设置为要使用的变量

javascript - 当没有公开 viewController 时,如何将 cocoapod 转换为 typescript ?

javascript - 如何解决 ionic 2 中 self.context.anim is not a function 错误?

jquery - 大多数人使用的 "standard"JQuery TreeView 是什么?似乎不再支持最受欢迎的插件了?

html - CSS 下拉菜单

javascript - 动态更改样式表规则选择器

javascript - 我可以将一个模态对话框放在另一个模态对话框中吗?两个模态?

javascript - Ajax xml 未成功

Javascript 进度条没有更新 'on the fly' ,但是一旦进程完成就立即更新?