简单的vue幻灯片
基于 Vue.js 制作的简单幻灯片,包含了自动播放、左右箭头控制、鼠标移入停止自动播放/移出恢复自动播放等基本功能
XML/HTML Code
- <div id="freejs">
- <image-slider class="imageSlider">
- <div class="imageSlider-nav" @mouseover="stopRotation" @mouseout="startRotation">
- <a class="prev fas fa-chevron-left" @click="prev"></a>
- <a class="next fas fa-chevron-right" @click="next"></a>
- </div>
- <transition-group name="fade" tag="div">
- <div class="imageSlider-wrapper" v-for="number in [currentNumber]" :key="number">
- <img class="imageSlider-item" :src="images[currentNumber]" @mouseover="stopRotation" @mouseout="startRotation">
- </div>
- </transition-group>
- </image-slider>
- </div>
JavaScript Code
- new Vue({
- el: '#freejs',
- data: {
- images: [
- 'images/img1.jpg',
- 'images/img2.jpg',
- ],
- currentNumber: 0,
- timer: null
- },
- mounted: function () {
- this.startRotation();
- },
- methods: {
- startRotation: function () {
- this.timer = setInterval(this.prev, 7000);
- },
- stopRotation: function () {
- clearTimeout(this.timer);
- this.timer = null;
- },
- next: function () {
- this.currentNumber = this.currentNumber + 1 == this.images.length ? 0 : this.currentNumber += 1
- },
- prev: function () {
- this.currentNumber = this.currentNumber == 0 ? this.images.length - 1 : this.currentNumber -= 1
- }
- }
- })// JavaScript Document
原文地址:http://www.freejs.net/article_jiaodiantu_742.html