轮播图效果

效果

轮播图效果

文件结构

看到效果后仿写手搓一个轮播图。
更改下img里的照片即可,目前轮播图轮播7张照片,如果要做增加或减少轮播图照片数量,需要在index2.html的代码css里增加或减少.slider及其背景图片,在html的div里增加或减少轮播图个数(一个div就是轮播图里一张图片),在js里改动左右两个button按钮对应的方法rightSlide和leftSlide() 里的if条件语句和for循环的参数。
在这里插入图片描述

index.html里的代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5波浪形切换焦点图动画DEMO演示</title>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.parent {
  width: 681px;
  height: 384px;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto auto;
  overflow: hidden;
  position: absolute;
  -webkit-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
}

svg {
  position: absolute;
  z-index: 1;
  width: 681px;
  height: 384px;
}

button {
  position: absolute;
  z-index: 50;
  width: 40px;
  overflow: hidden;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #fff;
  cursor: pointer;
  -webkit-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
}
button:focus {
  outline-width: 0;
}

circle {
  stroke: #fff;
  fill: none;
  transition: 0.3s;
}

#svg1 circle {
  transition-timing-function: linear;
}

#svg2 circle {
  transition-timing-function: linear;
}

#Capa_1 {
  position: absolute;
  width: 16px;
  height: 16px;
  transform: translate(-7px, -8px);
}

#Capa_2 {
  position: absolute;
  width: 16px;
  height: 16px;
  transform: translate(-9px, -8px);
}

.right {
  margin-left: 628px;
  margin-top: 168px;
  border: 1px solid #849494;
  background-color: transparent;
  transition: .5s;
}
.right:hover {
  background-color: #fff;
}

.left {
  margin-left: 0.5%;
  margin-top: 6.17%;
  border: 1px solid #849494;
  background-color: transparent;
  transition: .5s;
}
.left:hover {
  background-color: #fff;
}

.circle1 {
  transition-delay: 0.05s;
}

.circle2 {
  transition-delay: 0.1s;
}

.circle3 {
  transition-delay: 0.15s;
}

.circle4 {
  transition-delay: 0.2s;
}

.circle5 {
  transition-delay: 0.25s;
}

.circle6 {
  transition-delay: 0.3s;
}

.circle7 {
  transition-delay: 0.35s;
}

.circle8 {
  transition-delay: 0.4s;
}

.circle9 {
  transition-delay: 0.45s;
}

.circle10 {
  transition-delay: 0.05s;
}

.circle11 {
  transition-delay: 0.1s;
}

.circle12 {
  transition-delay: 0.15s;
}

.circle13 {
  transition-delay: 0.2s;
}

.circle14 {
  transition-delay: 0.25s;
}

.circle15 {
  transition-delay: 0.3s;
}

.circle16 {
  transition-delay: 0.35s;
}

.circle17 {
  transition-delay: 0.4s;
}

.circle18 {
  transition-delay: 0.45s;
}

.slide1 {
  background-image: url("img/1.jpg");
}

.slide2 {
  background-image: url("img/2.jpg");
}

.slide3 {
  background-image: url("img/3.jpg");
}

.slide4 {
  background-image: url("img/4.jpg");
}

.slider {
  position: absolute;
  width: 400%;
  height: 100%;
  background: #000;
  display: inline-flex;
  overflow: hidden;
}

.slide1,
.slide2,
.slide3,
.slide4 {
  position: absolute;
  background-position: center;
  background-size: cover;
  color: #fff;
  font-size: 62px;
  padding-top: 138px;
  font-weight: 800;
  font-family: 'Heebo', sans-serif;
  text-align: center;
  width: 25%;
  height: 100%;
  z-index: 10;
  transition: 1.4s;
}

.tran {
  transform: scale(1.3);
}

.up1 {
  z-index: 20;
}

.up2 {
  z-index: 40;
}

.steap {
  stroke-width: 0;
}

.streak {
  stroke-width: 82px;
}

@media (max-width: 700px) {
  .parent {
    margin-left: 1%;
  }
}
</style>
</head>
<body>
<div style="text-align:center;clear:both">
</div>
      <iframe frameborder="0" scrolling="no" src="index2.html" width="100%" height="500px"></iframe>
</body>
</html>


index2.htm里的代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>轮播图</title>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/*
调整照片大小的时候需要同时调整
.parent和.svg里的width和height 且两两值相同
*/

.parent {
  width: 60%; 
  height: 520px;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto auto;
  overflow: hidden;
  position: absolute;
  -webkit-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75); /*元素的阴影背景*/
  -moz-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
}

svg {
  position: absolute;
  z-index: 1;
  width: 60%;
  height: 520px;
}

button {
  position: absolute;
  z-index: 50;
  width: 40px;
  overflow: hidden; /*指定如果子元素内容溢出一个父元素的框发生的效果*/
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #fff;
  cursor: pointer; /*鼠标移动到按钮上的状态*/
  -webkit-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
  box-shadow: 0 0 88px 5px rgba(0, 0, 0, 0.75);
}
button:focus {
  outline-width: 0;
}

circle {
  stroke: #fff;
  fill: none;
  transition: 0.3s;
}

#svg1 circle {
  transition-timing-function: linear;
}

#svg2 circle {
  transition-timing-function: linear;
}

#Capa_1 {
  position: absolute;
  width: 16px;
  height: 16px;
  transform: translate(-7px, -8px); /*定义 2D 转换的度数*/
}

#Capa_2 {
  position: absolute;
  width: 16px;
  height: 16px;
  transform: translate(-9px, -8px);
}


.right {
  margin-left: 950px; /*调整右侧按钮button的离div左侧位置*/
  margin-top: 6.17%; /*调整右侧按钮button的离div顶部位置*/
  border: 1px solid #849494;
  background-color: transparent;
  transition: .5s;
}
.right:hover {
  background-color: #fff;
}

.left {
  margin-left: 0.5%; /*调整左侧按钮button离div左侧的位置*/
  margin-top: 6.17%; /*调整左侧按钮button离div顶部的位置*/
  border: 1px solid #849494;
  background-color: transparent;
  transition: .5s;
}
.left:hover {
  background-color: #fff;
}
/*图片切换时的效果*/
.circle1 {
  transition-delay: 0.05s;
}

.circle2 {
  transition-delay: 0.1s;
}

.circle3 {
  transition-delay: 0.15s;
}

.circle4 {
  transition-delay: 0.2s;
}

.circle5 {
  transition-delay: 0.25s;
}

.circle6 {
  transition-delay: 0.3s;
}

.circle7 {
  transition-delay: 0.35s;
}

.circle8 {
  transition-delay: 0.4s;
}

.circle9 {
  transition-delay: 0.45s;
}

.circle10 {
  transition-delay: 0.05s;
}

.circle11 {
  transition-delay: 0.1s;
}

.circle12 {
  transition-delay: 0.15s;
}

.circle13 {
  transition-delay: 0.2s;
}

.circle14 {
  transition-delay: 0.25s;
}

.circle15 {
  transition-delay: 0.3s;
}

.circle16 {
  transition-delay: 0.35s;
}

.circle17 {
  transition-delay: 0.4s;
}

.circle18 {
  transition-delay: 0.45s;
}

.slide1 {
  background-image: url("img/p1.jpg");
}

.slide2 {
  background-image: url("img/p2.jpg");
}

.slide3 {
  background-image: url("img/p3.jpg");
}

.slide4 {
  background-image: url("img/p4.jpg");
}

.slide5 {
  background-image: url("img/p5.jpg");
}

.slide6 {
  background-image: url("img/p6.jpg");
}

.slide7 {
  background-image: url("img/p7.jpg");
}

.slider {
  position: absolute;
  width: 400%;
  height: 100%;
  background: #000;
  display: inline-flex;
  overflow: hidden;
}

/*
slide里带文字
文字的的位置由以下两元素决定
  padding-top: 360px; 
  padding-left: 400px;
*/
.slide1,
.slide2,
.slide3,
.slide4,
.slide5,
.slide6,
.slide7 {
  position: absolute;
  background-position: center;
  background-size: cover;
  color: #fff;
  font-size: 62px;
  padding-top: 360px; 
  padding-left: 530px;
  font-weight: 800;
  font-family: 'Heebo', sans-serif;
  text-align: center;
  width: 25%;
  height: 100%;
  z-index: 10;
  transition: 1.4s;
}

.tran {
  transform: scale(1.3); /*定义 2D 缩放转换*/
}

.up1 {
  z-index: 20;
}

.up2 {
  z-index: 40;
}

.steap {
  stroke-width: 0; /*troke-width 属性定义矩形边框的宽度*/
}

.streak {
  stroke-width: 82px;
}

@media (max-width: 700px) {
  .parent {
    margin-left: 1%;
  }
}
</style>
</head>
<body>
<div class='parent'>
    <div class='slider'>
        <button type="button" id='right' class='right' name="button">

       <svg version="1.1" id="Capa_1" width='40px' height='40px ' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 477.175 477.175" style="enable-background:new 0 0 477.175 477.175;" xml:space="preserve">
       <g>
        <path style='fill: #9d9d9d;' d="M360.731,229.075l-225.1-225.1c-5.3-5.3-13.8-5.3-19.1,0s-5.3,13.8,0,19.1l215.5,215.5l-215.5,215.5
          c-5.3,5.3-5.3,13.8,0,19.1c2.6,2.6,6.1,4,9.5,4c3.4,0,6.9-1.3,9.5-4l225.1-225.1C365.931,242.875,365.931,234.275,360.731,229.075z
          ">
       </g>

       </svg>

       </button>
        <button type="button" id='left' class='left' name="button">
       <svg version="1.1" id="Capa_2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 477.175 477.175" style="enable-background:new 0 0 477.175 477.175;" xml:space="preserve">
       <g>
        <path style='fill: #9d9d9d;' d="M145.188,238.575l215.5-215.5c5.3-5.3,5.3-13.8,0-19.1s-13.8-5.3-19.1,0l-225.1,225.1c-5.3,5.3-5.3,13.8,0,19.1l225.1,225
          c2.6,2.6,6.1,4,9.5,4s6.9-1.3,9.5-4c5.3-5.3,5.3-13.8,0-19.1L145.188,238.575z">
       </g>
       </svg>
       </button>
    
   <svg id='svg2' class='up2' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <circle id='circle1' class='circle1 steap' cx="34px" cy="49%" r="20"  />
      <circle id='circle2' class='circle2 steap' cx="34px" cy="49%" r="100"  />
      <circle id='circle3' class='circle3 steap' cx="34px" cy="49%" r="180"  />
      <circle id='circle4' class='circle4 steap' cx="34px" cy="49%" r="260"  />
      <circle id='circle5' class='circle5 steap' cx="34px" cy="49%" r="340"  />
      <circle id='circle6' class='circle6 steap' cx="34px" cy="49%" r="420"  />
      <circle id='circle7' class='circle7 steap' cx="34px" cy="49%" r="500"  />
      <circle id='circle8' class='circle8 steap' cx="34px" cy="49%" r="580"  />
      <circle id='circle9' class='circle9 steap' cx="34px" cy="49%" r="660"  />
    </svg>
        <svg id='svg1' class='up2' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <circle id='circle10' class='circle10 steap' cx="648px" cy="49%" r="20"  />
      <circle id='circle11' class='circle11 steap' cx="648px" cy="49%" r="100"  />
      <circle id='circle12' class='circle12 steap' cx="648px" cy="49%" r="180"  />
      <circle id='circle13' class='circle13 steap' cx="648px" cy="49%" r="260"  />
      <circle id='circle14' class='circle14 steap' cx="648px" cy="49%" r="340"  />
      <circle id='circle15' class='circle15 steap' cx="648px" cy="49%" r="420"  />
      <circle id='circle16' class='circle16 steap' cx="648px" cy="49%" r="500"  />
      <circle id='circle17' class='circle17 steap' cx="648px" cy="49%" r="580"  />
      <circle id='circle18' class='circle18 steap' cx="648px" cy="49%" r="660"  />
    </svg>

        <div id='slide1' class='slide1 up1'>危机出现</div>
        <div id='slide2' class='slide2'>传播蔓延</div>
        <div id='slide3' class='slide3'>众志成城</div>
        <div id='slide4' class='slide4'>病毒研究</div>
        <div id='slide5' class='slide5'>政府行动</div>
        <div id='slide6' class='slide6'>战疫成果</div>
        <div id='slide7' class='slide7'>冲锋在前</div>
    
      </div>
</div>

<script>
  var curpage = 1; //当前图片下标
  var sliding = false; //是否滑动
  var click = true; //点击切换按钮与否
  var left = document.getElementById('left');
  var right = document.getElementById('right');
  var pagePrefix = 'slide';
  var pageShift = 500;
  var transitionPrefix = 'circle';
  var svg = true;

  //左键按钮
  function leftSlide() {
    if (click) {
      if (curpage == 1) curpage = 8;
        console.log('woek');
        sliding = true;
        curpage--;
        svg = true;
        click = false;

        //控制变换
        for(k=1;k<=7;k++){
          var a1 = document.getElementById(pagePrefix + k);
          a1.className += ' tran';
        }
        setTimeout(()=>{
          move();
        },200);
        setTimeout(()=>{
          for(k=1;k<=7;k++){ //设置变换的效果 给轮播图的dic增加.tran属性
            var a1 = document.getElementById(pagePrefix + k);
            a1.classList.remove('tran');
          };
        },1400);
      }
  }


   function rightSlide() {
    if (click) {
      if (curpage == 7) curpage = 0;
      console.log('woek');
      sliding = true;
      curpage++;
      svg = false;
      click = false;
      for(k=1;k<=7;k++){
        var a1 = document.getElementById(pagePrefix + k);
        a1.className += ' tran';
      }
      setTimeout(()=>{
        move();
      },200);
      setTimeout(()=>{
        for(k=1;k<=7;k++){
          var a1 = document.getElementById(pagePrefix + k);
          a1.classList.remove('tran');
        };
      },1400);
    }
  }


  function move() {
    if (sliding) {
      sliding = false;
      if (svg) {
        for (j = 1; j <= 9; j++) {
          var c = document.getElementById(transitionPrefix + j);
          c.classList.remove("steap");
          c.setAttribute("class", (transitionPrefix + j) + " streak")
          console.log('streak');
        }
      } else {
        for (j = 10; j <= 18; j++) {
          var c = document.getElementById(transitionPrefix + j);
          c.classList.remove("steap");
          c.setAttribute("class", (transitionPrefix + j) + " streak")
          console.log('streak');
        }
      }

      setTimeout(() => {
        for (i = 1; i <= 7; i++) {
          if (i == curpage) {
            var a = document.getElementById(pagePrefix + i);
            a.className += ' up1';  //将当前轮播的图片的div凸显出来
          } else {
            var b = document.getElementById(pagePrefix + i);
            b.classList.remove("up1");
          }
        };
        sliding = true;
      }, 600);
      setTimeout(() => {
        click = true;
      }, 1700);



      setTimeout(() => {
        if (svg) {
          for (j = 1; j <= 9; j++) {
            var c = document.getElementById(transitionPrefix + j);
            c.classList.remove("streak");
            c.setAttribute("class", (transitionPrefix + j) + " steap");
          }
        } else {
          for (j = 10; j <= 18; j++) {
            var c = document.getElementById(transitionPrefix + j);
            c.classList.remove("streak");
            c.setAttribute("class", (transitionPrefix + j) + " steap");
          }
          sliding = true;
        }
      }, 850);
      setTimeout(() => {
        click = true;
      }, 1700);
    }

  }

  left.onmousedown=()=>{
    leftSlide();
  }

  right.onmousedown=()=>{
    rightSlide();
  }

  document.onkeydown=(e)=>{
    if(e.keyCode==37){
      leftSlide();

    }
    else if (e.keyCode==39) {
      rightSlide();

    }
  }

	//for codepen header
	setTimeout(()=>{
		      rightSlide();
	},500)

</script>


</body>
</html>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值