简体   繁体   English

jQuery scrollTop滚动问题

[英]jQuery scrolling issues with scrollTop

I am having 2 issues with scrolling, i figured it would just be easier to ask them both in one post. 我在滚动时遇到2个问题,我认为在一个帖子中问两个问题会更容易。

Before I ask, below is my jQuery code im having issues with. 在我问之前,下面是我的jQuery代码存在问题。

$("a").click(function() {
    if(this.hash){
        $("#main").animate({
            scrollTop : $("h3[name="+this.hash.substr(1)+"]").position().top - 120
        },2000,"easeOutExpo");
    }
});

Situation: What I have going on in my page is basically i have a side menu with a couple lists. 情况:我在页面上所做的基本上是我有一个带有几个列表的侧边菜单。 Each item in each list links to a anchor in my main div section, or my 'content' section. 每个列表中的每个项目都链接到我的主要div部分或“内容”部分中的锚点。 When a list item is clicked, the code above runs and it scrolls to one of the anchors. 单击列表项后,上面的代码运行,并且滚动到锚点之一。

Issue 1: When i click on a item in one of the lists, it scrolls down to a anchor which works just fine. 问题1:当我单击列表之一中的项目时,它向下滚动到锚点,效果很好。 But when that same item is clicked again, the main area scrolls back up to the top of the div. 但是当再次单击同一项目时,主区域将滚动回到div的顶部。 My thought to fix this was to check the current 'scrolled to' location of the div, and then not allow the code to run again if the location hadn't changed since the first click but i couldn't get that to work. 我想解决此问题的方法是检查div的当前“滚动到”位置,然后,如果自第一次单击以来位置未更改,则不允许代码再次运行,但我无法使其正常工作。 Any suggestions on how to fix this issue? 有关如何解决此问题的任何建议?

Issue 2: Again as stated above, when i click on a item in a list it scrolls down to a anchor. 问题2:再次如上所述,当我单击列表中的项目时,它会向下滚动到锚点。 What i then want to be able to to is click on a different list item and have it scroll to that position. 然后,我希望能够单击另一个列表项,并将其滚动到该位置。 The problem is when i click on a different list item, it scrolls to some random position in the main div, positions i haven't even anchored yet. 问题是,当我单击其他列表项时,它会滚动到主div中的某个随机位置,这些位置我什至还没有定位。 Can anyone explain how I can make it so i can scroll from anchor to anchor? 谁能解释我如何做到这一点,以便我可以在锚点之间滚动?

Note: Please respond by having issue 1 or issue 2 above your explanation so i know which one your referring to. 注意:请在您的解释上方添加问题1或问题2作为答复,以便我知道您所指的是哪一个。 Thanks 谢谢

EDIT: Thanks to Roko's help i got it working. 编辑:感谢Roko的帮助,我让它正常工作。 For future viewers here is the fiddle of a working demo: http://jsfiddle.net/TsUcc/3/ and below is what the finally jquery code looks like 对于将来的观看者来说,这是一个正在运行的演示的小提琴: http : //jsfiddle.net/TsUcc/3/以下是最终的jquery代码如下所示

$("a").click(function( e ) {
    e.preventDefault();

    if(this.hash){

        var target = '#'+ this.hash.substr(1);
        var destination = $(target).offset().top - 10;

        $('#main').stop().animate({
            scrollTop : '+=' + destination
        }, 1000);
    }
});

LIVE DEMO 现场演示

ISSUE 1: 问题1:

you probably use something like: <a href="#goto">goto</a> and you did not prevented the browser default behavior which is simply done by passing the event to the click handler like: 您可能使用了诸如<a href="#goto">goto</a>并且没有阻止浏览器的默认行为,只需将事件传递给click处理程序即可,例如:

$("a").click(function( e ) {
     e.preventDefault();
     // .....
});

ISSUE 2 问题2

The HTMLelement position you're using is just a passive position of an element relative to it's positioned parent element. 您使用的HTMLelement position仅仅是元素相对于其父元素位置的被动位置。 which means that an element can have a position of ie: 30 even if it's at the bottom of your page. 也就是说,即使元素位于页面底部,其位置也可以为30
To fix that use 解决该用途

offset().top

Also worth reading: https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect 也值得一读: https//developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect

ISSUE 3 问题3

H3 elements are not supposed to have a name attribute. H3元素不应具有name属性。
Use an ID for that purpose. 为此,请使用ID
Why? 为什么?
if you have a nice web and you have some sexy stuff at the bottom of your page, I can send a link to my friend by referencing your web page and the ID in a link like: 如果您的网站不错,并且页面底部有一些性感的东西,我可以通过在以下链接中引用您的网页和ID将链接发送给我的朋友:

example.com#bottomLady example.com#bottomLady

and he'll get immediately my thoughts without the need to scroll your page all the way down. 他会立即得到我的想法,而无需一直向下滚动页面。

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

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