jquery - .animate() query not working -
i wanted implement "scroll top" button in static html site, i've done million times before. added code: (first part making button appear after time, don't worry that)
$( document ).ready(function() { $(window).scroll(function() { var scroll = $(document).scrolltop(); var wheight = $(window).height(); var hheight = wheight / 2; if(scroll >= hheight) { $('#scroller').css({'opacity': '1'}); } else if (scroll <= hheight) { $('#scroller').css({'opacity': '0'}); } }); $('#scroller').click(function() { alert("click works!"); $('html, body').animate({ scrolltop: 0 }, 500); }); });
the appearing , disappearing of button works , alerts "click works" if click button. scrolling won't work. lost. read somewhere shouldn't use height: 100%
in css , use
height: auto; min-height: 100%;
instead. won't change thing. i've tried use document
element scroll.
i glad if can me!
it seems code works:
$( document ).ready(function() { $(window).scroll(function() { var scroll = $(document).scrolltop(); var wheight = $(window).height(); var hheight = wheight / 2; if(scroll >= hheight) { $('#scroller').css({'opacity': '1'}); } else if (scroll <= hheight) { $('#scroller').css({'opacity': '0'}); } }); $('.scroll').click(function() { $('html, body').animate({ scrolltop: 0 }, 500); }); });
* { box-sizing: border-box; } html, body { margin: 0; } body { background: linear-gradient(to top, red, blue); height: 2000px; border: 10px solid goldenrod; position: relative; } .scroll { font-size: 2em; position: absolute; bottom: 0; display: block; width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="scroll">scroll</button>
Comments
Post a Comment