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 badges
Add a ment  | 

3 Answers 3

Reset to default 6

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

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