0

Im trying to set a div that I want to scroll to when I click a div in my menu. The problem is that the scroll to the div, is not scrolling to the div that I have in my code, It always scroll to something else on my page, strange. Im using the code bellow:

$(".dns-anchor").click(function() {
    $('html, body').animate({
        scrollTop: $("#dns-scroll").offset().top
    }, 2000);
});

ID #dns-scroll is the div that I want to scroll to.

Bruno Alves
  • 51
  • 1
  • 1
  • 7

1 Answers1

0
$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

or

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

Please check this link:-Smooth scroll to div id jQuery

Community
  • 1
  • 1
Razia sultana
  • 2,168
  • 3
  • 15
  • 20