admin管理员组文章数量:1291229
As the descriptions says, I've found many things about smooth scrolling and the location property on java script but nothing seems to do what Im looking for which is simply imitate the function of a html "a" tag (I need to NOT use the html link tag for this project)
As the descriptions says, I've found many things about smooth scrolling and the location property on java script but nothing seems to do what Im looking for which is simply imitate the function of a html "a" tag (I need to NOT use the html link tag for this project)
Share Improve this question asked Jan 12, 2017 at 23:16 Marco Ramirez CastroMarco Ramirez Castro 3462 gold badges6 silver badges23 bronze badges3 Answers
Reset to default 6So what i get is you want to scroll down to an element without using html anchor tag If i am right Assuming you have a html input tag
<input type="button" id="top" value="button"/>
this script will do the work
P.S #bottom_id is the id of element you want to scroll to. or anchor link:
<a href="#bottom" id="bottom_id"></a>
script:
$("#top").click(function() {
$('html, body').animate({
scrollTop: $("#bottom_id").offset().top
}, 2000);
});
The jquery way:
$("body").scrollTop($("[name=elementname]").position().top)
This gets the top of the named element and sets the scroll top position to that location.
There is a jquery plug in which does this effectively, but if you want to stick to straight javascript you can just use element.scrollTo as described on this previous answer;
Better documentation for this can be found on Mozilla's site. With javascript.
To make JQuery take the user to a different URL than the page (like an anchor tag) attach window.location to the event.
For example, this:
$('#ClickMe').click(function(){ window.location = 'index.php'; });
Would take the user to index.php if he clicks on the element that has the ID "ClickMe" (E.G. ...
本文标签: javascriptGo to anchor link on click event jqueryStack Overflow
版权声明:本文标题:javascript - Go to anchor link on click event jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741526885a2383521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论