有啥方法可以停止引导轮播幻灯片的自动播放?

Posted

技术标签:

【中文标题】有啥方法可以停止引导轮播幻灯片的自动播放?【英文标题】:Is there any way to stop auto-playing of bootstrap carousel slide?有什么方法可以停止引导轮播幻灯片的自动播放? 【发布时间】:2021-07-02 02:13:02 【问题描述】:

我正在使用 Bootstrap 轮播制作轮播幻灯片。我想停止幻灯片的自动播放,我想制作一个按钮,用于控制自动播放的开/关。

我已经搜索过它,它建议我为轮播div 添加属性data-interval=false 或添加脚本:$('.carousel').carousel( interval: false, )

当上述值设置为默认值时,它会停止自动播放,但是当我想打开/关闭自动播放时,它不起作用并继续自动播放。

这是我目前的脚本:

var total = $('.carousel-item').length;
var currentIndex = $('div.active').index() + 1;
$('.current_slide').html(currentIndex + ' / ' + total);

$('.carousel').on('slid.bs.carousel', function() 
currentIndex = $('div.active').index() + 1;

var text = currentIndex + ' / ' + total;
$('.current_slide').html(text);
);

function navAutoplay() 
    if ($('.if_paused').hasClass('carousel_now')) 
        $('*.carousel_now').removeClass('carousel_now');
        $('.if_started').addClass('carousel_now');
    

    else     
        $('*.carousel_now').removeClass('carousel_now');
        $('.if_paused').addClass('carousel_now');
    


$(document).ready(function() 
    if ($('.if_started').hasClass('carousel_now')) 
        $('.carousel').carousel(
            interval: 7000
        );
    

    else 
        $('.carousel').carousel(
            interval: false
        );
    
)    

html代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="carouselExampleFade" data-interval="7000" class="carousel slide carousel-fade" data-ride="carousel">
    <div class="carousel-inner">
    <div class="carousel-item active">
        <img src="https://p.bigstockphoto.com/GeFvQkBbSLaMdpKXF1Zv_bigstock-Aerial-View-Of-Blue-Lakes-And--227291596.jpg" class="d-block w-100" >
    </div>
    <div class="carousel-item">
        <img src="https://p.bigstockphoto.com/GeFvQkBbSLaMdpKXF1Zv_bigstock-Aerial-View-Of-Blue-Lakes-And--227291596.jpg" class="d-block w-100" >
    </div>
    </div>
    <div class="carousel_nav">
        <div class="current_slide">

        </div>
        <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <div class="corousel_nav_autoplay">
            <a class="if_paused">
                <span onclick="navAutoplay()"></span>
            </a>
            <a class="if_started carousel_now">
                <span onclick="navAutoplay()"></span>
            </a>
        </div>
        <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
    </div>

    <div id="carouselExampleFade" data-interval="7000" class="carousel slide carousel-fade" data-ride="carousel">
    <div class="carousel-inner">
    <div class="carousel-item active">
    <img src="img/slide01_01.png" class="d-block w-100" >
    </div>
    <div class="carousel-item">
    <img src="img/slide02_01.png" class="d-block w-100" >
    </div>
    </div>
    <div class="carousel_nav">
    <div class="current_slide">

    </div>
    <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <div class="corousel_nav_autoplay">
        <a class="if_paused">
            <span onclick="navAutoplay()"></span>
        </a>
        <a class="if_started carousel_now">
            <span onclick="navAutoplay()"></span>
        </a>
    </div>
    <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
    </div>
    </div>
    <script>
    var total = $('.carousel-item').length;
    var currentIndex = $('div.active').index() + 1;
    $('.current_slide').html(currentIndex + '&nbsp;/&nbsp;' + total);

    $('.carousel').on('slid.bs.carousel', function() 
    currentIndex = $('div.active').index() + 1;

    var text = currentIndex + '&nbsp;/&nbsp;' + total;
    $('.current_slide').html(text);
    );
    </script>
    <script>
    function navAutoplay() 
    if ($('.if_paused').hasClass('carousel_now')) 
        $('*.carousel_now').removeClass('carousel_now');
        $('.if_started').addClass('carousel_now');
    

    else     
        $('*.carousel_now').removeClass('carousel_now');
        $('.if_paused').addClass('carousel_now');
    
    

    $(document).ready(function() 
    if ($('.if_started').hasClass('carousel_now')) 
        $('.carousel').carousel(
            interval: 7000
        );
    

    else 
        $('.carousel').carousel(
            interval: false
        );
    
)
</script>

【问题讨论】:

【参考方案1】:

您好,根据文档,您应该能够使用这些方法切换轮播:

.carousel('cycle')
// to enable cycling

.carousel('pause')
// to stop cycling thru items

我认为,如果您使用选项对象调用 .carusel(),它会再次对其进行初始化,并且可能无法按您的预期工作

【讨论】:

【参考方案2】:

您只需要从 bootstrap 中阅读文档,那里有文档记录。

这是一个关于如何实现它的示例。

function navAutoplay() 
  const isAutoPlayEnabled = $('#toggle-auto-play').text() === 'Pause';

  if (isAutoPlayEnabled) 
    // Pause carousel if autoplay is enabled
    $('#carouselExampleIndicators').carousel('pause');
    $('#toggle-auto-play').text('Auto Play');
   else 
    // Enable Auto Play if it's paused
    $('#carouselExampleIndicators').carousel(
      interval: 500,  // Your choice of interval
    );
    $('#toggle-auto-play').text('Pause');
  
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link
            rel="stylesheet"
            href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
            integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
            crossorigin="anonymous"
        />
        <script
            src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"
        ></script>
        <script
            src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"
        ></script>
        <script
            src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"
        ></script>
    </head>
    <body>
        <div
            id="carouselExampleIndicators"
            class="carousel slide"
            data-ride="carousel"
        >
            <ol class="carousel-indicators">
                <li
                    data-target="#carouselExampleIndicators"
                    data-slide-to="0"
                    class="active"
                ></li>
                <li
                    data-target="#carouselExampleIndicators"
                    data-slide-to="1"
                    class=""
                ></li>
                <li
                    data-target="#carouselExampleIndicators"
                    data-slide-to="2"
                    class=""
                ></li>
            </ol>
            <div class="carousel-inner">
                <div class="carousel-item active">
                    <img
                        class="d-block w-100"
                        data-src="holder.js/800x400?auto=yes&amp;bg=777&amp;fg=555&amp;text=First slide"
                        
                        src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_178a69a8120%20text%20%7B%20fill%3A%23555%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_178a69a8120%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22285.9140625%22%20y%3D%22218.3%22%3EFirst%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
                        data-holder-rendered="true"
                    />
                </div>
                <div class="carousel-item">
                    <img
                        class="d-block w-100"
                        data-src="holder.js/800x400?auto=yes&amp;bg=666&amp;fg=444&amp;text=Second slide"
                        
                        src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_178a69a8120%20text%20%7B%20fill%3A%23444%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_178a69a8120%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23666%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22247.3125%22%20y%3D%22218.3%22%3ESecond%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
                        data-holder-rendered="true"
                    />
                </div>
                <div class="carousel-item">
                    <img
                        class="d-block w-100"
                        data-src="holder.js/800x400?auto=yes&amp;bg=555&amp;fg=333&amp;text=Third slide"
                        
                        src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22800%22%20height%3D%22400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20800%20400%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_178a69a8120%20text%20%7B%20fill%3A%23333%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A40pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_178a69a8120%22%3E%3Crect%20width%3D%22800%22%20height%3D%22400%22%20fill%3D%22%23555%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22276.9921875%22%20y%3D%22218.3%22%3EThird%20slide%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
                        data-holder-rendered="true"
                    />
                </div>
            </div>
            <a
                class="carousel-control-prev"
                href="#carouselExampleIndicators"
                role="button"
                data-slide="prev"
            >
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a
                class="carousel-control-next"
                href="#carouselExampleIndicators"
                role="button"
                data-slide="next"
            >
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>

        <!-- Toggle button -->
        <div>
            <button id="toggle-auto-play" class="btn btn-primary" onclick="navAutoplay()">Pause</button>
        </div>
    </body>
</html>

【讨论】:

【参考方案3】:

我已经尝试过 .carousel('pause');和 .carousel('cycle');切换自动播放,但它不起作用。

function navAutoplay() 
            if ($('.if_paused').hasClass('carousel_now')) 
                $('*.carousel_now').removeClass('carousel_now');
                $('.if_started').addClass('carousel_now');
                $('.carousel').carousel('cycle');
            

            else     
                $('*.carousel_now').removeClass('carousel_now');
                $('.if_paused').addClass('carousel_now');
                $('.carousel').carousel('pause');
            
        

【讨论】:

以上是关于有啥方法可以停止引导轮播幻灯片的自动播放?的主要内容,如果未能解决你的问题,请参考以下文章

引导轮播 - 滑动时暂停 html 视频

带有 Javascript 功能的引导轮播播放和暂停

带有播放/暂停按钮的多个引导轮播

防止 twitter 引导轮播在页面加载时自动滑动

修改引导轮播以同时显示多张幻灯片

在引导轮播中使用图像而不是幻灯片背景