vue使用swiper实现左右滑动切换图片


Posted in Javascript onOctober 16, 2020

本文实例为大家分享了vue使用swiper实现左右滑动切换图片的具体代码,供大家参考,具体内容如下

使用npm 安装vue-awesome-swiper

npm install vue-awesome-swiper --save

在main.js中引用

import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.user(VueAwesomeSwiper)
import 'swiper/dist/css/swiper.css'

在组件中使用

<template>
 <div>
  <label class="timeline">{{ time }}</label>
  <div id="star-pic-vue">
   <template v-if="data">
    <img
     e
     v-for="(item, index) in images"
     :src="item.url"
     :key="index"
     id="contract_url"
     @click="enlargePic(index)"
    />
    <template v-if="isDialogShow"> </template>
    <el-dialog
     :visible.sync="centerDialogVisible"
     width="100%"
     modal
     close-on-click-modal
     custom-class="dialog"
    >
     <swiper :options="swiperOption" ref="mySwiper" style="height: 100%;">
      <swiper-slide v-for="(img, index) in images" :key="index">
       <div class="swiper-zoom-container">
        <img :src="img.url" alt="" />
       </div>
      </swiper-slide>
     </swiper>
    </el-dialog>
   </template>
  </div>
 </div>
</template>
 
<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {
 name: "PictureComponent",
 props: ["data", "maxShow", "time"],
 data() {
  return {
   centerDialogVisible: false,
   showPic: "",
   isDialogShow: false,
   activeIndex: 1,
   startX: 0,
   swiperOption: {
    width: window.innerWidth,
    zoom: true,
    initialSlide: 0
   }
  };
 },
 computed: {
  images() {
   if (this.data instanceof Array && this.data.length > 2) {
    var value = this.data;
    return value.splice(0, this.maxShow);
   } else {
    return this.data;
   }
  }
 },
 components: {
  swiper,
  swiperSlide
 },
 methods: {
  // 放大图片
  enlargePic(i) {
   this.activeIndex = i;
   this.isDialogShow = true;
   // 使用$refs,如果ref是定位在有v-if、v-for、v-show中的DOM节点,
   // 返回来的只能是undefined,因为在mounted阶段他们根本不存在
   this.$nextTick(() => {
    var swiper = this.$refs.mySwiper.swiper;
    swiper.activeIndex = i;
   });
   this.centerDialogVisible = true;
  }
 }
};
</script>
 
<style lang="scss">
.timeline {
 display: block;
 margin: 10px 20px 5px;
}
#star-pic-vue .el-dialog__wrapper {
 position: fixed;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 overflow: auto;
 margin: 0;
 background: #171717;
}
#star-pic-vue {
 width: 100%;
 height: auto;
 display: flex;
 flex-wrap: wrap;
 justify-content: stretch;
 padding: 3px 13px;
 img {
  width: 82px;
  height: 80px;
  margin: 4px 0px 0px;
  padding-right: 2px;
 }
 .dialog {
  img {
   width: 100%;
   height: 100%;
   margin: 0;
  }
 }
 .el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
  height: 100%;
  width: 100%;
 }
 .el-dialog__header {
  display: none;
 }
 .el-dialog__body {
  padding: 0 !important;
  margin: 0 !important;
  height: 460px;
  background: #171717;
 }
 .el-carousel {
  height: 100%;
 }
 .el-carousel__container {
  height: 410px;
 }
 .el-carousel__indicators--outside {
  margin-top: 20px;
 }
}
</style>

效果

vue使用swiper实现左右滑动切换图片

$refs定位不到的主要原因是因为v-if、v-for、v-show这些语句如果依赖父组件传来的参数的话,该该参数是在mounted()阶段子还没获取得到~~~~!!!!

如果想要真正地在DOM加载完成后拿到数据,就需要调用VUE的全局api : this.$nextTick(() => {})

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

Javascript 相关文章推荐
Javascript与vbscript数据共享
Jan 09 Javascript
jQuery简单实现banner图片切换
Jan 02 Javascript
js实现字符串的16进制编码不加密
Apr 25 Javascript
浅析Node.js查找字符串功能
Sep 03 Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
Nov 29 Javascript
jQuery中deferred对象使用方法详解
Jul 14 Javascript
requirejs + vue 项目搭建详解
Jun 16 Javascript
Vue keep-alive实践总结(推荐)
Aug 31 Javascript
JS沙箱模式实例分析
Sep 04 Javascript
javascript用rem来做响应式开发
Jan 13 Javascript
npm 下载指定版本的组件方法
May 17 Javascript
使用Vue实现简单计算器
Feb 25 Javascript
详解webpack的clean-webpack-plugin插件报错
Oct 16 #Javascript
JS绘图Flot如何实现可选显示曲线图功能
Oct 16 #Javascript
JS跨浏览器解析XML应用过程详解
Oct 16 #Javascript
JS实现简易图片自动轮播
Oct 16 #Javascript
JS绘图Flot如何实现动态可刷新曲线图
Oct 16 #Javascript
基于JQuery和DWR实现异步数据传递
Oct 16 #jQuery
JS绘图Flot应用图形绘制异常解决方案
Oct 16 #Javascript
You might like
透析PHP的配置文件php.ini
2006/10/09 PHP
php递归列出所有文件和目录的代码
2008/09/10 PHP
php下清空字符串中的HTML标签的代码
2010/09/06 PHP
浅析php变量作用域的一些问题
2013/08/08 PHP
php一行代码获取文件后缀名实例分析
2014/11/12 PHP
Yii基于数组和对象的Model查询技巧实例详解
2015/12/28 PHP
PHP开发之归档格式phar文件概念与用法详解【创建,使用,解包还原提取】
2017/11/17 PHP
jQuery获取(选中)单选,复选框,下拉框中的值
2014/02/21 Javascript
Angular中的Promise对象($q介绍)
2015/03/03 Javascript
jQuery实现仿路边灯箱广告图片轮播效果
2015/04/15 Javascript
javascript判断数组内是否重复的方法
2015/04/21 Javascript
AngularJs Forms详解及简单示例
2016/09/01 Javascript
JS实现微信弹出搜索框 多条件查询功能
2016/12/13 Javascript
ExtJs异步无法向外传值和赋值的完美解决办法
2017/06/14 Javascript
webpack本地开发环境无法用IP访问的解决方法
2018/03/20 Javascript
vue.js实现的全选与全不选功能示例【基于elementui】
2018/12/03 Javascript
Angular(5.2-&gt;6.1)升级小结
2018/12/27 Javascript
在Layui 的表格模板中,实现layer父页面和子页面传值交互的方法
2019/09/10 Javascript
JavaScript实现省市区三级联动
2020/02/13 Javascript
js实现限定范围拖拽的示例
2020/10/26 Javascript
用ReactJS和Python的Flask框架编写留言板的代码示例
2015/12/19 Python
python引入导入自定义模块和外部文件的实例
2017/07/24 Python
pandas获取groupby分组里最大值所在的行方法
2018/04/20 Python
Tensorflow使用tfrecord输入数据格式
2018/06/19 Python
pytorch实现onehot编码转为普通label标签
2020/01/02 Python
一文读懂Python 枚举
2020/08/25 Python
美津浓巴西官方网站:Mizuno巴西
2019/07/24 全球购物
NOTINO英国:在线购买美容和香水
2020/02/25 全球购物
什么是Smarty变量操作符?如何使用Smarty变量操作符
2014/07/18 面试题
电子商务专业推荐信范文
2013/12/02 职场文书
普通话宣传标语
2014/06/26 职场文书
幼儿园国庆节活动总结
2015/03/23 职场文书
全国助残日活动总结
2015/05/11 职场文书
MySQL基础(二)
2021/04/05 MySQL
MySQL中的布尔值,怎么存储false或true
2021/06/04 MySQL
利用 JavaScript 构建命令行应用
2021/11/17 Javascript