简体   繁体   English

退出视口时是否停止滑块滚动?

[英]Stop slider from scrolling when out of viewport?

Sample page: http://550.9f2.myftpupload.com/speaking-engagements/ 样本页面:http: //550.9f2.myftpupload.com/speaking-engagements/

Built with Wordpress (Visual Composer). 使用Wordpress(Visual Composer)构建。

Near the middle of the page you'll see a yellow slider with quotes, which automatically scrolls between each slide. 在页面中间附近,您会看到一个带有引号的黄色滑块,该滑块会在每张幻灯片之间自动滚动。 Because the size of each slide changes dependent on the amount of text, I need the automatic sliding to stop once the user scrolls past it. 由于每张幻灯片的大小都会随文本量的变化而变化,因此我需要在用户滚动经过后自动停止滑动。 Otherwise the content below jumps up and down as the slider goes through different slides. 否则,下面的内容会随着滑块浏览不同的幻灯片而上下跳动。

My research online tells me this should be done with Javascript/jQuery? 我的在线研究告诉我,这应该使用Javascript / jQuery完成吗? Which I'm not familiar with at all, does anyone have any tips for how a novice can implement this? 我一点都不熟悉,对于新手如何实现此功能,有没有人有任何提示?

There are two things you should take care of: 您应该注意两件事:

1) detecting wether slider is visible for user. 1)检测是否有滑动条对用户可见。 There are several solutions for this, for example this or this 有几种解决方案,例如thisthis

2) stopping/starting slider depending on slider visibility. 2)根据滑块的可见性停止/启动滑块。 Combining it all together, the code would look like this. 将所有内容组合在一起,代码将如下所示。 It is conceptual and untested, but the idea is clear, I think. 我认为这是概念性的,未经测试,但想法很明确。

jQuery(window).scroll(function($) {

    function isScrolledIntoView(elem)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();

        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }


    if (isScrolledIntoView($('.rd_testimonials'))){
        $('.rd_testimonials').carouFredSel({auto: false});
    } else {
        $('.rd_testimonials').carouFredSel({auto: true});
    }

});

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

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