Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade on scroll effect

I've added a script I found on here. It works a treat. However, I've come stuck when trying to apply a fade effect to it. At the minute it just cuts out.

script type="text/javascript">


    $(document).ready(function() {


        $(window).scroll(function () {
              var height = $('body').height();
              var scrollTop = $('body').scrollTop();
              var opacity = 1;


              if(scrollTop > 400) {
                  opacity = 0;
              }

           $('.social').css('opacity', opacity);
        });
    });
</script>

EDIT: I tied this and it works. Thanks so much guys:

<script type="text/javascript"> 
$(window).scroll(function() { 
// The social div 
var $socialDiv = $('.social'); 

//Get scroll position of window 
var windowScroll = $(this).scrollTop(); 

//Slow scroll of social div and fade it out 
$socialDiv.css({ 
'margin-top' : - (windowScroll / 3) + "px", 
'opacity' : 1 - (windowScroll / 550) 
}); 
}); 
</script>​
like image 885
Michael Avatar asked Nov 04 '22 20:11

Michael


1 Answers

You should use .animate() or .fadeTo() for that effect.

Using .css('opacity', opacity); will make make your element to disappear abruptly.

like image 72
RRikesh Avatar answered Nov 13 '22 20:11

RRikesh