超实用的JavaScript代码段 Item3 --图片轮播效果

图片轮播效果

图片尺寸 统一设置成:490*170px;

一、页面加载、获取整个容器、所有放数字索引的li及放图片列表的ul、定义放定时器的变量、存放当前索引的变量index

二、添加定时器,每隔2秒钟index递增一次、调用一次切换图片函数

提示:

1、 index不能一直无限制的递增下去,需做判断

2、调用切换图片函数时需将递增之后的index作为参数传过去

三、定义图片切换函数

提示:

  1.遍历所有放数字索引的li,将每个li上的类去掉。
  2.根据传递过来的index值找到对应的li给它添加类设为当前高亮显示。
  3. 根据传递过来的index值计算放图片的ul的top值
  4. 改变index的值,让其等于传递过来的参数值

注意:放图片的ul的top值=-index*单张图片的高度(所有图片必须等高)

四、鼠标划过整个容器时,图片停止切换,离开继续

提示:

1.  鼠标滑过整个容器时清除定时器
2.  鼠标离开时继续执行定时器,切换至下一张图片
3. 

五、遍历所有放数字的li,且给他们添加索引、鼠标滑过时切换至对应的图片

  1. 鼠标滑过时调用图片切换函数,将滑过的li的索引传过去
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
  *{margin:0;
    padding:0;
    list-style:none;}
  .wrap{height:170px;
        width:490px;
        margin:60px auto;
        overflow: hidden;
        position: relative;
        margin:100px auto;}
  .wrap ul{position:absolute;} 
  .wrap ul li{height:170px;}
  .wrap ol{position:absolute;
           right:5px;
           bottom:10px;}
  .wrap ol li{height:20px; width: 20px;
              background:#ccc;
              border:solid 1px #666;
              margin-left:5px;
              color:#000;
              float:left;
              line-height:center;
              text-align:center;
              cursor:pointer;}
  .wrap ol .on{background:#E97305;
               color:#fff;}

  </style>
  <script type="text/javascript">
  window.onload=function(){
    var wrap=document.getElementById('wrap'),
        pic=document.getElementById('pic').getElementsByTagName("li"),
        list=document.getElementById('list').getElementsByTagName('li'),
        index=0,
        timer=null;

      // 定义并调用自动播放函数
    timer = setInterval(autoPlay, 2000);

     // 鼠标划过整个容器时停止自动播放
    wrap.onmouseover = function () {
      clearInterval(timer);
    }

     // 鼠标离开整个容器时继续播放至下一张
    wrap.onmouseout = function () {
      timer = setInterval(autoPlay, 2000);
    }
     // 遍历所有数字导航实现划过切换至对应的图片
    for (var i = 0; i < list.length; i++) {
      list[i].onmouseover = function () {
        clearInterval(timer);
        index = this.innerText - 1;
        changePic(index);
      };
    };

    function autoPlay () {
      if (++index >= pic.length) index = 0;
      changePic(index);
    }

      // 定义图片切换函数
    function changePic (curIndex) {
      for (var i = 0; i < pic.length; ++i) {
        pic[i].style.display = "none";
        list[i].className = "";
      }
      pic[curIndex].style.display = "block";
      list[curIndex].className = "on";
    }

   };

  </script> 
</head>
<body>
  <div class="wrap" id='wrap'>
    <ul id="pic">
      <li><img src="1.jpg" alt=""></li>
      <li><img src="2.jpg" alt=""></li>
      <li><img src="3.jpg" alt=""></li>
      <li><img src="4.jpg" alt=""></li>
      <li><img src="5.jpg" alt=""></li>    
    </ul>
    <ol id="list">
      <li class="on">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ol>
  </div>
</body>
</html>

实例效果:
这里写图片描述

这里写图片描述

版权声明:本文为小平果原创文章,转载请注明:http://blog.csdn.net/i10630226

转载于:https://www.cnblogs.com/dingxiaoyue/p/4948181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这样的效果,需要使用Vue.js和一些CSS和JavaScript技巧。以下是一种实现方式: 1. 首先,创建一个Vue组件,包含一个3D轮播图和一些CSS样式,用于让轮播图旋转和显示。 ``` <template> <div class="carousel-container"> <div class="carousel"> <div class="slide" v-for="(item, index) in items" :key="index"> <img :src="item.image" alt="slide" class="slide-image"> <div class="slide-content"> <h3 class="slide-title">{{ item.title }}</h3> <p class="slide-text">{{ item.text }}</p> </div> </div> </div> </div> </template> <script> export default { data() { return { items: [ { image: "https://picsum.photos/400/300", title: "Slide 1", text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." }, { image: "https://picsum.photos/400/300", title: "Slide 2", text: "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }, { image: "https://picsum.photos/400/300", title: "Slide 3", text: "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." } ] }; } }; </script> <style scoped> .carousel-container { position: relative; width: 100%; height: 400px; overflow: hidden; } .carousel { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 80%; height: 80%; transform-style: preserve-3d; transform: translateZ(-1000px); animation: rotate 15s linear infinite; } .slide { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 400px; height: 300px; border-radius: 10px; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5); transform-style: preserve-3d; backface-visibility: hidden; } .slide-image { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; } .slide-content { position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; background-color: rgba(0, 0, 0, 0.5); color: #fff; text-align: center; } .slide-title { font-size: 32px; margin-bottom: 10px; } .slide-text { font-size: 18px; } </style> ``` 2. 使用CSS和JavaScript添加拖拽飞屏效果。在组件的mounted生命周期钩子中,添加以下代码: ``` mounted() { let startX, startY, currentX, currentY, distanceX, distanceY, isDragging = false; const carousel = this.$el.querySelector(".carousel"); carousel.addEventListener("mousedown", e => { startX = e.clientX; startY = e.clientY; isDragging = true; carousel.style.animation = "none"; }); carousel.addEventListener("mousemove", e => { if (!isDragging) return; e.preventDefault(); currentX = e.clientX; currentY = e.clientY; distanceX = currentX - startX; distanceY = currentY - startY; carousel.style.transform = `translateX(${distanceX}px) translateY(${distanceY}px) translateZ(-1000px) rotateY(${distanceX / 10}deg) rotateX(${distanceY / 10}deg)`; }); carousel.addEventListener("mouseup", e => { isDragging = false; carousel.style.animation = "rotate 15s linear infinite"; carousel.style.transform = `translateZ(-1000px)`; }); carousel.addEventListener("mouseleave", e => { isDragging = false; carousel.style.animation = "rotate 15s linear infinite"; carousel.style.transform = `translateZ(-1000px)`; }); } ``` 这代码监听轮播图的mousedown、mousemove、mouseup和mouseleave事件,在鼠标按下记录起始坐标,移动根据坐标计算移动距离,并将轮播图的transform样式设置为相应的位移和旋转角度。在鼠标抬起或离开轮播图,将轮播图的transform样式重置,并重新启动旋转动画。 这样就实现了一个Vue 3D轮播图上下拖拽飞屏效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值