Change CSS element with JQuery when scroll reaches an anchor point

前端 未结 3 1166
不知归路
不知归路 2020-11-28 08:51

I currently have this solution to change the css elements when the page reaches a certain point but I\'d like to use an #anchor-point instead of the pixel value (1804) to be

相关标签:
3条回答
  • 2020-11-28 09:22
    function scroll_style() {
       var window_top = $(window).scrollTop();
       var div_top = $('#anchor-point').offset().top;
    
       if (window_top > div_top){
          $('#voice2').css({"border-bottom":"2px solid #f4f5f8"});
          $('#voice3').css({"border-bottom":"2px solid #2e375b"});
       }
    }
    $(function() {
      $(window).scroll(scroll_style);
      scroll_style();
     });
    

    Solution based on: http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html

    0 讨论(0)
  • 2020-11-28 09:27

    Try this:

    var targetOffset = $("#anchor-point").offset().top;
    
    var $w = $(window).scroll(function(){
        if ( $w.scrollTop() > targetOffset ) {   
            $('#voice2').css({"border-bottom":"2px solid #f4f5f8"});
            $('#voice3').css({"border-bottom":"2px solid #2e375b"});
        } else {
          // ...
        }
    });
    
    0 讨论(0)
  • 2020-11-28 09:27
    $(window).bind("scroll", function() { 
       var $sec1 = $('.bg1').offset().top;
         var $sec2 = $('.bg2').offset().top;
         var $sec3 = $('.bg3').offset().top;
       var $sec4 = $('.bg4').offset().top; 
       var $sec5 = $('.carousel-indicators').offset().top;   
    
       if ($(this).scrollTop() < $sec2){ 
         $(".navbar1").fadeOut();  
         $(".navbar2").fadeOut();  
         $(".navbar3").fadeOut();  
       }     
       if ($(this).scrollTop() > $sec2 & $(this).scrollTop() < $sec3){ 
         $(".navbar1").fadeIn();   
         $(".navbar2").fadeOut();      
       } 
       if ($(this).scrollTop() > $sec3 & $(this).scrollTop() < $sec4){ 
         $(".navbar2").fadeIn();  
         $(".navbar3").fadeOut();  
       }    
       if ($(this).scrollTop() > $sec4 & $(this).scrollTop() < $sec5){ 
         $(".navbar3").fadeIn();  
         $(".navbar2").fadeOut();  
       }    
       if ($(this).scrollTop() > $sec5){ 
         $(".navbar1").fadeOut();  
         $(".navbar2").fadeOut();  
         $(".navbar3").fadeOut();   
       }         
     });
    
    0 讨论(0)
提交回复
热议问题