vue无限轮播插件代码实例

2020-06-16 05:56:57易采站长站整理

思路:

要实现无限轮播,需要在轮播图前后各加一张图片,加在前面的是轮播图的最后一张图片(重复的),加在后面的是轮播图的第一张图片(重复的)。例:


<div class="wrapper-content">
<img class="wrapper-content_img" alt="4" src="img/4.jpg"/>
<img class="wrapper-content_img" alt="1" src="img/1.jpg"/>
<img class="wrapper-content_img" alt="2" src="img/2.jpg"/>
<img class="wrapper-content_img" alt="3" src="img/3.jpg" />
<img class="wrapper-content_img" alt="4" src="img/4.jpg" />
<img class="wrapper-content_img" alt="1" src="img/1.jpg" />
</div>

然后再用left来控制滑动,当顺向到达alt为4的图片时,下一张滑到第六张图片,alt为1,同时改变index为1.然后立即将left移到第二张图片,alt为1那张。这样就不会被察觉

好了,贴代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{margin: 0;padding: 0}
.wrapper{position: relative;overflow: hidden;}
.wrapper-content{position: absolute;left: 0;z-index: 1;}
.wrapper-content_img{border: none;outline:none;float: left}
.wrapper-buttons{position: absolute;width: 100px;height: 20px;text-align: center;bottom: 3px;z-index: 2;}
.wrapper-button{float: left;width: 20px;height: 20px;border-radius: 10px;background: gray;margin: 0 2.5px;cursor: pointer;}
.wrapper-arrow{position: absolute;width: 40px;height:40px;cursor: pointer;background-color: RGBA(0,0,0,.3); color: #fff;display: none;top:50%;line-height: 40px;font-size: 36px;text-align: center;z-index: 2;}
.wrapper:hover .wrapper-arrow{display: block;background-color: rgba(0,0,0,.7);}
.wrapper-prev{left:10px;}
.wrapper-next{right:10px;}
.wrapper_on{background-color: yellow}
.wrapper_trans{transition: left .3s ease}
</style>
</head>
<body>
<div id="app">
<div class="wrapper" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}" @mouseover="stop" @mouseout="play">
<div class="wrapper-content" :class="{wrapper_trans:isTrans}" :style="{width:originalData.img_width*(originalData.num+2)+'px',height:originalData.img_height+'px',left:-originalData.img_width+'px'}" ref="wrapperContent">
<img class="wrapper-content_img" alt="4" src="img/4.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
<img class="wrapper-content_img" alt="1" src="img/1.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>
<img class="wrapper-content_img" alt="2" src="img/2.jpg" :style="{width:originalData.img_width+'px',height:originalData.img_height+'px'}"/>