欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-13 13:45:34
定义一个banner.js文件,代码如下 ;window.requestanimationframe = window.requestanimationframe...

定义一个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图片碎片轮播图切换效果的实现代码,希望对大家有所帮助

上一篇:

下一篇: