简体   繁体   English

无需滚动即可转到特定 div

[英]Go to particular div without scroll

Need to land a particular element on click but without scrolling.需要点击特定元素但不滚动。 Below I have a code but it is scrolling to a particular element on click, Is there any way to go to a particular HTML element without scroll?下面我有一个代码,但它在单击时滚动到特定元素,有没有办法在不滚动的情况下转到特定的 HTML 元素?

$("#button").click(function() {
    $("html, body).animate({
        scrollTop: $("#myelement").offset().top
    }, 2000);
});
<a href="#test" id="GoToAtag">Go to p tag</a>
<div style="background-color:lightblue;height:800px" >

</div>

<p id="test">This is some text.</p>
<a href="#GoToAtag">Go to a tag</a>

Check this one for move to any element using ID and anchor or try this jsfiddle.net/thepeanut/ohhffte6 url检查这个使用 ID 和锚点移动到任何元素或试试这个 jsfiddle.net/thepeanut/ohhffte6 url

Below code is working fine.下面的代码工作正常。 Now, it is going on top instantly without animation.现在,它在没有动画的情况下立即进入顶部。 When i am firing click event..当我触发点击事件时..

$("#button").click(function() {
     $("html, body").scrollTop(0);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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