如何从其他页面重定向后将页面滚动到元素?
$(document).ready(function () { $("#button").click(function () { window.location.href = "page2.aspx"; $('html, body').animate({ scrollToElement ("#div").offset().top }, 2000); }); });
这个想法是,你单击第1页上的按钮,然后你被重定向到page2然后你使用jQuery滚动到一个特定的元素。
您始终可以为该元素设置锚点,如此
I wanna scroll here
并链接到它: http://mydomain.com/index.php#scroll
: http://mydomain.com/index.php#scroll
使用jQuery完成此任务的唯一好处是为滚动本身设置动画,在这种情况下,您可以执行以下操作:
I wanna scroll here
链接到这里: http://mydomain.com/index.php#scrollhere
: http://mydomain.com/index.php#scrollhere
然后在重定向页面的jQuery中:
$(document).ready(function() { if (window.location.hash != null && window.location.hash != '') $('body').animate({ scrollTop: $(window.location.hash).offset().top }, 1500); });