Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate if scrollTop less than specified value - does not work

ease-scroll is a div with one anchor tag in it.

<div id="ease-scroll">
    <a href="#">&uarr; Top</a>      
</div>

I need the div's opacity to be 0.9 when scrollTop() is greater than 450, and this works as expected, and if I now scroll up thereby scrollTop() value is less than 450, I want to revert the opacity to original value 0.1. But, revert opacity value is not happening. Any clue what is wrong?

// Help navigate!
jQuery(window).scroll(function () { 
    if ( jQuery(window).scrollTop() > 450 && jQuery("#ease-scroll").css("opacity") != 0.9 ) {
       jQuery("#ease-scroll").animate( {opacity: 0.9}, 'medium');
    }
    if ( jQuery(window).scrollTop() < 450 && jQuery("#ease-scroll").css("opacity") != 0.1 ) {
        jQuery("#ease-scroll").animate( {opacity: 0.1}, 'medium');
    }
});
like image 416
venkrao Avatar asked Apr 07 '26 05:04

venkrao


1 Answers

jsBin demo

jQuery(function( $ ){

   $(window).scroll(function(){
       
      var scrolled = $(window).scrollTop();
      $("#ease-scroll").stop().animate({opacity: (scrolled>450 ? 0.9 : 0.1) }, 600);

   });

});
like image 132
Roko C. Buljan Avatar answered Apr 08 '26 18:04

Roko C. Buljan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!