Vue实现图片轮播组件思路及实例解析


Posted in Javascript onMay 11, 2020

1、先看效果:

Vue实现图片轮播组件思路及实例解析

熟悉的图片轮播,只要是个网站,百分之90以上会有个图片轮播。我认为使用图片轮播。

第一可以给人以一种美观的感受,而不会显得网站那么呆板,

第二可以增加显示内容,同样的区域可以显示更多内容。

 2、每学一个新东西 ,图片轮播都是很好的练手案例,而且,也很实用。

    3、基本要求:页面加载,自动播放。鼠标悬停,停止播放。鼠标离开,继续播放

点击左右箭头切换上一张,下一张图片。

下方小圆点显示当前位第几张图片。

 4、使用Vue实现

 5、示例代码

结构html:

<template>
 <div id="slider">
  <div class="window" @mouseover="stop" @mouseleave="play">
   <ul class="container" :style="containerStyle">
    <li>
     <img :style="{width:imgWidth+'px'}" :src="sliders[sliders.length - 1].img" alt="">
    </li>
    <li v-for="(item, index) in sliders" :key="index">
     <img :style="{width:imgWidth+'px'}" :src="item.img" alt="">
    </li>
    <li>
     <img :style="{width:imgWidth+'px'}" :src="sliders[0].img" alt="">
    </li>
   </ul>
   <ul class="direction">
    <li class="left" @click="move(600, 1, speed)">
     <svg class="icon" width="30px" height="30.00px" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path fill="#ffffff" d="M481.233 904c8.189 0 16.379-3.124 22.628-9.372 12.496-12.497 12.496-32.759 0-45.256L166.488 512l337.373-337.373c12.496-12.497 12.496-32.758 0-45.255-12.498-12.497-32.758-12.497-45.256 0l-360 360c-12.496 12.497-12.496 32.758 0 45.255l360 360c6.249 6.249 14.439 9.373 22.628 9.373z" /></svg>
    </li>
    <li class="right" @click="move(600, -1, speed)">
     <svg class="icon" width="30px" height="30.00px" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path fill="#ffffff" d="M557.179 904c-8.189 0-16.379-3.124-22.628-9.372-12.496-12.497-12.496-32.759 0-45.256L871.924 512 534.551 174.627c-12.496-12.497-12.496-32.758 0-45.255 12.498-12.497 32.758-12.497 45.256 0l360 360c12.496 12.497 12.496 32.758 0 45.255l-360 360c-6.249 6.249-14.439 9.373-22.628 9.373z" /></svg>
    </li>
   </ul>
   <ul class="dots">
    <li v-for="(dot, i) in sliders" :key="i"
    :class="{dotted: i === (currentIndex-1)}"
    @click = jump(i+1)
    >
    </li>
   </ul>
  </div>
 </div>
</template>

CSS部分:

*{
    box-sizing: border-box;
    margin:0;
    padding:0;
   }
   ol,ul{
    list-style: none;
   }
   #slider{
    text-align: center;
   }
   .window{
    position:relative;
    width:600px;
    height:400px;
    margin:0 auto;
    overflow:hidden;
   }
   .container{
    display:flex;
    position:absolute;
   }
   .left, .right{
    position:absolute;
    top:50%;
    transform:translateY(-50%);
    width:50px;
    height:50px;
    background-color:rgba(0,0,0,.3);
    border-radius:50%;
    cursor:pointer;
   }
   .left{
    left:3%;
    padding-left:12px;
    padding-top:10px;
   }
   .right{
    right:3%;
    padding-right:12px;
    padding-top:10px;
   }
   img{
    user-select: none;
   }
   .dots{
     position:absolute;
     bottom:10px;
     left:50%;
     transform:translateX(-50%);
    }
   .dots li{
    display:inline-block;
    width:15px;
    height:15px;
    margin:0 3px;
    border:1px solid white;
    border-radius:50%;
    background-color:#333;
    cursor:pointer;
   }
   .dots .dotted{
    background-color:orange;
   }

JavaScript部分:

script>
export default {
 name: 'slider',
 props: {
  initialSpeed: {
   type: Number,
   default: 30
  },
  initialInterval: {
   type: Number,
   default: 3
  }
 },
 data () {
  return {
   sliders:[
    {
     img:'http://img.hb.aicdn.com/adbde61e4343dedd21e97ea7f22666825a8db7d077ffe-qn8Pjn_fw658'
    },
    {
     img:'http://img.hb.aicdn.com/adeed7d28df6e776c2fa6032579c697381d1a82b7fe00-fwRqgn_fw658'
    },
    {
     img:'http://img.hb.aicdn.com/ab7f48509b3c0353017d9a85ef1d12400c9b2724540d4-p3zouo_fw658'
    },
    {
     img:'http://img.hb.aicdn.com/60f788fc2a846192f224b9e6d4904b30e54926211d3d67-ACFJ9G_fw658'
    },
    {
     img:'http://img.hb.aicdn.com/22ded455284aab361b8d2056e82f74a891a019704296a-PSraEB_fw658'
    },
   ],
   imgWidth:600,
   currentIndex:1,
   distance:-600,
   transitionEnd: true,
   speed: this.initialSpeed
  }
 },
 computed:{
  containerStyle() {
   return {
    transform:`translate3d(${this.distance}px, 0, 0)`
   }
  },
  interval() {
   return this.initialInterval * 1000
  }
 },
 mounted() {
  this.init()
 },
 methods:{
  init() {
   this.play()
   window.onblur = function() { this.stop() }.bind(this)
   window.onfocus = function() { this.play() }.bind(this)
  },
  move(offset, direction, speed) {
   console.log(speed)
   if (!this.transitionEnd) return
   this.transitionEnd = false
   direction === -1 ? this.currentIndex += offset/600 : this.currentIndex -= offset/600
   if (this.currentIndex > 5) this.currentIndex = 1
   if (this.currentIndex < 1) this.currentIndex = 5

   const destination = this.distance + offset * direction
   this.animate(destination, direction, speed)
  },
  animate(des, direc, speed) {
   if (this.temp) {
    window.clearInterval(this.temp);
    this.temp = null ;
   }
   this.temp = window.setInterval(() => {
    if ((direc === -1 && des < this.distance) || (direc === 1 && des > this.distance)) {
     this.distance += speed * direc
    } else {
     this.transitionEnd = true
     window.clearInterval(this.temp)
     this.distance = des
     if (des < -3000) this.distance = -600
     if (des > -600) this.distance = -3000
    }
   }, 20)
  },
  jump(index) {
   const direction = index - this.currentIndex >= 0 ? -1 : 1;
   const offset = Math.abs(index - this.currentIndex) * 600;
   const jumpSpeed = Math.abs(index - this.currentIndex) === 0 ? this.speed : Math.abs(index - this.currentIndex) * this.speed ;
   this.move(offset, direction, jumpSpeed)
  },
  play() {
   if (this.timer) {
    window.clearInterval(this.timer)
    this.timer = null
   }
   this.timer = window.setInterval(() => {
    this.move(600, -1, this.speed)
   }, this.interval)
  },
  stop() {
   window.clearInterval(this.timer)
   this.timer = null
  }
 }
}
</script>

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

Javascript 相关文章推荐
CheckBox 如何实现全选?
Jun 23 Javascript
javascript实现的基于金山词霸网络翻译的代码
Jan 15 Javascript
jQuery中add实现同时选择两个id对象
Oct 22 Javascript
一款由jquery实现的整屏切换特效
Sep 15 Javascript
jQuery中prepend()方法使用详解
Aug 11 Javascript
jQuery实现图片轮播特效代码分享
Sep 15 Javascript
基于jQuery插件实现点击小图显示大图效果
May 11 Javascript
JS 数字转换为大写金额的简单实例
Aug 04 Javascript
js实现九宫格拼图小游戏
Feb 13 Javascript
Vue项目分环境打包的实现步骤
Apr 02 Javascript
Nuxt.js实现一个SSR的前端博客的示例代码
Sep 06 Javascript
element中的$confirm的使用
Apr 26 Javascript
浅谈webpack构建工具配置和常用插件总结
May 11 #Javascript
只有 20 行的 JavaScript 模板引擎实例详解
May 11 #Javascript
ES6使用新特性Proxy实现的数据绑定功能实例
May 11 #Javascript
JavaScript异步操作的几种常见处理方法实例总结
May 11 #Javascript
Nuxt默认模板、默认布局和自定义错误页面的实现
May 11 #Javascript
Vue.js获取手机系统型号、版本、浏览器类型的示例代码
May 10 #Javascript
vue总线机制(bus)知识点详解
May 10 #Javascript
You might like
PHP学习之正则表达式
2011/04/17 PHP
php从数据库中获取数据用ajax传送到前台的方法
2018/08/20 PHP
使用Jquery实现每日签到功能
2015/04/03 Javascript
JS实现跟随鼠标的链接文字提示框效果
2015/08/06 Javascript
ajax如何实现页面局部跳转与结果返回
2015/08/24 Javascript
JS中this上下文对象使用方式
2016/10/09 Javascript
基于NodeJS+MongoDB+AngularJS+Bootstrap开发书店案例分析
2017/01/12 NodeJs
详解vue2.0组件通信各种情况总结与实例分析
2017/03/22 Javascript
详解Vue.js基于$.ajax获取数据并与组件的data绑定
2017/05/26 Javascript
vue.extend实现alert模态框弹窗组件
2018/04/28 Javascript
javaScript强制保留两位小数的输入数校验和小数保留问题
2018/05/09 Javascript
浅谈vux之x-input使用以及源码解读
2018/11/04 Javascript
微信小程序websocket聊天室的实现示例代码
2019/02/12 Javascript
如何通过setTimeout理解JS运行机制详解
2019/03/23 Javascript
js实现烟花特效
2020/03/02 Javascript
JQuery复选框全选效果如何实现
2020/05/08 jQuery
Python iter()函数用法实例分析
2018/03/17 Python
python中实现字符串翻转的方法
2018/07/11 Python
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2019/01/09 Python
Python实现的列表排序、反转操作示例
2019/03/13 Python
Django中提示消息messages的设置方式
2019/11/15 Python
详解基于python的图像Gabor变换及特征提取
2020/10/26 Python
浅谈盘点5种基于Python生成的个性化语音方法
2021/02/05 Python
python pyg2plot的原理知识点总结
2021/02/28 Python
几道Web/Ajax的面试题
2016/11/05 面试题
MYSQL相比于其他数据库有哪些特点
2013/07/19 面试题
行政文员实习自我鉴定范文
2014/09/14 职场文书
出国签证在职证明
2014/09/20 职场文书
领导班子整改措施
2014/10/24 职场文书
客服专员岗位职责范本
2015/04/07 职场文书
廉政承诺书2015
2015/04/28 职场文书
中国古代史学名著《战国策》概述
2019/08/09 职场文书
深入理解python多线程编程
2021/04/18 Python
分享几个JavaScript运算符的使用技巧
2021/04/24 Javascript
MySQL之PXC集群搭建的方法步骤
2021/05/25 MySQL
Flask搭建一个API服务器的步骤
2021/05/28 Python