簡體   English   中英

將滾動到所選項目的頂部

[英]Move scroll to top with the selected item

在包含多個元素的ul中,我想滾動條以查看始終位於頂部的所選項目。 此項將分配selected類。 到目前為止,我已經設法將欄滾動到此元素的位置,但沒有將其滾動到容器的頂部。

我的代碼是:

jQuery(document).ready(function(){
    jQuery('#container').animate({
        scrollTop: jQuery('li.selected').offset().top
    }, 2000);
});

任何的想法?

像這樣遍歷到parent元素:

jQuery(document).ready(function(){
    var x = jQuery('li.selected').parent();
    jQuery('#container').animate({
        scrollTop: jQuery(x).offset().top
    }, 2000);
});

我在類似的情況下使用了它:無法直接在scrollTop ... 行中遍歷,因此我使用了一個變量來選擇父級並將此變量放入scrollTop行中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM