简体   繁体   English

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

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

I have the following jQuery, its working fine. 我有以下jQuery,它的工作正常。 What I need is when the accordion is clicked to take the user to the top of page of the active accordion. 我需要的是单击手风琴以将用户带到活动手风琴的页面顶部。

Here is the code: 这是代码:

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

    });    
});

Thank you in advance :) 先感谢您 :)

You can use plain javascript to scroll the page programmatically: 您可以使用普通的javascript以编程方式滚动页面:

window.scrollTo(0, 0);

Here's what your code should look like: 这是您的代码应如下所示:

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