简体   繁体   English

jQuery(".element").scrollTop(0); 不起作用

[英]jQuery(".element").scrollTop(0); doesn't work

I have multiple tabs like this:我有多个这样的标签:

[tab 1] [tab 2] [标签 1] [标签 2]

--content - 内容

[tab 1] [tab 2] [标签 1] [标签 2]

HTML: HTML:

<div class="tabs">

    <ul class="tab-links">
        <li class="active"><a href="#tab1">Tab #1</a></li>
        <li><a href="#tab2">Tab #2</a></li>
    </ul>  

    <div class="tab-content">

        <div id="tab1" class="tab active">#1 content</div>

        <div id="tab2" class="tab">#2 content </div> 

    </div>

    <div class="tabs">

        <ul class="tab-links">
            <li class="active"><a href="#tab1">Tab #1</a></li>
            <li><a href="#tab2">Tab #2</a></li>
        </ul>

    </div>

</div>

As you can see there are tab list above and below the tab-content.如您所见,选项卡内容的上方和下方都有选项卡列表。

As long as content is long, user has to scroll-up to page when he change the tab on the tab-list-bottom.只要内容很长,用户在更改选项卡列表底部的选项卡时必须向上滚动到页面。

So, I wanted to scrollTop automatically when user change the tab from below.因此,当用户从下方更改选项卡时,我想自动滚动顶部。 And I added jQuery(".tab-content").scrollTop(0);我添加了jQuery(".tab-content").scrollTop(0); into the java-script code.进入java脚本代码。 But somehow It doesn't respond.但不知何故它没有响应。

jsFiddle: js小提琴:

http://jsfiddle.net/4m2ut16k/8/ http://jsfiddle.net/4m2ut16k/8/

Note: It works when I change jQuery(".tab-content").scrollTop(0);注意:当我更改jQuery(".tab-content").scrollTop(0);时它有效jQuery(".tab-content").scrollTop(0); to jQuery(window).scrollTop(0);jQuery(window).scrollTop(0); but this is not what I want because my content not on the top of page.但这不是我想要的,因为我的内容不在页面顶部。

Just can't understand why it doesn't work on the first place.只是不明白为什么它首先不起作用。 Has anyone figure out what is the problem?有没有人弄清楚是什么问题?

您必须将窗口滚动到选项卡容器的顶部,您可以使用 jquery offset 函数获取.tab-content元素的偏移量,然后获取 top 属性:

jQuery(window).scrollTop(jQuery(".tab-content").offset().top);

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

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