Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击手风琴时如何滚动到页面顶部?_Javascript_Jquery_Accordion - Fatal编程技术网

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

Javascript 单击手风琴时如何滚动到页面顶部?,javascript,jquery,accordion,Javascript,Jquery,Accordion,我有以下jQuery,它工作正常。我需要的是当点击手风琴时,将用户带到活动手风琴页面的顶部 代码如下: jQuery(document).ready(function($){ $('.accordion-container').hide(); //$('.accordion-toggle:first').addClass('active').next().show(); $('.accordion-toggle').click(function(){

我有以下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

    });    

});

谢谢你的回答,你能告诉我在代码中我在哪里添加窗口吗@user3019325我添加了更多代码。我不是100%确定你想怎么做,所以只要玩一下,你很快就会明白。再次感谢你,它成功了,但现在它把我带到了整个页面的顶部。我只想让它把我带到活动手风琴的顶部。然后在你的问题中这样说。您明确表示希望它将用户带到“页面顶部”。进行一些搜索,学习如何检索手风琴顶部的Y坐标,并在
窗口中使用它。滚动到(0,yCoordinate)。我在问题中确实说过。无论如何,谢谢你抽出时间:)