Smooth scroll to div id jQuery

0 votes

I've been trying to get a scroll to div id jquery code to work correctly. Based on another stack overflow question i tried the following

DEMO http://jsfiddle.net/kevinPHPkevin/8tLdq/

$('#myButton').click(function() {
   $.scrollTo($('#myDiv'), 1000);
});

But it didn't work. It just snaps to the div. I also tried

$('#myButton').click(function(event) {
     event.preventDefault();
   $.scrollTo($('#myDiv'), 1000);
});

With no progress.

Jun 23, 2022 in Web Development by gaurav
• 23,260 points
3,270 views

1 answer to this question.

0 votes

You need to animate the html, body

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

Hope it helps!!
Thank you!!

answered Jun 23, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

Scroll / Jump to id without jQuery

I have search a lot of topics ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
2,328 views
0 votes
0 answers

scroll up and down a div on button click using jquery

I am trying to add a feature ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
8,164 views
0 votes
1 answer

Not able to show/hide div which is nearest to checkbox using JQuery

pretty simple: $('.check-hide-show-content').hide(); // hide all content divs // ...READ MORE

answered Aug 2, 2022 in Web Development by rajatha
• 7,680 points
1,470 views
0 votes
0 answers

How to stretch a GIF to fill the whole div using jQuery Mobile?

I'm using jQuery Mobile to create a ...READ MORE

Aug 22, 2022 in Web Development by gaurav
• 23,260 points
823 views
0 votes
1 answer

Smooth scrolling when clicking an anchor link

document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', ...READ MORE

answered Jun 20, 2022 in JQuery by rajatha
• 7,680 points
2,240 views
0 votes
0 answers

Scroll to an element with jQuery

I have this input element: <input type="text" class="textfield" ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
514 views
0 votes
0 answers

How to scroll to top of page with JavaScript/jQuery?

Is there a way to control browser ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
2,278 views
0 votes
0 answers

Smooth scrolling when clicking an anchor link

My page has a few hyperlinks. When ...READ MORE

Jul 22, 2022 in CSS by Edureka
• 13,620 points
532 views
0 votes
1 answer

How to hide a div with jQuery?

We hide the divs by adding a CSS ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
439 views
0 votes
1 answer

How to detect current element id in jQuery?

Use the jQuery attr() Method You can simply ...READ MORE

answered Jun 30, 2022 in Web Development by rajatha
• 7,680 points
854 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP