Vue实现图片轮播组件思路及实例解析
作者:指间流逝的夏末 发布时间:2023-07-02 17:02:02
标签:Vue,图片,轮播,组件
1、先看效果:
熟悉的图片轮播,只要是个网站,百分之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>
来源:https://www.cnblogs.com/zhangyongl/p/8870064.html


猜你喜欢
- 哪行哪业都少不了基本功,都说“马步”要扎得稳。在都快说烂了的以目标用户为中心设计的今天,还是要勤练基本功的。不多说了,先了解下“设计的3个C
- 在本节中,您将添加一些类,这些类用于管理数据库中的电影。这些类是ASP.NET MVC 应用程序中的"模型(Model)"
- 学校让我们在放假期间自觉Python,对于Python我是小白的不能再小白了。一切从头开始,找学习资料,看视频教程光看书看视频也不行还要自己
- 如下所示:import cv2vidcap = cv2.VideoCapture('005.avi')success,ima
- 可匹配单行,也支持换行匹配[\s\S]*?加上括号,效果更好([\s\S]*?)来源:https://blog.csdn.net/ASUKA
- 1 abstract验证码作为一种自然人的机器人的判别工具,被广泛的用于各种防止程序做自动化的场景中。传统的字符型验证安全性已经名存实亡的情
- 绪论java默认精度是毫秒级别的,生成的时间戳是13位,而python默认是10位的,精度是秒。那么python是如何生成13位时间戳,以及
- 如何用拦截表单的方法上传图片? 看下列代码:<script language="
- 快速入门模块提供三个类来处理一对一映射类型的一些操作'bidict', 'inverted', 'n
- jupyter notebook其实是可以使用 argparse来调用参数的,只要把参数转为list即可。不能在jupyter notebo
- 本文以实例形式讲述了ASP.NET实现图片以二进制的形式存入数据库的方法。过去我们都是直接在数据库中存入图片文件名的,还没有试过存储整张图片
- 本文实例讲述了Python简单生成8位随机密码的方法。分享给大家供大家参考,具体如下:#!/usr/bin/env python# -*-
- 有时在浏览网页时,常常因为网页中的图片文件过大而使下载时间较长,这样还没有下载完,就会有许多浏览者不耐烦地拂袖而去,从而损失了客户流。但要使
- 前言kettle通过域名或者IP连接Oracle,本地不需要安装Oracle,但是要把Oracle的驱动jar包复制到kettle的lib目
- 首先看一下来自Wolfram的定义 马尔可夫链是随机变量{X_t}的集合(t贯穿0,1,..
- 常用的一共有4个方法,如下:1.使用locate()方法普通用法:SELECT`column`from`table`wherelocate(
- 在SQL Server中,TempDB主要负责供下述三类情况使用:内部使用(排序、hash join、
- 第一步:建与mysql同构的oracle数据库实例,并更新数据。 1.期望将my
- 一、http协议无状态问题http协议没有提供多次请求之间的关联功能,协议的本意也并未考虑到多次请求之间的状态维持,每一次请求都被协议认为是
- 在现实的图像操作软件中,经常碰到的不是给出放大多少倍,而是由用户在软件的界面上选择多大的区域,或者选择几个点,那么这样情况下,怎么样来计算出