18. Feb 2018 18:50 by Ivan • Snippets • # # # #

Smooth scroll to element using jQuery

If you are currently developing one page website, or just having a page where you navigate to certain elements, chances are that you’ll want that scroll to be a smooth and clean. Immediately you think I will need some scrolling JS plugin/script (like ScrollMagic), but there is a much simpler and easier way:

$('a[href*="#"]').on('click', function (el) {
  el.preventDefault();

  $('html, body').animate({
    scrollTop: $($(this).attr('href')).offset().top
  }, 500, 'linear');
});

Just change the 500 to whatever speed (in milliseconds) you want the page to scroll at.

4 responses to “Smooth scroll to element using jQuery”

  1. I was curious if you ever thought of changing the structure of your blog?

    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful
    lot of text for only having one or two images. Maybe you could
    space it out better?

  2. These are in fact great ideas in about blogging. You have
    touched some pleasant points here. Any way keep up wrinting.

  3. Hello there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My website goes over a lot of the same topics as yours and I believe we could greatly benefit from each other. If you’re interested feel free to send me an email. I look forward to hearing from you! Fantastic blog by the way!

  4. Aw, this was a very nice post. Taking a few minutes and
    actual effort to make a great article… but what can I say… I put things off a lot and don’t seem to get nearly anything done.

Leave a Reply to leonardo noll Cancel reply

Your email address will not be published. Required fields are marked *

Smooth scroll to element using jQuery

by Ivan time to read: <1 min
4