Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth scroll to specific div on click

What I'm trying to do is make it so that if you click on a button, it scrolls down (smoothly) to a specific div on the page.

What I need is if you click on the button, it smooth scrolls to the div 'second'.

.first {      width: 100%;      height: 1000px;      background: #ccc;  }    .second {      width: 100%;      height: 1000px;      background: #999;  }
<div class="first"><button type="button">Click Me!</button></div>  <div class="second">Hi</div>
like image 415
Erik Fischer Avatar asked Aug 06 '13 02:08

Erik Fischer


People also ask

How do I scrollTo a specific div in HTML?

If you want to scroll the current document to a particular place, the value of HREF should be the name of the anchor to which to scroll, preceded by the # sign. If you want to open another document at an anchor, give the URL for the document, followed by #, followed by the name of the anchor.

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

Modern Browsers detect the hash in the url and then automatically open that part. 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.

How do I smooth scrollTo Element?

To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.


1 Answers

do:

$("button").click(function() {     $('html,body').animate({         scrollTop: $(".second").offset().top},         'slow'); }); 

Updated Jsfiddle

like image 141
Sudhir Bastakoti Avatar answered Oct 12 '22 11:10

Sudhir Bastakoti