javascript - How to make page scroll to a specific element when clicking a button? Scroll animation, if possible -
as title says want make page scroll specific element on page when user clicks text/image. if it's possible, don't want skip element, have scrolling animation it. know it's possible in js, forgot how it.
depending on specific needs can organise code in such way, can pass id of element needs scrolled click handler.
<a href="#scrollhere">click me</a> <div id="scrollhere"></div> $('a[href^="#"]').on('click', function(event) { var target = $(this.href); if( target.length ) { event.preventdefault(); $('html, body').animate({ scrolltop: target.offset().top }, 1000); } }); this scroll page #scrollhere element on period of 1 second (1,000 milliseconds = 1 second).
or in more generic way:
$('.someelement').on('click', function(event) { var target = $('#someelementtoscrollto'); $('html, body').animate({ scrolltop: target.offset().top }, 1000); });
Comments
Post a Comment