jquery - 在大屏幕上滚动量后添加body类

时间:2015-06-29 20:33:21

标签: jquery scroll resize window-resize

我已经阅读了几个不同的主题并且无法完成这项工作。我试图在动态滚动量之后向body标签添加一个类,并且只在非移动窗口宽度上添加一个类(> = 768)

这是我目前的代码。非常感谢任何帮助!

 var $document = $(document),
     $element = $('body'), //body tag - to have class appended
     $topHeight = $('.slider_image').height() + 75, //height of the slider div (dynamic) + the height of the navbar (75px)
     className = 'sticky-scroll'; //class I want to add to <body>
 $document.scroll(function() { //function to run on pageload
     if ($(window).width() >= 768) && ($document.scrollTop() >= $topHeight) { //multiple conditions
         $element.addClass(className);
     } else {
         $element.removeClass(className);
     }
 });
 $(window).resize(function() { //reload function when window resized
     var $topHeight = $('.slider_image'); //recalculate $topHeight
     $document.scroll(function() { //re-running function
         if ($(window).width() >= 768) && ($document.scrollTop() >= $topHeight) { //multiple conditions
             $element.addClass(className);
         } else {
             $element.removeClass(className);
         }
     });
 });

0 个答案:

没有答案