NeviJ NeviJ - 1 year ago 67
HTML Question

How to scrollTop to a div properly in jQuery?

Ok so basically I have:

<button class="contactButton">XYZ</button>


And when user will press this button I want my page to scroll down to specific div.

I tried this:

$(".contactButton").click(function () {
$("html,body").animate({
scrollTop: $(".specificDivClass").offset().top
}, 1100);
});


And I also tried other solutions from this site but without results. I'm new to jQuery so thanks for any advices.

Answer Source

Here you go with a solution https://jsfiddle.net/psfLbo1n/

$(".contactButton").click(function () {
    $("html,body").animate({
    	scrollTop: $(".specificDivClass").offset().top
    }, 1100);
});
.div1{
  height: 700px;
  width: 100%;
  background: red;
}

.specificDivClass{
  height: 1000px;
  width: 100%;
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="contactButton">XYZ</button>

<div class="div1">

</div>

<div class="specificDivClass">

</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download