开发者

jQuery onClick GoTo ID or Class

开发者 https://www.devze.com 2023-03-04 23:04 出处:网络
For some odd reason my Header gets all messed up when I click on an A tag to go to an ID.So instead 开发者_StackOverflow社区is there a way to use jQuery to do this, such as a Click(), Goto ID?

For some odd reason my Header gets all messed up when I click on an A tag to go to an ID. So instead 开发者_StackOverflow社区is there a way to use jQuery to do this, such as a Click(), Goto ID?

<a href="#allreviewstop">Read Reviews (1)</a>
<div style="height:1500px;"> Really Long Stuff</div>
<div id="allreviewstop"> My Reviews go down here</div>

This is the page I'm dealing with Click Here


$("div").click(function() {
    window.location.hash = "#"+$(this).attr("id");
}

Is that what you're after?

[edit] I can't remember if you need the # or not. Try it without if it doesn't work.


Try the jquery ScrollTo plugin -

also check the demo

$(function(){
    $("#goTop").click(function(){
      $.scrollTo($("#nav"), { duration: 0});
    });
});


Does this plugin helps you: jQuery ScrollTo ?


In my experience the window.location.hash solution only works once. If you don't want to use the plugin you could try this:

var navigationFn = {
    goToSection: function(id) {
        $('html, body').animate({
            scrollTop: $(id).offset().top
        }, 0);
    }
}

and then call it like so (where someID is the ID of the element you wish to scroll to):

navigationFn.goToSection('#someID');

With this you can also vary the animation speed (I have it at 0) so that it is instant, but you could pass the value to the function so the code is reusable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号