vue.js实现的幻灯片功能示例


Posted in Javascript onJanuary 18, 2019

本文实例讲述了vue.js实现的幻灯片功能。分享给大家供大家参考,具体如下:

1、在父组件中

<slide-show :slides="slides"></slide-show>
import SlideShow from '@/components/SlideShow'
export default {
 components: {
  SlideShow,
 },

2、在slideshow.vue中

<template>
  <div class="slide-show" @mouseover="clearInv" @mouseout="runInv">  // 当鼠标移入的时候清除,移出的时候
    <div class="slide-img">
      <a href="slides[nowIndex].href" rel="external nofollow" >
      <transition name="slide-trans">  // 使用动画
         <img v-if="isShow" :src="slides[nowIndex].src">
        </transition>
        <transition name="slide-trans-old">
         <img v-if="!isShow" :src="slides[nowIndex].src">
        </transition>
      </a>
    </div>
    <h2>{{ slides[nowIndex].title }}</h2>
    <ul class="slide-pages">
      <li @click="goto(prevIndex)"><</li>
      <li v-for="(item, index) in slides" @click="goto(index)">
        <a :class="{ on: index === nowIndex}">
          {{ index + 1 }}
        </a>
      </li>
      <li @click="goto(nextIndex)">></li>
    </ul>
  </div>
</template>
<script>
  export default {
    props: {
      slides: {  // 获取父组件的属性
        type: Array,
        default: []
      },
      inv: {
        type: Number,
        default: 1000
      }
    },
    data () {
      return {
        nowIndex: 0,
        isShow: true
      }
    },
    computed: {
      prevIndex () {  // 使用计算属性,
        if (this.nowIndex === 0) {
          return this.slides.length - 1
        } else {
          return this.nowIndex - 1
        }
      },
      nextIndex () {
        if (this.nowIndex === this.slides.length - 1) {
          return 0
        } else {
          return this.nowIndex + 1
        }
      }
    },
    methods: {
      goto (index) {
        this.isShow = false,
        setTimeout(() => {       // 过10毫秒后,
          this.isShow = true,
          this.nowIndex = index
        }, 10)
      },
      runInv () {         // 设置定时器
        this.timer = setInterval(() => {
          this.goto(this.nextIndex)
        }, this.inv)
      },
      clearInv () {
        clearInterval(this.timer)
      }
    },
    mounted () {     // 加载完后执行
      this.runInv()
    }
  }
</script>
<style scoped>
.slide-trans-enter-active {
 transition: all .5s;
}
.slide-trans-enter {
 transform: translateX(900px);
}
.slide-trans-old-leave-active {
 transition: all .5s;
 transform: translateX(-900px);
}
.slide-show {
 position: relative;
 margin: 15px 15px 15px 0;
 width: 900px;
 height: 500px;
 overflow: hidden;
}
.slide-show h2 {
 position: absolute;
 width: 100%;
 height: 100%;
 color: #fff;
 background: #000;
 opacity: .5;
 bottom: 0;
 height: 30px;
 text-align: left;
 padding-left: 15px;
}
.slide-img {
 width: 100%;
}
.slide-img img {
 width: 100%;
 position: absolute;
 top: 0;
}
.slide-pages {
 position: absolute;
 bottom: 10px;
 right: 15px;
}
.slide-pages li {
 display: inline-block;
 padding: 0 10px;
 cursor: pointer;
 color: #fff;
}
.slide-pages li .on {
 text-decoration: underline;
}
</style>

希望本文所述对大家vue.js程序设计有所帮助。

Javascript 相关文章推荐
JavaScript初学者需要了解10个小技巧
Aug 25 Javascript
js 判断脚本加载完毕的代码
Jul 13 Javascript
JQUERY dialog的用法详细解析
Dec 19 Javascript
jQuery选择器简明总结(含用法实例,一目了然)
Apr 25 Javascript
js获取内联样式的方法
Jan 27 Javascript
基于jquery步骤进度条源码分享
Nov 12 Javascript
解决Angular.Js与Django标签冲突的方案
Dec 20 Javascript
js正则相关知识点专题
May 10 Javascript
vue-cli脚手架搭建的项目去除eslint验证的方法
Sep 29 Javascript
React中阻止事件冒泡的问题详析
Apr 12 Javascript
小程序云函数调用API接口的方法
May 17 Javascript
vue.js自定义组件实现v-model双向数据绑定的示例代码
Jan 08 Javascript
vue ssr 实现方式(学习笔记)
Jan 18 #Javascript
JS实现的贪吃蛇游戏完整实例
Jan 18 #Javascript
jquery的$().each和$.each的区别
Jan 18 #jQuery
使用form-create动态生成vue自定义组件和嵌套表单组件
Jan 18 #Javascript
jquery层次选择器的介绍
Jan 18 #jQuery
js实现图片放大并跟随鼠标移动特效
Jan 18 #Javascript
vue.js的双向数据绑定Object.defineProperty方法的神奇之处
Jan 18 #Javascript
You might like
PHP网上调查系统
2006/10/09 PHP
php在页面中调用fckeditor编辑器的方法
2011/06/10 PHP
Pain 全世界最小最简单的PHP模板引擎 (普通版)
2011/10/23 PHP
PHP与MYSQL中UTF8 中文排序示例代码
2014/10/23 PHP
从零开始学习jQuery (二) 万能的选择器
2010/10/01 Javascript
jquery索引在使用中的一些困惑
2013/10/24 Javascript
JSONP获取Twitter和Facebook文章数的具体步骤
2014/02/24 Javascript
JavaScript每天定时更换皮肤样式的方法
2015/07/01 Javascript
jQuery实现的简单提示信息插件
2015/12/08 Javascript
JavaScript正则表达式匹配 div  style标签
2016/03/15 Javascript
jQuery自定义组件(导入组件)
2016/11/08 Javascript
使用jquery实现的循环连续可停顿滚动实例
2016/11/23 Javascript
angular实现表单验证及提交功能
2017/02/01 Javascript
Vue2.0实现将页面中表格数据导出excel的实例
2017/08/09 Javascript
浅谈jquery中ajax跨域提交的时候会有2次请求的问题
2017/11/10 jQuery
vue2.0 如何把子组件的数据传给父组件(推荐)
2018/01/15 Javascript
详解vue中使用transition和animation的实例代码
2020/12/12 Vue.js
python获取远程图片大小和尺寸的方法
2015/03/26 Python
简单的Python的curses库使用教程
2015/04/11 Python
在Python中操作字符串之replace()方法的使用
2015/05/19 Python
简单学习Python多进程Multiprocessing
2017/08/29 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
python使用百度文字识别功能方法详解
2019/07/23 Python
dpn网络的pytorch实现方式
2020/01/14 Python
浅谈keras使用中val_acc和acc值不同步的思考
2020/06/18 Python
Python二元算术运算常用方法解析
2020/09/15 Python
图解CSS3制作圆环形进度条的实例教程
2016/05/26 HTML / CSS
canvas绘制表情包的示例代码
2018/07/09 HTML / CSS
详解移动端html5页面长按实现高亮全选文本内容的兼容解决方案
2016/12/03 HTML / CSS
中国旅游网站:途牛旅游网
2019/09/29 全球购物
俄罗斯极限运动网上商店:Board Shop №1
2020/12/18 全球购物
新品发布会主持词
2014/04/02 职场文书
降价通知函
2015/04/23 职场文书
干货:如何写好观后感 !
2019/05/21 职场文书
毕业欢送晚会主持词
2019/06/25 职场文书
历史名人教你十五个读书方法,赶快Get起来!
2019/07/18 职场文书