How can I scroll to a specific location on the page using jquery?

JqueryScrollScrolltoJquery Scrollable

Jquery Problem Overview


Is it possible to scroll to a specific location on the page using jQuery?

Does the location I want to scroll to have to have:

<a name="#123">here</a>

Or can it just move to a specific DOM id?

Jquery Solutions


Solution 1 - Jquery

jQuery Scroll Plugin

since this is a question tagged with jquery i have to say, that this library has a very nice plugin for smooth scrolling, you can find it here: http://plugins.jquery.com/scrollTo/

Excerpts from Documentation:

$('div.pane').scrollTo(...);//all divs w/class pane

or

$.scrollTo(...);//the plugin will take care of this

Custom jQuery function for scrolling

you can use a very lightweight approach by defining your custom scroll jquery function

$.fn.scrollView = function () {
	return this.each(function () {
		$('html, body').animate({
			scrollTop: $(this).offset().top
		}, 1000);
	});
}

and use it like:

$('#your-div').scrollView();

Scroll to a page coordinates

Animate html and body elements with scrollTop or scrollLeft attributes

$('html, body').animate({
    scrollTop: 0,
    scrollLeft: 300
}, 1000);

Plain javascript

scrolling with window.scroll

window.scroll(horizontalOffset, verticalOffset);

only to sum up, use the window.location.hash to jump to element with ID

window.location.hash = '#your-page-element';

Directly in HTML (accesibility enhancements)

<a href="#your-page-element">Jump to ID</a>

<div id="your-page-element">
    will jump here
</div>

Solution 2 - Jquery

Yep, even in plain JavaScript it's pretty easy. You give an element an id and then you can use that as a "bookmark":

<div id="here">here</div>

If you want it to scroll there when a user clicks a link, you can just use the tried-and-true method:

<a href="#here">scroll to over there</a>

To do it programmatically, use scrollIntoView()

document.getElementById("here").scrollIntoView()

Solution 3 - Jquery

There is no need to use any plugin, you can do it like this:

var divPosition = $('#divId').offset();

then use this to scroll document to specific DOM:

$('html, body').animate({scrollTop: divPosition.top}, "slow");

Solution 4 - Jquery

Here's a pure javascript version:

location.hash = '#123';

It'll scroll automatically. Remember to add the "#" prefix.

Solution 5 - Jquery

Plain Javascript:

window.location = "#elementId"

Solution 6 - Jquery

<div id="idVal">
    <!--div content goes here-->
</div>
...
<script type="text/javascript">
     $(document).ready(function(){
         var divLoc = $('#idVal').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
</script>

This example shows to locate to a particular div id i.e, 'idVal' in this case. If you have subsequent divs/tables that will open up in this page via ajax, then you can assign unique divs and call the script to scroll to the particular location for each contents of divs.

Hope this will be useful.

Solution 7 - Jquery

<script type="text/javascript">
    $(document).ready(function(){
        $(".scroll-element").click(function(){
            $('html,body').animate({
                scrollTop: $('.our_companies').offset().top
            }, 1000);

            return false;
        });
    })
</script>

arrow

Solution 8 - Jquery

Try this

<div id="divRegister"></div>

$(document).ready(function() {
location.hash = "divRegister";
});

Solution 9 - Jquery

Here is variant of @juraj-blahunka's lightweight approach. This function does not assume the container is the document and only scrolls if the item is out of view. Animation queuing is also disabled to avoid unnecessary bouncing.

$.fn.scrollToView = function () {
    return $.each(this, function () {
        if ($(this).position().top < 0 ||
            $(this).position().top + $(this).height() > $(this).parent().height()) {
            $(this).parent().animate({
                scrollTop: $(this).parent().scrollTop() + $(this).position().top
            }, {
                duration: 300,
                queue: false
            });
        }
    });
};

Solution 10 - Jquery

Using jquery.easing.min.js, With fixed IE console Errors

Html

<a class="page-scroll" href="#features">Features</a>
<section id="features" class="features-section">Features Section</section>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="js/jquery.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>

        <!-- Scrolling Nav JavaScript -->
        <script src="js/jquery.easing.min.js"></script>

Jquery

//jQuery to collapse the navbar on scroll, you can use this code with in external file with name scrolling-nav.js
        $(window).scroll(function () {
        	if ($(".navbar").offset().top > 50) {
        		$(".navbar-fixed-top").addClass("top-nav-collapse");
        	} else {
        		$(".navbar-fixed-top").removeClass("top-nav-collapse");
        	}
        });
        //jQuery for page scrolling feature - requires jQuery Easing plugin
        $(function () {
        	$('a.page-scroll').bind('click', function (event) {
        		var anchor = $(this);
        		if ($(anchor).length > 0) {
        			var href = $(anchor).attr('href');
        			if ($(href.substring(href.indexOf('#'))).length > 0) {
        				$('html, body').stop().animate({
        					scrollTop: $(href.substring(href.indexOf('#'))).offset().top
        				}, 1500, 'easeInOutExpo');
        			}
        			else {
        				window.location = href;
        			}
        		}
        		event.preventDefault();
        	});
        });

Solution 11 - Jquery

You can use scroll-behavior: smooth; to get this done without Javascript

https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmrblahView Question on Stackoverflow
Solution 1 - JqueryJuraj BlahunkaView Answer on Stackoverflow
Solution 2 - JquerynickfView Answer on Stackoverflow
Solution 3 - JqueryMeghdad HadidiView Answer on Stackoverflow
Solution 4 - Jqueryo.k.wView Answer on Stackoverflow
Solution 5 - JqueryLuiz PicançoView Answer on Stackoverflow
Solution 6 - Jqueryuser2793243View Answer on Stackoverflow
Solution 7 - JqueryYogesh RathodView Answer on Stackoverflow
Solution 8 - JqueryAjay-SystematixView Answer on Stackoverflow
Solution 9 - JqueryRichieView Answer on Stackoverflow
Solution 10 - JquerydevView Answer on Stackoverflow
Solution 11 - JquerySairamView Answer on Stackoverflow