iSlider手机端图片滑动切换插件使用详解

 更新时间:2019年12月24日 10:46:06   作者:here962464  
这篇文章主要为大家详细介绍了iSlider手机端图片滑动切换插件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

自适应轮播图,支持手机触屏滑动,三种切换效果。

效果图:

js:

var iSlider = function(opts) {
 if (!opts.dom) {
  throw new Error("dom element can not be empty!");
 }
 if (!opts.data || !opts.data.length) {
  throw new Error("data must be an array and must have more than one element!");
 }
 this._opts = opts;
 this._setting();
 this._renderHTML();
 this._bindHandler();
};
iSlider.prototype._setting = function() {
 var opts = this._opts;
 this.wrap = opts.dom;
 this.data = opts.data;
 this.type = opts.type || 'pic';
 this.isVertical = opts.isVertical || false;
 this.onslide = opts.onslide;
 this.onslidestart = opts.onslidestart;
 this.onslideend = opts.onslideend;
 this.onslidechange = opts.onslidechange;
 this.duration = opts.duration || 4000;
 this.log = opts.isDebug ?
 function(str) {
  console.log(str)
 }: function() {};
 this.axis = this.isVertical ? 'Y': 'X';
 this.width = this.wrap.clientWidth;
 this.height = this.wrap.clientHeight;
 this.ratio = this.height / this.width;
 this.scale = opts.isVertical ? this.height: this.width;
 this.sliderIndex = this.sliderIndex || 0;
 if (this.data.length < 2) {
  this.isLooping = false;
  this.isAutoPlay = false;
 } else {
  this.isLooping = opts.isLooping || false;
  this.isAutoplay = opts.isAutoplay || false;
 }
 if (this.isAutoplay) {
  this.play();
 }
 this._setUpDamping();
 this._animateFunc = (opts.animateType in this._animateFuncs) ? this._animateFuncs[opts.animateType] : this._animateFuncs['default'];
 this._setPlayWhenFocus();
};
iSlider.prototype._setPlayWhenFocus = function() {
 var self = this;
 window.addEventListener('focus',
 function() {
  self.isAutoplay && self.play();
 },
 false);
 window.addEventListener('blur',
 function() {
  self.pause();
 },
 false);
}
iSlider.prototype._animateFuncs = {
 'default': function(dom, axis, scale, i, offset) {
  dom.style.webkitTransform = 'translateZ(0) translate' + axis + '(' + (offset + scale * (i - 1)) + 'px)';
 },
 'rotate': function(dom, axis, scale, i, offset) {
  var rotateDirect = (axis == "X") ? "Y": "X";
  var absoluteOffset = Math.abs(offset);
  var bdColor = window.getComputedStyle(this.wrap.parentNode, null).backgroundColor;
  if (this.isVertical) {
   offset = -offset;
  }
  this.wrap.style.webkitPerspective = scale * 4;
  if (i == 1) {
   dom.style.zIndex = scale - absoluteOffset;
  } else {
   dom.style.zIndex = (offset > 0) ? (1 - i) * absoluteOffset: (i - 1) * absoluteOffset;
  }
  dom.style.backgroundColor = bdColor || '#333';
  dom.style.position = 'absolute';
  dom.style.webkitBackfaceVisibility = 'hidden';
  dom.style.webkitTransformStyle = 'preserve-3d';
  dom.style.webkitTransform = 'rotate' + rotateDirect + '(' + 90 * (offset / scale + i - 1) + 'deg) translateZ(' + (0.888 * scale / 2) + 'px) scale(0.888)';
 },
 'flip': function(dom, axis, scale, i, offset) {
  var rotateDirect = (axis == "X") ? "Y": "X";
  var bdColor = window.getComputedStyle(this.wrap.parentNode, null).backgroundColor;
  if (this.isVertical) {
   offset = -offset;
  }
  this.wrap.style.webkitPerspective = scale * 4;
  if (offset > 0) {
   dom.style.visibility = (i > 1) ? 'hidden': 'visible';
  } else {
   dom.style.visibility = (i < 1) ? 'hidden': 'visible';
  }
  dom.style.backgroundColor = bdColor || '#333';
  dom.style.position = 'absolute';
  dom.style.webkitBackfaceVisibility = 'hidden';
  dom.style.webkitTransform = 'translateZ(' + (scale / 2) + 'px) rotate' + rotateDirect + '(' + 180 * (offset / scale + i - 1) + 'deg) scale(0.875)';
 },
 'depth': function(dom, axis, scale, i, offset) {
  var rotateDirect = (axis == "X") ? "Y": "X";
  var zoomScale = (4 - Math.abs(i - 1)) * 0.15;
  this.wrap.style.webkitPerspective = scale * 4;
  if (i == 1) {
   dom.style.zIndex = 100;
  } else {
   dom.style.zIndex = (offset > 0) ? (1 - i) : (i - 1);
  }
  dom.style.webkitTransform = 'scale(' + zoomScale + ', ' + zoomScale + ') translateZ(0) translate' + axis + '(' + (offset + 1.3 * scale * (i - 1)) + 'px)';
 },
 'tear': function(dom, axis, scale, i, offset) {
  var rotateDirect = (axis == "X") ? "Y": "X";
  var zoomScale = 1 - (Math.abs(i - 1) * 0.2);
  this.wrap.style.webkitPerspective = scale * 4;
  if (i == 1) {
   dom.style.zIndex = 100;
  } else {
   dom.style.zIndex = (offset > 0) ? (1 - i) : (i - 1);
  }
  dom.style.webkitTransform = 'scale(' + zoomScale + ', ' + zoomScale + ') translateZ(0) translate' + axis + '(' + (offset + scale * (i - 1)) + 'px)';
 }
}
iSlider.prototype._setUpDamping = function() {
 var oneIn2 = this.scale >> 1;
 var oneIn4 = oneIn2 >> 1;
 var oneIn16 = oneIn4 >> 2;
 this._damping = function(distance) {
  var dis = Math.abs(distance);
  var result;
  if (dis < oneIn2) {
   result = dis >> 1;
  } else if (dis < oneIn2 + oneIn4) {
   result = oneIn4 + ((dis - oneIn2) >> 2);
  } else {
   result = oneIn4 + oneIn16 + ((dis - oneIn2 - oneIn4) >> 3);
  }
  return distance > 0 ? result: -result;
 };
};
iSlider.prototype._renderItem = function(i) {
 var item, html;
 var len = this.data.length;
 if (!this.isLooping) {
  item = this.data[i] || {
   empty: true
  };
 } else {
  if (i < 0) {
   item = this.data[len + i];
  } else if (i > len - 1) {
   item = this.data[i - len];
  } else {
   item = this.data[i];
  }
 }
 if (item.empty) {
  return '';
 }
 if (this.type === 'pic') {
  html = item.height / item.width > this.ratio ? '<img height="' + this.height + '" src="' + item.content + '">': '<img width="' + this.width + '" src="' + item.content + '">';
 } else if (this.type === 'dom') {
  html = '<div style="height:' + item.height + ';width:' + item.width + ';">' + item.content + '</div>';
 } else if (this.type === 'overspread') {
  html = this.ratio < 1 ? '<div style="height: 100%; width:100%; background:url(' + item.content + ') center no-repeat; background-size:' + this.width + 'px auto;"></div>': '<div style="height: 100%; width:100%; background:url(' + item.content + ') center no-repeat; background-size: auto ' + this.height + 'px;"></div>';
 }
 return html;
};
iSlider.prototype._renderHTML = function() {
 var outer;
 if (this.outer) {
  this.outer.innerHTML = '';
  outer = this.outer;
 } else {
  outer = document.createElement('ul');
 }
 outer.style.width = this.width + 'px';
 outer.style.height = this.height + 'px';
 this.els = [];
 for (var i = 0; i < 3; i++) {
  var li = document.createElement('li');
  li.style.width = this.width + 'px';
  li.style.height = this.height + 'px';
  this._animateFunc(li, this.axis, this.scale, i, 0);
  this.els.push(li);
  outer.appendChild(li);
  if (this.isVertical && (this._opts.animateType == 'rotate' || this._opts.animateType == 'flip')) {
   li.innerHTML = this._renderItem(1 - i + this.sliderIndex);
  } else {
   li.innerHTML = this._renderItem(i - 1 + this.sliderIndex);
  }
 }
 if (!this.outer) {
  this.outer = outer;
  this.wrap.appendChild(outer);
 }
};
iSlider.prototype._slide = function(n) {
 var data = this.data;
 var els = this.els;
 var idx = this.sliderIndex + n;
 if (data[idx]) {
  this.sliderIndex = idx;
 } else {
  if (this.isLooping) {
   this.sliderIndex = n > 0 ? 0 : data.length - 1;
  } else {
   n = 0;
  }
 }
 this.log('pic idx:' + this.sliderIndex);
 var sEle;
 if (this.isVertical && (this._opts.animateType == 'rotate' || this._opts.animateType == 'flip')) {
  if (n > 0) {
   sEle = els.pop();
   els.unshift(sEle);
  } else if (n < 0) {
   sEle = els.shift();
   els.push(sEle);
  }
 } else {
  if (n > 0) {
   sEle = els.shift();
   els.push(sEle);
  } else if (n < 0) {
   sEle = els.pop();
   els.unshift(sEle);
  }
 }
 if (n !== 0) {
  sEle.innerHTML = this._renderItem(idx + n);
  sEle.style.webkitTransition = 'none';
  sEle.style.visibility = 'hidden';
  setTimeout(function() {
   sEle.style.visibility = 'visible';
  },
  200);
  this.onslidechange && this.onslidechange(this.sliderIndex);
 }
 for (var i = 0; i < 3; i++) {
  if (els[i] !== sEle) {
   els[i].style.webkitTransition = 'all .3s ease';
  }
  this._animateFunc(els[i], this.axis, this.scale, i, 0);
 }
 if (this.isAutoplay) {
  if (this.sliderIndex === data.length - 1 && !this.isLooping) {
   this.pause();
  }
 }
};
iSlider.prototype._bindHandler = function() {
 var self = this;
 var scaleW = self.scaleW;
 var outer = self.outer;
 var len = self.data.length;
 var startHandler = function(evt) {
  self.pause();
  self.onslidestart && self.onslidestart();
  self.log('Event: beforeslide');
  self.startTime = new Date().getTime();
  self.startX = evt.targetTouches[0].pageX;
  self.startY = evt.targetTouches[0].pageY;
  var target = evt.target;
  while (target.nodeName != 'LI' && target.nodeName != 'BODY') {
   target = target.parentNode;
  }
  self.target = target;
 };
 var moveHandler = function(evt) {
  evt.preventDefault();
  self.onslide && self.onslide();
  self.log('Event: onslide');
  var axis = self.axis;
  var offset = evt.targetTouches[0]['page' + axis] - self['start' + axis];
  if (!self.isLooping) {
   if (offset > 0 && self.sliderIndex === 0 || offset < 0 && self.sliderIndex === self.data.length - 1) {
    offset = self._damping(offset);
   }
  }
  for (var i = 0; i < 3; i++) {
   var item = self.els[i];
   item.style.webkitTransition = 'all 0s';
   self._animateFunc(item, axis, self.scale, i, offset);
  }
  self.offset = offset;
 };
 var endHandler = function(evt) {
  evt.preventDefault();
  var boundary = self.scale / 2;
  var metric = self.offset;
  var endTime = new Date().getTime();
  boundary = endTime - self.startTime > 300 ? boundary: 14;
  if (metric >= boundary) {
   self._slide( - 1);
  } else if (metric < -boundary) {
   self._slide(1);
  } else {
   self._slide(0);
  }
  self.isAutoplay && self.play();
  self.offset = 0;
  self.onslideend && self.onslideend();
  self.log('Event: afterslide');
 };
 var orientationchangeHandler = function(evt) {
  setTimeout(function() {
   self.reset();
   self.log('Event: orientationchange');
  },
  100);
 };
 outer.addEventListener('touchstart', startHandler);
 outer.addEventListener('touchmove', moveHandler);
 outer.addEventListener('touchend', endHandler);
 window.addEventListener('orientationchange', orientationchangeHandler);
};
iSlider.prototype.reset = function() {
 this.pause();
 this._setting();
 this._renderHTML();
 this.isAutoplay && this.play();
};
iSlider.prototype.play = function() {
 var self = this;
 var duration = this.duration;
 clearInterval(this.autoPlayTimer);
 this.autoPlayTimer = setInterval(function() {
  self._slide(1);
 },
 duration);
};
iSlider.prototype.pause = function() {
 clearInterval(this.autoPlayTimer);
};
iSlider.prototype.extend = function(plugin) {
 var fn = iSlider.prototype;
 Object.keys(plugin).forEach(function(property) {
  Object.defineProperty(fn, property, Object.getOwnPropertyDescriptor(plugin, property));
 })
}
iSlider.prototype.extend({
 bindMouse: function() {
  var self = this;
  var scaleW = self.scaleW;
  var outer = self.outer;
  var len = self.data.length;
  var bDrag = false;
  var mouseStart = function(evt) {
   bDrag = true;
   self.pause();
   self.onslidestart && self.onslidestart();
   self.startTime = new Date().getTime();
   self.startX = evt.clientX;
   self.startY = evt.clientY;
   var target = evt.target;
   while (target.nodeName != 'LI' && target.nodeName != 'BODY') {
    target = target.parentNode;
   }
   self.target = target;
  };
  var mouseMove = function(evt) {
   if (bDrag) {
    evt.preventDefault();
    self.onslide && self.onslide();
    var axis = self.axis;
    var offset = evt['client' + axis] - self['start' + axis];
    if (!self.isLooping) {
     if (offset > 0 && self.sliderIndex === 0 || offset < 0 && self.sliderIndex === self.data.length - 1) {
      offset = self._damping(offset);
     }
    }
    for (var i = 0; i < 3; i++) {
     var item = self.els[i];
     item.style.webkitTransition = 'all 0s';
     self._animateFunc(item, axis, self.scale, i, offset);
    }
    self.offset = offset;
   }
  };
  var mouseEnd = function(evt) {
   evt.preventDefault();
   bDrag = false;
   var boundary = self.scale / 2;
   var metric = self.offset;
   var endTime = new Date().getTime();
   boundary = endTime - self.startTime > 300 ? boundary: 14;
   if (metric >= boundary) {
    self._slide( - 1);
   } else if (metric < -boundary) {
    self._slide(1);
   } else {
    self._slide(0);
   }
   self.isAutoplay && self.play();
   self.offset = 0;
   self.onslideend && self.onslideend();
   self.log('Event: afterslide');
  };
  outer.addEventListener('mousedown', mouseStart);
  outer.addEventListener('mousemove', mouseMove);
  outer.addEventListener('mouseup', mouseEnd);
 }
})

html:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style type="text/css">
  *{
   padding: 0;
   list-style: none;
   margin: 0;
  }
  /*容器高度*/
  #iSlider-effect-wrapper {
   height: 400px;
   width: 500px;
   margin: 0 auto;
   margin-top: 4.6rem;
   overflow: hidden;
   position: relative;
  }
  .iSlider-effect ul{
   list-style: none;
   padding: 0;
   margin: 0;
   height: 100%;
   overflow: hidden
  }
  .iSlider-effect li {
   position: absolute;
   margin: 0;
   padding: 0;
   height: 100%;
   overflow: hidden;
   display: -webkit-box;
   -webkit-box-pack: center;
   -webkit-box-align: center;
   list-style: none
  }
  .iSlider-effect ul li img {
   max-width: 100%;
   max-height: 100%;
   margin: 0;
   padding: 0
  }
  .iSlider-effect div {
   background-color: #fff;
   padding: 3px
  }
 </style>
</head>
<body>
 <script type="text/javascript" src="mobile_slider.js"></script>
 <!--组件容器-->
 <div id="iSlider-effect-wrapper"> </div>
 <script>
  //组件注册
  var islider1 = new iSlider({
   //节点获取
   dom: document.getElementById("iSlider-effect-wrapper"),
   //图片配置
   data: [
    {
     content: "images/01.jpeg",
    },
    {
     content: "images/04.jpg",
    },
    {
     content: "images/05.jpg",
    }
   ],
   //播放间隔
   duration: 3000,
   //animateType切换方式
   //default:默认
   //rotate:旋转
   //flip:弹出
   animateType: 'rotate',
   //是否自动播放
   isAutoplay: true,
   //是否循环播放
   isLooping: true,
   // isVertical: true, 是否垂直滚动
  });
  islider1.bindMouse();
 </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 微信小程序实现幸运大转盘功能的示例代码

    微信小程序实现幸运大转盘功能的示例代码

    这篇文章主要介绍了如何在微信小程序中实现幸运大转盘功能,参与用户点击抽奖便可抽取轮盘的奖品,感兴趣的小伙伴可以跟随小编一起动手试一试
    2022-03-03
  • JavaScript的懒加载处理的方式

    JavaScript的懒加载处理的方式

    懒加载是一种优化技术,它可以延迟加载某些资源,直到它们真正需要被使用的时候才进行加载,实现懒加载的方法一般分为两种:基于 Intersection Observer API 的懒加载和基于滚动事件的懒加载,本文给大家介绍JavaScript的懒加载处理方式,感兴趣的朋友一起看看吧
    2023-10-10
  • js replace 与replaceall实例用法详解

    js replace 与replaceall实例用法详解

    这篇文章介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下
    2013-08-08
  • JavaScript实现精美个性导航栏筋斗云效果

    JavaScript实现精美个性导航栏筋斗云效果

    这篇文章主要介绍了JavaScript实现精美个性导航栏筋斗云效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-10-10
  • var let const关键字之间的区别及使用场景示例详解

    var let const关键字之间的区别及使用场景示例详解

    这篇文章主要为大家介绍了var let const关键字之间的区别及使用场景示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • js实现的仿Photoshop鼠标滚轮控制输入框取值(修正兼容Chrome)

    js实现的仿Photoshop鼠标滚轮控制输入框取值(修正兼容Chrome)

    一直很想做这个效果,原理是监听鼠标滚轮事件;可将此效果继续发散到其他应用上,如图片缩放,页面缩放等。
    2010-02-02
  • JavaScript调试工具汇总

    JavaScript调试工具汇总

    这篇文章主要介绍了7款基于桌面和WEB的JavaScript调试工具,更有效地处理动态类型使应用程序更符合编码标准,不管你是新学习javascript的菜鸟还是业界大神,都非常的有用。
    2014-12-12
  • 微信端口及协议分析(java、C版)

    微信端口及协议分析(java、C版)

    最近接了个项目,项目需求是:手机通过WIFI连接上网,而老板要求,员工使用手机只能上微信,而不能上其他网页和看在线视频。下面通过本文给大家分享微信端口及协议分析,感兴趣的朋友一起看看吧
    2016-11-11
  • JavaScript 继承的实现

    JavaScript 继承的实现

    正因为JavaScript本身没有完整的类和继承的实现,并且我们也看到通过手工实现的方式存在很多问题, 因此对于这个富有挑战性的任务网上已经有很多实现了
    2009-07-07
  • avaScript基础学习-基本的语法规则

    avaScript基础学习-基本的语法规则

    这篇文章主要介绍了avaScript的语法规则,本文的语法讲解主要讲一下与其他语言的区别,下面详细的介绍内容,需要的小伙伴可以参考一下,希望对你有所帮助
    2022-02-02

最新评论