Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll element to top of page by clicking anchor inside element or anywhere inside containing div id

I would like to scroll the page to the top of the e.g. navigation div ID when clicking either any of the links inside the navigation div ID or (if possible) when clicking anywhere into the div ID itself containing the navigation links.

I have researched this and the closest I have come to is jQuery - How to scroll an anchor to the top of the page when clicked? however for some reason it does not seem to work when I try to do this in my example. What am I doing wrong?

I am new to jQuery and medium at html and css (and used inline styling of the divs since I did not want to supply a separate css just for the example).

When I load the html and script from jQuery - How to scroll an anchor to the top of the page when clicked? in Aptana it also does not scroll. Even with a content div above the div class work so that there is space to scroll to at all.

I have included two little paragraphs inside the concerning divs to explain exactly what I mean.

Any help is very much appreciated. Thank you.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
        $('#scroll_navigation ul li').click(function() {
             $('html,body').animate({scrollTop: $(this).offset().top}, 800);
        }); 
    </script>

<body>

<div id="logo" style="margin: 0 auto; height: 300px; width: 60%; border: 1px solid #000">Logo</div>

    <div id="scroll_navigation" style="margin: 0 auto; width: 40%; border: 4px solid #225599">

        <p>Scroll navigation to top of page</p>
        <p>Catch any clicks in "#scroll_navigation" to scroll the div id to the top of the page</p>


            <div id="navigation">

                <div class="container" style="margin: 0 auto; height: 220px; width: 60%; border: 1px solid #000">

                    <ul>
                        <p>Catch clicks on anchors to scroll the div id to the top of the page</p>
                        <li><a href="#Portfolio" title="">Portfolio</a></li>
                        <li><a href="#Services" title="">Services</a></li>
                        <li><a href="#About" title="">About</a></li>
                        <li><a href="#Contact" title="">Contact</a></li>
                    </ul>

                </div>

            </div>

    </div>

<div id="content" style="margin: 0 auto; height: 1500px; width: 60%; border: 1px solid #000">Content</div>

</body>

EDIT Beware this when using scrollTop in Firefox. Animate scrollTop not working in firefox jQuery scrollTop - no support for negative values in Mozilla / Firefox Took me a while to figure out my code was fine but scrollTop was this issue in FF. In this same example the behaviour can also be observed. When scrolling down and making the anchors fixed, FF will scroll up to random positions either 2-4px before or after the target div.

I am now using Scrolld.js to achieve pixel perfect scrolling to the desired points across major browsers. I don't understand how different browser engines can render such a common thing as scrollTop from different (either html or body) inputs as far as I understand and I am new to jQuery. Guess this is not "the fault" of jQuery but how the browser engines render scrollTop.

like image 784
moderntimes Avatar asked Oct 13 '13 23:10

moderntimes


People also ask

How do you make a div scroll to the top?

Use the scrollTo() Method to Scroll to Top of Div in JavaScript. The scrollTo() method takes parameters to reset the viewport measurements. Usually, the current state of the viewport will have its position on the x-axis and y-axis.

How do you scroll with 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 scroll down to a specific element in JavaScript?

The scrollIntoView() method scrolls an element into the visible area of the browser window.

How do you scroll in a container?

// get the "Div" inside which you wish to scroll (i.e. the container element) const El = document. getElementById('xyz'); // Lets say you wish to scroll by 100px, El. scrollTo({top: 100, behavior: 'smooth'}); // If you wish to scroll until the end of the container El. scrollTo({top: El.


1 Answers

I dont know if you did, but you have to include jquery to use it. Another thing would be, that your "click listener" has to be in jqueries document ready function like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>

$(document).ready(function(){
    $('#scroll_navigation ul li').on('click', function(){
        $('html,body').animate({scrollTop: $(this).offset().top}, 800);
    }); 
});  

</script>
like image 80
malifa Avatar answered Oct 22 '22 12:10

malifa