<script>
$(document).ready(function () {
$("#button").click(function () {
window.location.href = "page2.aspx";
$('html, body').animate({
scrollToElement ("#div").offset().top
}, 2000);
});
});
</script>
The idea, is that you click a button on page1, then you get redirected to page2 and then you scroll to a specific element using jQuery.
Method 1: Using HTML: One can use the anchor tag to redirect to a particular section on the same page. You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag.
So, if you want to scroll smoothly to that part instead, you first need to reset the scroll position to 0 and then add smooth scrolling. // direct browser to top right away if (window. location. hash) scroll(0,0); // takes care of some browsers issue setTimeout(function(){scroll(0,0);},1);
scroll() The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.
The first one is with javascript: set the scrollTop property of the scrollable element (e.g. document. body. scrollTop = 1000; ). The second is setting the link to point to a specific id in the page e.g.
You can always set an anchor for that element, like so
<a name="scroll"></a>
<h2>I wanna scroll here</h2>
And link to it: http://mydomain.com/index.php#scroll
The only benefit in using jQuery for this task would be to animate the scrolling itself, in which case you do something like this:
<h2 id="scrollhere">I wanna scroll here</h2>
Link here: http://mydomain.com/index.php#scrollhere
And then in jQuery in the redirected page:
$(document).ready(function() {
if (window.location.hash != null && window.location.hash != '')
$('body').animate({
scrollTop: $(window.location.hash).offset().top
}, 1500);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With