簡體   English   中英

jQuery使用.class滾動到最接近的元素

[英]Jquery scroll to closest element with .class

問題

我有我的html頁面,我正在使用由我創建的搜索引擎,例如“ Ctrl + F”,我集成了一個jquery插件,該插件突出顯示了我搜索到的所有結果,並將類“ .highlight”添加到了它的元素中突出顯示,每當我按搜索按鈕時,我想在它們之間滾動。

我試過了,但是沒有用:

 $(document).ready(function () {

        $("#btnSearch").on("click", function () {
            $('html, body').animate({
                'scrollTop': $(this).closest(".highlight").position().top
            });
        });
    });

也許這可以幫助您...

 $(document).ready(function () {

    $("#btnSearch").on("click", function () {
        $('html, body').animate({
            'scrollTop': $(this).closest(".highlight").parent().scrollTop()+ $(this).closest(".highlight").offset().top - $(this).closest(".highlight").parent().offset().top,

        });
    });
});

暫無
暫無

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

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