How do I fade a div in/out on page scroll?

后端 未结 2 1168
半阙折子戏
半阙折子戏 2021-02-08 19:35

Here is the jsfiddle: http://jsfiddle.net/NKgG9/

I\'m basically wanting those pink boxes to be on show on page load as normal but as soon as a user scrolls down the page

2条回答
  •  半阙折子戏
    2021-02-08 20:03

    Like this? http://jsfiddle.net/NKgG9/6/

    $(function(){ 
        var targets = $(".title, .social");
        if($(window).scrollTop() > 10){
          targets.hide();
        }
        $(window).scroll(function(){
            var pos = $(window).scrollTop();
            if(pos > 10){
                targets.stop(true, true).fadeOut("fast" );
            } else {
                targets.stop(true, true).fadeIn("fast");
            }
        });
    
    });
    

    The basic idea: subscribe to the scroll event. If the scroll position moves past a certain point, start the fade out and likewise if the user scrolls up fade in. Some important details: keep track if you're already fading in/out (variable shown) and stop any ongoing fade if you start a new fade.

提交回复
热议问题