user2530708
user2530708

Reputation: 1

Prevent an auto reload caused by jquery scroll function

I'm using a simple piece of code inorder to have internal links scrolls to specific divs. It works they way I intended but it also reloads the page. The scroll works perfect, just that I need it to not reload the page. Also if this matters, Im using this inside a Wordpress theme I created.

HTML:

<a href="#hiremenow">This may sound crazy, So hire me maybe?</a>
<div id="hiremenow"></div>

SCRIPT:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();
        var target = this.hash,
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
         void(0);
        });
    });

});

Upvotes: 0

Views: 832

Answers (1)

mgs
mgs

Reputation: 5176

Return a false value.

$('a[href^="#"]').on('click',function (e) {
   //whatever...
   return false;
});

This means "the click event has been handled, do not perform the standard action".

Upvotes: 1

Related Questions