zoukankan      html  css  js  c++  java
  • 层叠样式的轮播效果

    静态效果图如下:

    1、定义静态样式:使用z-index来实现层级

    2、将图片的类放在一个数组中,每次通过改变数组的排列来实现图片的变化

    3、进入页面就要开始定时器;鼠标移入mouseover,清除定时器clearInterval;鼠标移出mouseleave,开始定时器setInterval

    HTML:

    <div class="display-left">
    <ul class="img-gather">
    <li class="p5"><img src="image/1.png" alt=""></li>
    <li class="p4"><img src="image/2.png" alt=""></li>
    <li class="p3"><img src="image/3.png" alt=""></li>
    <li class="p2"><img src="image/4.png" alt=""></li>
    <li class="p1"><img src="image/5.png" alt=""></li>
    </ul>
    <div class="arr">
    <img src="image/ljt.png" class="prev" alt="上一张">
    <img src="image/rjt.png" class="next" alt="下一张">
    </div>
    </div>
    CSS:
    body{
    background-color: #aaa;
    }
    .display-left{
    position: relative;
    display: inline-block;
    vertical-align: top;
    794px;
    height: 404px;
    margin-left: 348px;
    margin-top: 93px;
    margin-bottom: 74px;
    }
    .display-left>img{
    17px;
    height: 32px;
    }
    .img-gather>li{
    float: left;
    }
    .display-left .arr img:nth-child(1){
    position: absolute;
    top: 186px;
    left: 0;
    }
    .display-left .arr img:nth-child(2){
    position: absolute;
    top: 186px;
    right: 0;
    }
    .img-gather{
    display: inline-block;
    620px;
    height: 100%;
    margin-left: 74px;
    margin-right: 66px;
    list-style: none;
    }
    .img-gather .p5>img{
    196px;
    height: 404px;
    position: absolute;
    z-index: 100;
    top: 0;
    left: 91px;
    }
    .img-gather .p4>img{
    181px;
    height: 371px;
    position: absolute;
    z-index: 90;
    top: 16px;
    left: 240px;
    }
    .img-gather .p3>img{
    161px;
    height: 337px;
    position: absolute;
    z-index: 80;
    top: 33px;
    left: 372px;
    }
    .img-gather .p2>img{
    120px;
    height: 306px;
    position: absolute;
    z-index: 70;
    top: 49px;
    left: 507px;
    }
    .img-gather .p1>img{
    131px;
    height: 280px;
    position: absolute;
    z-index: 60;
    top: 62px;
    left: 580px;
    }
    JS:
    var cArr = ["p5","p4","p3","p2","p1"];
    var index = 0;
    $(".next").click(function () {
    nextImg();
    });
    $(".prev").click(function () {
    prevImg();
    });
    function nextImg() {
    cArr.unshift(cArr[4]); // 向开头添加p1,unshift返回新的数组长度
    cArr.pop(); // 把最后的p1删掉,最终数组是【p1,p5,p4,p3,p2】
    $(".img-gather li").each(function (i, e) {
    $(e).removeClass().addClass(cArr[i]);
    })
    index++;
    if (index>4){
    index = 0;
    }
    // show();
    }
    function prevImg() {
    cArr.push(cArr[0]); // 向数组末尾添加p5
    cArr.shift(); // 把开头的p5删掉,最终数组是【p4,p3,p2,p1,p5】
    $(".img-gather li").each(function (i, e) {
    $(e).removeClass().addClass(cArr[i]);
    })
    index--;
    if(index<0){
    index = 4;
    }
    // show();
    }
     
    // 点击图片能切换的功能
    // 点击class为p3的图片
    $(document).on("click",".p3",function(){
    nextImg();
    });
    // 点击class为p5的图片
    $(document).on("click",".p5",function(){
    prevImg();
    });
    // 自动播放功能
    // 鼠标移入box时清除定时器
    $(".content").mouseover(function(){
    clearInterval(timer);
    })
    // 鼠标移出box时开始定时器
    $(".content").mouseleave(function(){
    timer=setInterval(nextImg,2000);
    })
    // 进入页面自动开始定时器
    timer=setInterval(nextImg,2000);
  • 相关阅读:
    20191024-6 Alpha发布用户使用报告
    【第三章】MySQL数据库的字段约束:数据完整性、主键、外键、非空、默认值、自增、唯一性
    【第六章】MySQL日志文件管理
    【第四章】MySQL数据库的基本操作:数据库、表的创建插入查看
    【第一章】MySQL数据概述
    【Linux运维】LNMP环境配置
    【Linux 运维】linux系统修改主机名
    【Linux 运维】linux系统查看版本信息
    【Linux 运维】Centos7初始化网络配置
    50、树中两个节点的公共祖先
  • 原文地址:https://www.cnblogs.com/prospective-zkq/p/10248417.html
Copyright © 2011-2022 走看看