javascript - jQuery - scroll or jump to top -
the context: have 1 page web app. there's lots of div
's being hidden @ 1 time (i'm not sure if matters). finding when user finished 1 page (page x), click (to page y) - if return page x position same when left page. button @ bottom, that's user ends again.
what want, when return page x them @ top of page can start again. whether scrolls or jumps - either way fine.
i've tried of following no success:
// scroll top settimeout(function(){ alert('scroll'); $('html, body').animate({scrolltop : 0}, 2000); }, 2000);
adding div id top-anchor
@ top , using:
$('html, body').animate({ scrolltop: $("#top-anchor").offset().top }, 2000);
having a
, using anchor, code below (it works once though, after hash in url no longer works suppose):
document.hash = '#top-anchor';
also tried:
window.scrollto(0, 0);
no luck.
any alternative ideas appreciated.
you can achieve that: demo : https://jsfiddle.net/yeyene/bpwtlg1w/1/
not sure how content divs shown , hidden, idea of adding scroll top of page div part.
add scroll event on button click event, since known page go, can scroll page's top, using...
$(element).position().top
jquery
$(document).ready(function () { $('input[type=button]').on('click', function(){ getpageid = $(this).attr('id'); $('.page').hide(0); $('div#'+getpageid).show(0); $('html,body').animate({ scrolltop: $('div#'+getpageid).position().top - 10 }, 500); }); });