Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scroll the page to an element after being re-directed from another page? [closed]

<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.

like image 296
gabor_Csapo Avatar asked May 26 '13 07:05

gabor_Csapo


People also ask

How do you go to a specific section in HTML?

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.

How do you navigate to another page with a smooth scroll on a specific ID?

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);

How do you scroll down elements?

scroll() The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.

How do I automatically scroll down a page in HTML?

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.


1 Answers

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);
});
like image 83
casraf Avatar answered Nov 08 '22 18:11

casraf