简体   繁体   English

jQuery手风琴-使Clicked元素滚动到页面顶部

[英]Jquery Accordion - Make Clicked element scroll to top of page

I am using this accordion script for an FAQ page: http://www.snyderplace.com/demos/accordion.html 我正在此手风琴脚本用于FAQ页面: http : //www.snyderplace.com/demos/accordion.html

It's great except for one problem which is especially evident on mobile devices. 除了一个在移动设备上特别明显的问题,它很棒。 When you click on a question and it has a lot of content inside, it expands upwards off the screen, to where you have to scroll up to see the question and the beginning of the content. 当您单击问题并在其中包含很多内容时,它会向上扩展至屏幕之外,您必须向上滚动才能看到问题和内容的开头。

Ideally I'd like to have it to where the script scrolls the question to the top of the page/viewport when you click on it. 理想情况下,我希望将其放到脚本上,当您单击它时,它将问题滚动到页面/视口的顶部。 If anyone has an idea of what to tweak in the script that would be amazing! 如果有人对脚本中的内容进行了调整,那就太好了!

You may try something like this. 您可以尝试类似的方法。 You don't need a plugin for an accordion: 您不需要手风琴的插件:

Edited version with icons, default open, and touch enabled scroll to top 带图标的编辑版本,默认打开,并触摸启用滚动到顶部

https://jsfiddle.net/07fdq3t1/10/ https://jsfiddle.net/07fdq3t1/10/

Add the class show to the one you want to open. 将课程显示添加到您要打开的课程中。

This could probably be written more efficiently as there's repeating code, but it should work. 由于存在重复代码,这可能会更有效地编写,但是应该可以。

jQuery jQuery的

$(document).ready(function() {
    $('.accordion').click(function(){
        if($(this).next('.container').is(':visible')) {
            $(this).removeClass('show');
            $(this).next('.container').slideUp();
        }
        else {
            $('.accordion').find('.container:visible').slideUp();
            $('.accordion').removeClass('show');
            $(this).addClass('show');
            $(this).next('.container').slideDown();
        }         
    });
    $('.accordion').on( "touchstart", function(){
        if($(this).next('.container').is(':visible')) {
            $(this).removeClass('show');
            $(this).next('.container').slideUp();
        }
        else {
            $('.accordion').find('.container:visible').slideUp();
            $('.accordion').removeClass('show');
            $(this).addClass('show');
            $(this).next('.container').slideDown();
            $('html, body').animate({
                scrollTop: $(this).offset().top
            }, 200);
        } 
    });
});

HTML 的HTML

<div class="accordion">Heading<span></span></div>
    <div class="container">
        <div class="content">
            <div>Sample Content</div>
            <p>Content here....</p>
        </div>
    </div>
<div class="accordion">Heading<span></span></div>
    <div class="container">
        <div class="content">
            <div>Sample Content</div>
            <p>Content here....</p>
        </div>
    </div>
<div class="accordion show">Heading<span></span></div>
    <div class="container">
        <div class="content">
            <div>Sample Content</div>
            <p>Content here....</p>
        </div>
    </div>
<div class="accordion">Heading<span></span></div>
    <div class="container">
        <div class="content">
            <div>Sample Content</div>
            <p>Content here....</p>
        </div>
    </div>

Note you will need to include jQuery in your code. 注意,您将需要在代码中包含jQuery。

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

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