简体   繁体   中英

Jquery scroll to closest element with .class

The problem

I have my html page, i am using a search engine like "Ctrl+F" created by me, i integrated a jquery plugin which highlights me all the result i searched for, and it adds the class ".highlight" to the elements it highlights, the thing is i want to scroll between them , each time i press the search button.

I tried this but it didn't work:

 $(document).ready(function () {

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

Maybe this helps you...

 $(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,

        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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