原生js写轮播图分块运动 JavaScript的技巧

原生js写轮播图分块运动
先上效果图
在这里插入图片描述
图片是背景图片,把图片分成块添加,利用运动框架改变opacity的值从0~1
利用backgroundPosition选择图片的位置,拼成一整张图,第一步首先创建图片的容器大小一样
横7竖4,把一整图分成28份,分别添加到容器里 同时要吧位置添加上代码如下

 for (var r = 0; r < R; r++) {
      for (var c = 0; c < C; c++) {
        var oSpan = document.createElement('span');
        oSpan.style.width = oBox.offsetWidth / C + 'px';
        oSpan.style.height = oBox.offsetHeight / R + 'px';
        oBox.appendChild(oSpan);
        oSpan.style.left = oSpan.offsetWidth * c + 'px';
        oSpan.style.top = oSpan.offsetHeight * r + 'px';
        oSpan.style.backgroundPosition =
          '-' + oSpan.offsetWidth * c + 'px -' + oSpan.offsetHeight * r + 'px';
        oSpan.r = r;
        oSpan.c = c;
      }
    }

然后在写一个更换图片的过程,分布运动 点击时候执行,同时还需要一个开关(sign)防止连续点击代码如下
  function near() {
      var oSpan = document.getElementsByTagName('span');
      iNow++;
      for (let i = 0; i < oSpan.length; i++) {
        setTimeout(() => {
          oSpan[i].style.opacity = 0;
          animate(oSpan[i], { opacity: 100 }, 1, 0.01, () => {
            if (i == oSpan.length - 1) sign = true;
          });
          oSpan[i].style.backgroundImage =
            'url("./img/' + (iNow % 3) + '.jpg")';
          oSpan[i].style.backgroundPosition =
            '-' +
            oSpan[i].offsetWidth * oSpan[i].c +
            'px -' +
            oSpan[i].offsetHeight * oSpan[i].r +
            'px';
        }, (oSpan[i].r + oSpan[i].c) * 100);
      }
    }

运动框架的封装

function animate(obj, json, interval, sp, fn) {
      clearInterval(obj.timer);
      function getStyle(obj, arr) {
        if (obj.currentStyle) {
          return obj.currentStyle[arr];
        } else {
          return document.defaultView.getComputedStyle(obj, null)[arr];
        }
      }
      obj.timer = setInterval(function() {
        var flag = true;
        for (var arr in json) {
          var icur = 0;
          if (arr == 'opacity') {
            icur = Math.round(parseFloat(getStyle(obj, arr)) * 100);
          } else {
            icur = parseInt(getStyle(obj, arr));
          }
          var speed = (json[arr] - icur) * sp;
          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
          if (icur != json[arr]) {
            flag = false;
          }
          if (arr == 'opacity') {
            obj.style.filter = "alpha(opacity : '+(icur + speed)+' )";
            obj.style.opacity = (icur + speed) / 100;
          } else {
            obj.style[arr] = icur + speed + 'px';
          }
        }
        if (flag) {
          clearInterval(obj.timer);
          if (fn) {
            fn();
          }
        }
      }, interval);
    }

html

<input type="button" name="" value="点击" id="btn" />
    <div id="box"></div>

css样式

  body {
        margin: 0;
      }
      #box {
        width: 700px;
        height: 400px;
        position: relative;
        margin: 100px auto;
      }
      #box span {
        position: absolute;
        width: 100px;
        height: 100px;
        /* background: url('./img/0.jpg'); */
      }

重点js

window.onload = function() {
    var oBox = document.getElementById('box');
    var oBtn = document.getElementById('btn');
    var C = 7;
    var R = 4;
    var sign = true;
    //  初始化创建
    for (var r = 0; r < R; r++) {
      for (var c = 0; c < C; c++) {
        var oSpan = document.createElement('span');
        oSpan.style.width = oBox.offsetWidth / C + 'px';
        oSpan.style.height = oBox.offsetHeight / R + 'px';
        oBox.appendChild(oSpan);
        oSpan.style.left = oSpan.offsetWidth * c + 'px';
        oSpan.style.top = oSpan.offsetHeight * r + 'px';
        oSpan.style.backgroundPosition =
          '-' + oSpan.offsetWidth * c + 'px -' + oSpan.offsetHeight * r + 'px';
        oSpan.r = r;
        oSpan.c = c;
      }
    }

    oBtn.onclick = function() {
      if (!sign) return;
      sign = false;
      near();
    };
    //  注释打开就是自动轮播图
    // setInterval(() => {
    //   oBtn.onclick();
    // }, 40);
    let iNow = 0;
    near();
    function near() {
      var oSpan = document.getElementsByTagName('span');
      iNow++;
      for (let i = 0; i < oSpan.length; i++) {
        setTimeout(() => {
          oSpan[i].style.opacity = 0;
          animate(oSpan[i], { opacity: 100 }, 1, 0.01, () => {
            if (i == oSpan.length - 1) sign = true;
          });
          oSpan[i].style.backgroundImage =
            'url("./img/' + (iNow % 3) + '.jpg")';
          oSpan[i].style.backgroundPosition =
            '-' +
            oSpan[i].offsetWidth * oSpan[i].c +
            'px -' +
            oSpan[i].offsetHeight * oSpan[i].r +
            'px';
        }, (oSpan[i].r + oSpan[i].c) * 100);
      }
    }
    // 封装的运动框架
    function animate(obj, json, interval, sp, fn) {
      clearInterval(obj.timer);
      function getStyle(obj, arr) {
        if (obj.currentStyle) {
          return obj.currentStyle[arr];
        } else {
          return document.defaultView.getComputedStyle(obj, null)[arr];
        }
      }
      obj.timer = setInterval(function() {
        var flag = true;
        for (var arr in json) {
          var icur = 0;
          if (arr == 'opacity') {
            icur = Math.round(parseFloat(getStyle(obj, arr)) * 100);
          } else {
            icur = parseInt(getStyle(obj, arr));
          }
          var speed = (json[arr] - icur) * sp;
          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
          if (icur != json[arr]) {
            flag = false;
          }
          if (arr == 'opacity') {
            obj.style.filter = "alpha(opacity : '+(icur + speed)+' )";
            obj.style.opacity = (icur + speed) / 100;
          } else {
            obj.style[arr] = icur + speed + 'px';
          }
        }
        if (flag) {
          clearInterval(obj.timer);
          if (fn) {
            fn();
          }
        }
      }, interval);
    }
  };

全部代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      body {
        margin: 0;
      }
      #box {
        width: 700px;
        height: 400px;
        position: relative;
        margin: 100px auto;
      }
      #box span {
        position: absolute;
        width: 100px;
        height: 100px;
        /* background: url('./img/0.jpg'); */
      }
    </style>
  </head>
  <body>
    <input type="button" name="" value="点击" id="btn" />
    <div id="box"></div>
  </body>
</html>
<script>
  window.onload = function() {
    var oBox = document.getElementById('box');
    var oBtn = document.getElementById('btn');
    var C = 7;
    var R = 4;
    var sign = true;
    //  初始化创建
    for (var r = 0; r < R; r++) {
      for (var c = 0; c < C; c++) {
        var oSpan = document.createElement('span');
        oSpan.style.width = oBox.offsetWidth / C + 'px';
        oSpan.style.height = oBox.offsetHeight / R + 'px';
        oBox.appendChild(oSpan);
        oSpan.style.left = oSpan.offsetWidth * c + 'px';
        oSpan.style.top = oSpan.offsetHeight * r + 'px';
        oSpan.style.backgroundPosition =
          '-' + oSpan.offsetWidth * c + 'px -' + oSpan.offsetHeight * r + 'px';
        oSpan.r = r;
        oSpan.c = c;
      }
    }

    oBtn.onclick = function() {
      if (!sign) return;
      sign = false;
      near();
    };
    //  注释打开就是自动轮播图
    // setInterval(() => {
    //   oBtn.onclick();
    // }, 40);
    let iNow = 0;
    near();
    function near() {
      var oSpan = document.getElementsByTagName('span');
      iNow++;
      for (let i = 0; i < oSpan.length; i++) {
        setTimeout(() => {
          oSpan[i].style.opacity = 0;
          animate(oSpan[i], { opacity: 100 }, 1, 0.01, () => {
            if (i == oSpan.length - 1) sign = true;
          });
          oSpan[i].style.backgroundImage =
            'url("./img/' + (iNow % 3) + '.jpg")';
          oSpan[i].style.backgroundPosition =
            '-' +
            oSpan[i].offsetWidth * oSpan[i].c +
            'px -' +
            oSpan[i].offsetHeight * oSpan[i].r +
            'px';
        }, (oSpan[i].r + oSpan[i].c) * 100);
      }
    }
    // 封装的运动框架
    function animate(obj, json, interval, sp, fn) {
      clearInterval(obj.timer);
      function getStyle(obj, arr) {
        if (obj.currentStyle) {
          return obj.currentStyle[arr];
        } else {
          return document.defaultView.getComputedStyle(obj, null)[arr];
        }
      }
      obj.timer = setInterval(function() {
        var flag = true;
        for (var arr in json) {
          var icur = 0;
          if (arr == 'opacity') {
            icur = Math.round(parseFloat(getStyle(obj, arr)) * 100);
          } else {
            icur = parseInt(getStyle(obj, arr));
          }
          var speed = (json[arr] - icur) * sp;
          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
          if (icur != json[arr]) {
            flag = false;
          }
          if (arr == 'opacity') {
            obj.style.filter = "alpha(opacity : '+(icur + speed)+' )";
            obj.style.opacity = (icur + speed) / 100;
          } else {
            obj.style[arr] = icur + speed + 'px';
          }
        }
        if (flag) {
          clearInterval(obj.timer);
          if (fn) {
            fn();
          }
        }
      }, interval);
    }
  };
</script>

本文章为原创!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值