vue自定义js图片碎片轮播图切换效果的实现代码


Posted in Javascript onApril 28, 2019

定义一个banner.js文件,代码如下

;window.requestAnimationFrame = window.requestAnimationFrame||function(a){return setTimeout(a,1000/60)};
window.cancelAnimationFrame = window.cancelAnimationFrame||clearTimeout;
var FragmentBanner = function(option) {
  //实例化时,可传的参数
  this.whiteList = ['container','controller','size','imgs','size','grid','index','fnTime','boxTime','type'];
  //容器将包容控制器
  this.container = '.banner';
  //默认的控制器
  this.controller = {
    view : '.banner-view',
    btn : '.banner-btn',
    num : '.banner-number',
    progre : '.banner-progres'
  };
  //栅格 行*列
  this.grid = {
    line : 5,
    list : 10
  };
  //容器的大小
  this.size = {
    width : 1200,
    height : 675,
  };
  //切换类型
  this.type = 1;
  //索引位置
  this.index = 0;
  //函数每次切换时间
  this.fnTime = 5000;
  //栅格每次运动时间
  this.boxTime = 2000;
  //栅格运动结束的时间
  this.activeTime = new Date();
  for(var a = 0,attrLenght = this.whiteList.length; a < attrLenght;a++){
    var attr = this.whiteList[a];
    if(option[attr] != undefined){
      this[attr] = option[attr];
    }
  }
  for(var i in option){
    if(this.whiteList[i] !== undefined){ ; }
  }
  init();
}
function setIndex(){
  this.index %= this.imgs.length;
  this.index = (this.index < 0) ? this.imgs.length - 1 : this.index;
  this.elem.numFind[this.index].className = 'on'; 
}
function init(){
  this.container = document.querySelector(this.container)
  if(!this.container){
    return alert('获取banner容器失败');
  }else{
    this.container.style.width = this.size.width+'px';
    this.container.style.height = this.size.height+'px';
  }
  this.elem = {};
  for(var e in this.controller){
    this.elem[e] = this.container.querySelector(this.controller[e]);
    if(this.elem[e] == null){
      return alert('获取'+e+'容器');
    }
  }
  //栅格
  var w = this.size.width / this.grid.list,
    h = this.size.height / this.grid.line;
  this.elem.viewBox = new Array();
  for(var i = 0,iL = this.grid.line;i < iL;i++){
    for(var j = 0,jL = this.grid.list;j < jL;j++){
      var newI = document.createElement('i');
      setCss(newI,{
        width : w+'px',
        height : h+'px',
        left : 0,
        top : 0,
        opacity : 1,
        backgroundImage : 'url("'+this.imgs[this.index]+'")',
        backgroundSize : this.size.width + 'px ' + this.size.height +'px',
        backgroundPosition : w * -j+'px ' + h * -i+'px'
      });
      this.elem.view.appendChild(newI);
      this.elem.viewBox.push(newI);
    }
  }
  //按钮动作
  for (var b = 1; b >= 0; b--) {
    var oB = document.createElement('span');
    (b) ? oB.innerHTML = '<' : oB.innerHTML = '>';
    oB.setIndex = b;
    oB.onclick = function(obj){
      show({
        switch : true,
        change : obj.setIndex == 0
      });
    }.bind(this,oB);
    this.elem.btn.appendChild(oB);
  }
  //数量
  for(var n = 0,nL = this.imgs.length; n < nL;n++){
    var oI = document.createElement('i');
    oI.setIndex = n;
    oI.onclick = function(obj){
      //显示动画
      show({
        switch : true,
        change : obj.setIndex
      });
    }.bind(this,oI)
    this.elem.num.appendChild(oI);
  }
  this.elem.numFind = this.elem.num.querySelectorAll('i');
  //进度条
  this.progre = new Array;
  for(var p = 1;p >= 0;p--){
    var oP = document.createElement('i');
    setCss(oP,{
      width : 0,
      backgroundColor : p ? '#00c3ff' : '#ffc300'
    });
    this.elem.progre.appendChild(oP);
    this.progre.push(oP);
  }
  //显示动画
  show();
  this.elem.numFind[this.index].className = 'on';
}
function setCss(obj,json){
  for( c in json){
    if(c == 'opacity'){
      obj.style.opacity = c;
      obj.style.filter = "alpha(opacity="+ (json[c]*100) +")";
    }else{
      obj.style[c] = json[c];
    }
  }
  return this;
}
function show(order){
  order = order || {};
  if(new Date() >= this.activeTime){
    this.elem.numFind[this.index].className = '';
    // 下次播放动画时候的进度条
    setCss(this.progre[1],{width : 0})
    anime(this.progre[1],{
        width : this.size.width
      },this.fnTime,function(){
        show({
          switch : true,
          change : true
        });
      }.bind(this));
    var status = true,
      activeTime = 0;
    for( var i = 0,iL = this.elem.viewBox.length;i < iL;i++ ){
      var startTime = getTypeTime(),
        endTime = getTypeTime(),
        obj = this.elem.viewBox[i];
        activeTime = Math.max(activeTime,startTime[1] + endTime[1]);
      anime(obj,{
        left : Math.ceil(Math.random() * this.size.width * 2 - this.size.width),
        top : Math.ceil(Math.random() * this.size.height * 2 - this.size.height),
        opacity: 0
      }, startTime[0] ,function(obj){
        if(order.switch && status){
          if(/number/i.test(typeof order.change)){
            this.index = order.change;
          }else{
            (order.change) ? ++this.index : --this.index;
          }
          setIndex();
          this.elem.numFind[this.index].className = 'on';
          status = false;
        }
        setCss(obj,{backgroundImage : 'url("'+this.imgs[this.index]+'")'})
        anime(obj,{
            left : 0,
            top : 0,
            opacity : 1
          },endTime[0]);
      }.bind(this,obj));
    }
    //栅格结束运动的时间
    this.activeTime = new Date(new Date().getTime() + activeTime);
    setCss(this.progre[0],{width : 0})
    anime(this.progre[0],{width : this.size.width},activeTime);
  }
}
function getTypeTime(){
  var timer = new Array();
  switch(this.type){
    case 1:
      timer.push(this.boxTime / 4 + Math.random() * this.boxTime / 4);
      timer.push(timer[0]);
    break;
    default:
      timer.push([Math.random() * this.boxTime / 5,this.boxTime / 10 * 3]);
      timer.push(timer[0][0] + timer[0][1]);
    break;
  }
  return timer;
}
function anime(obj,attr,endTime,callback) {
  (obj.timer) && cancelAnimationFrame(obj.timer);
  var cssJosn = obj.currentStyle || getComputedStyle(obj,null),
    start = {},end = {},goTime;
  //设置初始属性值和结束属性值
  for(var key in attr){
    if(attr[key] != parseFloat(cssJosn[key])){
      start[key] = parseFloat(cssJosn[key]);
      end[key] = attr[key] - start[key];
    }
  }
  goTime = new Date();
  if(endTime instanceof Array){
     (function delayFn(){
       if((new Date() - goTime) >= endTime[0]){
         endTime = endTime[1];
         goTime = new Date();
         ref();
       }else{
         obj.timer = requestAnimationFrame(delayFn);
       }
     })();
  }else{
    ref();
  }
  function ref(){
    var prop = (new Date() - goTime) / endTime;
    (prop >= 1) ? prop = 1 : obj.timer = requestAnimationFrame(ref);
    for(var key in start){
      var val = -end[key] * prop *(prop-2) + start[key];

      if(key == 'opacity'){

        obj.style.opacity = val;
        obj.style.filter = "alpha(opacity="+ (val*100) +")";
      }else{

        obj.style[key] = val+'px';
      }
    }

    (prop === 1) && callback && callback.call(obj);
  };
}
module.exports.FragmentBanner = FragmentBanner;

在需要的组件里面引入改js

import {FragmentBanner} from '../../../static/js/banner.js'

使用方式如下

// html代码
<header class="js_header mod-header">
   <div class="banner" id="banner1" style="margin: 50px auto;margin-bottom:0px">
      <div class="banner-view"></div>
      <div class="banner-btn"></div>
      <div class="banner-number"></div>
      <div class="banner-progres"></div>
   </div>
 </header>
//css样式
<style>
.banner{
  position: relative;
  overflow: hidden;
}
.banner-view{
  position: relative;
  height: 100%;
  z-index: 999;
  background-color: #090b22;
  background-repeat: no-repeat;
}
.banner-view i{
  position: relative;
  display: block;
  float: left;
  background-repeat: no-repeat;
}
.banner-btn{
  position: absolute;
  width: 100%;
  height: 0;
  top: 45%;
  font-family: "宋体";
  font-size: 20px;
  z-index: 999;
}
.banner-btn span{
  display: block;
  float: left;
  width: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #27cfc3;
  color: white;
  cursor: pointer;
  font-weight: 800;
  /* position: absolute;
  right:-150px; */
  /* background-image: url(../../../static/images/style-index.d437fb5e.png);
 background-position: -268px -18px;
 width: 56px;
 height: 56px; */
}
/* 
.banner-btn span:first-child{
  left: -150px;
} */
.banner-btn span:hover{
  background-color: rgba(0,0,0,0.6);
}
.banner-btn span + span{
  float: right;
}
.banner-number{
  position: absolute;
  bottom: 35px;
  width: 100%;
  height: 0;
  font-size: 0;
  text-align: center;
  z-index: 1000;
}
.banner-number > *{
  display: inline-block;
  border: 2px solid #fff;
  border-radius: 50%;
  margin: 0 8px;
  width: 10px;
  height: 10px;
  background-color: #00c3ff;
  cursor: pointer;
}
.banner-number > *:hover,
.banner-number > *.on{
  background-color: #ffc300;
}
.banner-progres{
  width: 100%;
  position: absolute;
  bottom: 0;
  height: 3px;
  z-index: 1000;
}
.banner-progres i{
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 3px;
  display: block;
  height: 100%;
  width: 0;
}
</style>
// js的引用
this.obj = {
      container : '#banner1',//选择容器 必选
      imgs : ['https://www.xuanfengge.com/wp-content/themes/lee3.0/dist/img/banner/1.0bd56759.jpg','https://www.xuanfengge.com/wp-content/themes/lee3.0/dist/img/banner/6.c6b4ec87.jpg'],//图片集合 必选
      size : {
        width : 1200,
        height : 300
      },//容器的大小 可选
      //行数与列数 可选
      grid : {
        line : 12,
        list : 14
      },
      index: 0,//图片集合的索引位置 可选
      type : 2,//切换类型 1 , 2 可选
      boxTime : 5000,//小方块来回运动的时长 可选
      fnTime : 10000//banner切换的时长 可选
   }
mounted () {
  FragmentBanner(this.obj );
 }

总结

以上所述是小编给大家介绍的vue自定义js图片碎片轮播图切换效果的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
理解Javascript_09_Function与Object
Oct 16 Javascript
JS、jquery实现几分钟前、几小时前、几天前等时间差显示效果的代码实例分享
Apr 11 Javascript
jQuery功能函数详解
Feb 01 Javascript
快速学习AngularJs HTTP响应拦截器
Dec 31 Javascript
学习JavaScript设计模式之迭代器模式
Jan 19 Javascript
JavaScript绑定事件监听函数的通用方法
May 14 Javascript
Javascript 普通函数和构造函数的区别
Nov 05 Javascript
详解Vue + Vuex 如何使用 vm.$nextTick
Nov 20 Javascript
Mint UI组件库CheckList使用及踩坑总结
Dec 20 Javascript
基于Vue实现微前端的示例代码
Apr 24 Javascript
浅谈JavaScript节流和防抖函数
Aug 25 Javascript
vue+vant 上传图片需要注意的地方
Jan 03 Vue.js
如何在微信小程序里面退出小程序的方法
Apr 28 #Javascript
vue实现移动端轻量日期组件不依赖第三方库的方法
Apr 28 #Javascript
详解小程序退出页面时清除定时器
Apr 28 #Javascript
详解在Javascript中进行面向切面编程
Apr 28 #Javascript
js比较两个单独的数组或对象是否相等的实例代码
Apr 28 #Javascript
详解在HTTPS 项目中使用百度地图 API
Apr 26 #Javascript
vue操作动画的记录animate.css实例代码
Apr 26 #Javascript
You might like
php上传图片到指定位置路径保存到数据库的具体实现
2013/12/30 PHP
将php数组输出html表格的方法
2014/02/24 PHP
php中的strpos使用示例
2014/02/27 PHP
PHPThumb图片处理实例
2014/05/03 PHP
既简单又安全的PHP验证码 附调用方法
2016/06/02 PHP
PHP实现限制IP访问的方法
2017/04/20 PHP
web的各种前端打印方法之jquery打印插件jqprint实现网页打印
2013/01/09 Javascript
让JavaScript的Alert弹出框失效的方法禁止弹出警告框
2014/09/03 Javascript
js实现数字每三位加逗号的方法
2015/02/05 Javascript
jquery实现的动态回到顶部特效代码
2015/10/28 Javascript
html、css和jquery相结合实现简单的进度条效果实例代码
2016/10/24 Javascript
jQuery zTree树插件动态加载实例代码
2017/05/11 jQuery
JS实现简单抖动效果
2017/06/01 Javascript
js实现搜索栏效果
2018/11/16 Javascript
Vue将页面导出为图片或者PDF
2020/08/17 Javascript
刷新页面后让控制台的js代码继续执行
2019/09/20 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
vuex+axios+element-ui实现页面请求loading操作示例
2020/02/02 Javascript
js实现简单放大镜效果
2020/03/07 Javascript
mpvue网易云短信接口实现小程序短信登录的示例代码
2020/04/03 Javascript
使用 Opentype.js 生成字体子集的实例代码详解
2020/05/25 Javascript
[01:29:46]DOTA2上海特级锦标赛C组资格赛#1 OG VS LGD第二局
2016/02/27 DOTA
[00:15]天涯墨客终极技能展示
2018/08/25 DOTA
[01:10]为家乡而战!完美世界城市挑战赛全国总决赛花絮
2019/07/25 DOTA
[43:57]Liquid vs Mineski 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
Python实现读取及写入csv文件的方法示例
2018/01/12 Python
Django 使用Ajax进行前后台交互的示例讲解
2018/05/28 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
python:HDF和CSV存储优劣对比分析
2020/06/08 Python
澳大利亚波西米亚风连衣裙在线商店:Fortunate One
2019/04/01 全球购物
英国健身超市:Fitness Superstore
2019/06/17 全球购物
安全检查汇报材料
2014/12/26 职场文书
少年犯观后感
2015/06/11 职场文书
2016年11月份红领巾广播稿
2015/12/21 职场文书
Windows安装Anaconda3的方法及使用过程详解
2021/06/11 Python
Python IO文件管理的具体使用
2022/03/20 Python