简体   繁体   中英

disable url change when link to a different part of the same page is clicked

suppose I have a button on www.domain.com linking to a lower part of the page through an anchor tag <a href="#destination">text link</a>

is it possible to prevent the url in url bar of the browser from showing www.domain.com/#destination ?

thanks

Use jquery like this:

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

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

Hope this helps.

fiddle:

http://jsfiddle.net/ZGk5F/

You can use this:

 $("a.anchor").on("click", function(e) { e.preventDefault(); $(window).scrollTop($($(this).attr('href')).offset().top); }); 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <a href="#second" class="anchor">Second</a> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <h2 id="second">Second Part</h2> 

Now the anchor link shows the address on hover, But scrolls to destination instead of navigation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM