2014-11-04 42 views
10

如何使此功能在滾動後添加課程100vh
目前它在850px之後添加類。如何在100vh滾動後切換課程

$("document").ready(function($){ 
    var nav = $('#verschwinden'); 

    $(window).scroll(function() { 
     if ($(this).scrollTop() > 850) { 
      nav.addClass("doch"); 
     } else { 
      nav.removeClass("doch"); 
     } 
    }); 
}); 
+0

它不應該是'$(文件)' ?或甚至更短:'jQuery(function($){' – 2014-11-04 07:29:58

+0

我不知道,這是我在互聯網上找到的,它的工作原理 – 2014-11-04 07:32:20

回答

14

jQuery中100vh是簡單$(window).height()而在純JavaScript是window.innerHeightor a bit more longer

jsFiddle demo

jQuery(function($) { 

    var $nav = $('#verschwinden'); 
    var $win = $(window); 
    var winH = $win.height(); // Get the window height. 

    $win.on("scroll", function() { 
     if ($(this).scrollTop() > winH) { 
      $nav.addClass("doch"); 
     } else { 
      $nav.removeClass("doch"); 
     } 
    }).on("resize", function(){ // If the user resizes the window 
     winH = $(this).height(); // you'll need the new height value 
    }); 

}); 

你也可以通過簡單的使用使if部分短一點:

$nav.toggleClass("doch", $(this).scrollTop() > winH); 

demo

+0

不錯,它工作!但我忘記Naviagtion也採取6vh,有沒有某種方式從窗口高度去除導航的高度? – 2014-11-04 08:07:45

+0

6視窗高度是6%sry – 2014-11-04 08:12:21