繁体   English   中英

单击手风琴时如何滚动到页面顶部?

[英]How to scroll to top of page when accordion is clicked?

我有以下jQuery,它的工作正常。 我需要的是单击手风琴以将用户带到活动手风琴的页面顶部。

这是代码:

jQuery(document).ready(function($){

    $('.accordion-container').hide();

    //$('.accordion-toggle:first').addClass('active').next().show();

    $('.accordion-toggle').click(function(){
        if( $(this).next().not(':hidden') ) {
            $('.accordion-toggle').removeClass('active').next().slideUp();

        }
        if( $(this).next().is(':hidden') ) {

            $('.accordion-toggle').removeClass('active').next().slideUp();

            $(this).toggleClass('active').next().slideDown();

        }

        return false; // Prevent the browser jump to the link anchor

    });    
});

先感谢您 :)

您可以使用普通的javascript以编程方式滚动页面:

window.scrollTo(0, 0);

这是您的代码应如下所示:

jQuery(document).ready(function($){

    $('.accordion-container').hide();

    //$('.accordion-toggle:first').addClass('active').next().show();

    $('.accordion-toggle').click(function(){

        if( $(this).next().not(':hidden') ) {
            $('.accordion-toggle').removeClass('active').next().slideUp();

        }

        if( $(this).next().is(':hidden') ) {

            $('.accordion-toggle').removeClass('active').next().slideUp();

            $(this).toggleClass('active').next().slideDown();

        }

        //scroll to top
        window.scrollTo(0, 0);

        return false; // Prevent the browser jump to the link anchor

    });    

});

暂无
暂无

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

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