zoukankan      html  css  js  c++  java
  • 两种轮播图实现方式

    一、css实现轮播图

    原文网址:https://www.jianshu.com/p/550c11f3b731

    实现逻辑:

    1)将所有的轮播图片放在一个容器里面,并排排列;

    2)编写css动画事件:每隔一定时间向左偏移一定距离,距离为一个轮播图宽度;到最后一个轮播图后切换到第一个图片,实现无限循环

    优点:

    实现逻辑简单,可以直接拿来用

    缺点:

    轮播图数量固定,如果要增删,需要修改代码;不是一个顺序的无限循环,到达最后一个轮播图后,会有一个快速倒退的动画,效果不是太好

    主要实现代码:

    <!-- 点击轮播 -->
    <div class="slide" style="margin-top: 80px;">

    <input type="radio" name="sildeInput" value="0" id="sildeInput1" checked hidden />
    <label for="sildeInput1">1</label>

    <input type="radio" name="sildeInput" value="1" id="sildeInput2" hidden />
    <label for="sildeInput2">2</label>

    <input type="radio" name="sildeInput" value="1" id="sildeInput3" hidden />
    <label for="sildeInput3">3</label>

    <ul>
    <li>one-点击切换</li>
    <li>two-点击切换</li>
    <li>three-点击切换</li>
    </ul>
    </div>
    .slide {
    position: relative;
    margin: auto;
    600px;
    height: 200px;
    text-align: center;
    font-family: Arial;
    color: #FFF;
    overflow: hidden;
    }

    .slide ul {
    margin: 10px 0;
    padding: 0;
    calc(600px * 3);
    transition: all 0.5s;
    }

    .slide li {
    float: left;
    600px;
    height: 200px;
    list-style: none;
    line-height: 200px;
    font-size: 36px;
    }

    .slide li:nth-child(1) {
    background: #9fa8ef;
    }

    .slide li:nth-child(2) {
    background: #ef9fb1;
    }

    .slide li:nth-child(3) {
    background: #9fefc3;
    }

    .slide input[name="sildeInput"] {
    display: none;
    }

    .slide label[for^="sildeInput"] {
    position: absolute;
    top: 170px;
    20px;
    height: 20px;
    margin: 0 10px;
    line-height: 20px;
    color: #FFF;
    background: green;
    cursor: pointer;
    border-radius: 50%;
    }

    .slide label[for="sildeInput1"] {
    left: 0;
    }

    .slide label[for="sildeInput2"] {
    left: 30px;
    }

    .slide label[for="sildeInput3"] {
    left: 60px;
    }

    #sildeInput1:checked~ul {
    margin-left: 0;
    }

    #sildeInput2:checked~ul {
    margin-left: -600px;
    }

    #sildeInput3:checked~ul {
    margin-left: -1200px;
    }

    #sildeInput1:checked~label[for="sildeInput1"] {
    color: #000;
    background: #fff;
    }

    #sildeInput2:checked~label[for="sildeInput2"] {
    color: #000;
    background: #fff;
    }

    #sildeInput3:checked~label[for="sildeInput3"] {
    color: #000;
    background: #fff;
    }

    https://www.98891.com/article-35-1.html

    二、swiper插件实现方式

    文档网址:https://www.swiper.com.cn/usage/index.html

    实现方法:

    1)下载插件文件:swiper-bundle.min.js和swiper-bundle.min.css,下载地址:https://www.swiper.com.cn/download/index.html#file1,下载文件后解压,在swiper-masterpackage目录下有这两个文件;

    2)引入插件文件:

    <!DOCTYPE html>
    <html>
    <head>
    ...
    <link rel="stylesheet" href="dist/css/swiper-bundle.min.css">
    </head>
    <body>
    ...
    <script src="dist/js/swiper-bundle.min.js"></script>
    ...
    </body>
    </html>

    3)编写轮播html页面:

    <div >
    <div >
    <div >Slide 1</div>
    <div >Slide 2</div>
    <div >Slide 3</div>
    </div>
    <!-- 如果需要分页器 -->
    <div ></div>

    <!-- 如果需要导航按钮 -->
    <div ></div>
    <div ></div>

    <!-- 如果需要滚动条 -->
    <div ></div>
    </div>

    4)JS编写代码启动轮播图效果

    var mySwiper = new Swiper ('.swiper-container', {
    direction: 'vertical', // 垂直切换选项
    loop: true, // 循环模式选项

    // 如果需要分页器
    pagination: {
    el: '.swiper-pagination',
    },

    // 如果需要前进后退按钮
    navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
    },

    // 如果需要滚动条
    scrollbar: {
    el: '.swiper-scrollbar',
    },
    })

    优点:

    对轮播图数量没有限制,增删轮播图不需要修改代码;

    功能可配置,自由增删轮播功能,api文档地址:https://www.swiper.com.cn/api/index.html

    可以外部控制轮播图,详细可见api文档中的Methods(Swiper方法)

    提供import引入方式,可应用在vue和react中。

  • 相关阅读:
    子串周期查询问题的相关算法及其应用(原文为2019年国家集训队论文集)
    微软最有价值专家 Azure DevOps MVP(第六年)
    当一个程序员一天被打扰 10 次, 后果很惊人
    什么是CAP定理?
    Java中的锁原理、锁优化、CAS、AQS详解
    如何停止一个正在运行的线程?
    lammps总结(7.27-7.30)
    packmol建模
    Linux 命令 (1)
    ElementUI中的el-select中多选回显数据后没法重新选择和更改
  • 原文地址:https://www.cnblogs.com/qianxiaox/p/14932560.html
Copyright © 2011-2022 走看看