jquery datatables scroll to top when pages clicked from bottom

后端 未结 7 637
余生分开走
余生分开走 2021-01-02 17:13

I am using a jQuery datatable with bottom pagination. When the pages are clicked from bottom , I want it to scroll it to top, so users do not have to manually do that for lo

7条回答
  •  一个人的身影
    2021-01-02 17:42

    Thank's from davidkonrad. But when I used his code, after I clicked on paginate button, scroll of page went to top and then after load data, scroll backed to bottom.

    I combined multiple posts in StackOverFlow and Datatables forum.

    I defined a global variable :

    var isScroll = false    
    

    When it's clicked on paginate button isScroll set to true:

    $(document).on('click', '.paginate_button:not(.disabled)', function () {
        isScroll = true;
    });
    

    And finally:

    $(document).ready(function () {
        $('#myDataTable').DataTable({
            ...
            "fnDrawCallback": function () {
                if (isScroll) {
                    $('html, body').animate({
                        scrollTop: $(".dataTables_wrapper").offset().top
                    }, 500);
                    isScroll = false;
                }
            },
            ...
        });
    });
    

    Thanks from @0110

提交回复
热议问题