Go to anchor link on click event jquery

独自空忆成欢 提交于 2019-12-11 04:45:32

问题


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)


回答1:


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. ...




回答2:


So 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);
});



回答3:


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.



来源:https://stackoverflow.com/questions/41624778/go-to-anchor-link-on-click-event-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!