mint ui : swipe swipe-item -----轮播图的实现 mint-ui swipe组件源码解析

https://www.vue-js.com/topic/5a4379dc9904b1140d0950eb
https://blog.csdn.net/u010014658/article/details/73605167

<div class="dev-nav-wrap" id="dev-nav-wrap">
  <mt-swipe :auto="0" :continuous="false" :speed="100">
    <mt-swipe-item>
      <ul>
        <li>
          <router-link :to="{name: 'elecMotoMapLocation', params: {userInfoList: userInfoList, clientObj: client}}">
            <img src="../../assets/images/icon_cheliangwz.png"/>
            <span>位置信息</span>
          </router-link>
        </li>
        <li>
          <a href="javascript:;" v-on:click="cancelPrevent">
            <div v-if="preventStatus">
              <img src="../../assets/images/icon_chefang.png"/>
              <span>撤防</span>
            </div>
            <div v-else>
              <img src="../../assets/images/icon_shefang.png"/>
              <span>设防</span>
            </div>
          </a>
        </li>
        <li>
          <a href="javascript:;" v-on:click="getSyncData">
            <img src="../../assets/images/icon_genxsj.png"/>
            <span>更新数据</span>
          </a>
        </li>
        <li>
          <router-link :to="{name: 'carCheck', params: {clientObj: client}}">
            <img src="../../assets/images/icon_chekuangxx.png"/>
            <span>车辆自检</span>
          </router-link>
        </li>
        <li>
          <a href="javascript:;" v-on:click="startCar">
            <div v-if="startStatus">
              <img src="../../assets/images/icon_shangs.png"/>
              <span>上锁</span>
            </div>
            <div v-else>
              <img src="../../assets/images/icon_qidong.png"/>
              <span>启动</span>
            </div>
          </a>
        </li>
        <li>
          <router-link :to="{name: 'setElecFence'}">
            <img src="../../assets/images/icon_anquanweil.png"/>
            <span>电子围栏</span>
          </router-link>
        </li>
      </ul>
    </mt-swipe-item>
    <mt-swipe-item>
      <ul>
        <li>
          <a href="javascript:;" id="contactDevBtn2">
            <img src="../../assets/images/icon_shebxx.png"/>
            <span>设备消息</span>
          </a>
        </li>
      </ul>
    </mt-swipe-item>
  </mt-swipe>


</div>

import { Toast, MessageBox, Indicator, Button, Swipe, SwipeItem } from 'mint-ui'



vue.js整合mint-ui里的轮播图
 发布于 6个月前  作者  tomoya92  1499 次浏览  来自 分享

初始化vue项目

npm install -g vue-cli
vue init webpack demo # 中间会让你选npm yarn 等来安装依赖,我选的是yarn,因为它快些

安装mint-ui

yarn add mint-ui

mint-ui装好了,还要配置一下babel,方法跟着mint-ui的官方文档来配置就可以了

下面是我配置好的 .babelrc 文件,启动的时候会报跟es2015相关的错,装一下babel-preset-es2015就好了

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2",
    ["es2015", { "modules": false }]
  ],
  "plugins": [["component", [
    {
      "libraryName": "mint-ui",
      "style": true
    }
  ]],"transform-vue-jsx", "transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2", "es2015"],
      "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}

集成

打开创建的vue项目demo,在src里找到components/HelloWorld.vue文件,然后将内容换成下面内容

<template>
  <div>
    <mt-swipe :auto="2000">
      <mt-swipe-item v-for="item in items" :key="item.id">
        <a :href="item.href">
          <img :src="item.url" class="img"/>
          <span class="desc">{{item.title}}</span>
        </a>
      </mt-swipe-item>
    </mt-swipe>
  </div>
</template>

<script>
  import {Swipe, SwipeItem} from 'mint-ui'
  import 'mint-ui/lib/style.css'

  export default {
    components: {
      'mt-swipe': Swipe,
      'mt-swipe-item': SwipeItem
    },
    data () {
      return {
        items: [{
          title: '你的名字',
          href: 'http://google.com',
          url: 'http://localhost:8080/static/img1.png'
        }, {
          title: '我的名字',
          href: 'http://baidu.com',
          url: 'http://localhost:8080/static/img2.png'
        }]
      }
    }
  }
</script>

<style scoped>
  img {
    width: 100%;
  }
  .mint-swipe {
    height: 218px;
  }
  .desc {
    font-weight: 600;
    opacity: .9;
    padding: 5px;
    height: 20px;
    line-height: 20px;
    width: 100%;
    color: #fff;
    background-color: gray;
    position: absolute;
    bottom: 0;
  }
</style>

找两张图片,名字分别是 img1.png, img2.png, 放在demo项目的static里,然后启动项目

npm run dev

打开浏览器:http://localhost:8080/

注意

  1. 如果发现文字都是居中的

可以找到文件 App.vue 把里面的居中css代码去掉就好了

  1. 如果页面有内边距

设置一下 body 的样式 margin: 0 auto;

  1. 页面里用的时候,必须要给类样式一个高度,要不然图片不出来 .mint-swipe { height: 218px; }



mint-ui swipe组件源码解析

前叙


mint-ui组件库中swipe组件,实现的是常见的轮播图效果。但是它的实现方式,和常见的实现有所不同。 
常见的实现方式: 通过移动轮播图的wrapper来实现item的切换效果(也就是修改wrapper的translate3d属性来实现)。如果支持循环播放,需要在首部插入一个最后一个轮播图item的clone版,以及在尾部插入一个第一个轮播图item的clone版。 
swipe组件实现的方式: 只显示当前显示的轮播图item,当切换的时候,显示出当前item的前后相邻的两个item;通过设置三个item的translate3d来实现切换的效果。 
两个实现方式的对比:

  • 第一种方式,初始会渲染出所有的item,通过translate3d来实现切换和滑动,这种方式会启动硬件加速提升性能。但是毕竟是在所有轮播图的基础上的渲染。
  • 第二种方式,通过切换item的display属性来实现对应item的显示和隐藏,虽然会引起回流和重绘,但是每个item的position为absolute,脱离文档流,所以并不会引起其他dom的回流和重绘。每个item的translate3d引发的渲染只是在当前item的基础上。
  • 通过上面分析,可以得出: 如果轮播图的数量不多,第一种方式不会引起回流和重绘,并且translate引发渲染的item不多,性能相对好;但是轮播图的数量比较多的话,第二种性能相对比较好。

swipe接入示例


  • html代码

    <div id="app">
        <div class="swipe-wrapper">
            <mt-swipe :auto="0" ref="swipeWrapper">
                <mt-swipe-item class="swip-item-1 item">1</mt-swipe-item>
                <mt-swipe-item class="swip-item-2 item">2</mt-swipe-item>
                <mt-swipe-item class="swip-item-3 item">3</mt-swipe-item>
            </mt-swipe>
        </div>
    
        <div class="button-wrapper">
            <button class="prev-button flex-item" @click="prev">prev</button>
            <button class="next-button flex-item" @click="next">next</button>
        </div>
    </div>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • css代码

    <!-- 引入组件库css -->
    <link rel="stylesheet" href="../css/mint-style.css">
    <style>
        html,body{
            width: 100%;
            height: 100%;
            margin: 0;
        }
        #app{
            width: 100%;
            height: 100%;
        }
        .swipe-wrapper{
            width: 100%;
            height: 300px;
        }
        .swip-item-1{
            background: red;
        }
        .swip-item-2{
            background: blue;
        }
        .swip-item-3{
            background: green;
        }
        .item{
            text-align: center;
            font-size: 40px;
            color: white;
        }
    
        .button-wrapper{
            display: flex;
            height: 100px;
        }
        .flex-item{
            flex: 1;
            display: inline-block;
            text-align: center;
            height: 100%;
            line-height: 100%;
            font-size: 40px;
        }
        .prev-button{
            background: darkorange;
        }
        .next-button{
            background: green;
        }
    
    </style>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • js代码

    <!-- 先引入 Vue -->
    <script src="../js/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../js/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            methods: {
                prev: function () {
                    this.$refs.swipeWrapper.prev();
                    console.log(this.$children);
                },
                next: function () {
                    this.$refs.swipeWrapper.next();
                }
            }
        });
    </script>

前叙


mint-ui组件库中swipe组件,实现的是常见的轮播图效果。但是它的实现方式,和常见的实现有所不同。 
常见的实现方式: 通过移动轮播图的wrapper来实现item的切换效果(也就是修改wrapper的translate3d属性来实现)。如果支持循环播放,需要在首部插入一个最后一个轮播图item的clone版,以及在尾部插入一个第一个轮播图item的clone版。 
swipe组件实现的方式: 只显示当前显示的轮播图item,当切换的时候,显示出当前item的前后相邻的两个item;通过设置三个item的translate3d来实现切换的效果。 
两个实现方式的对比:

  • 第一种方式,初始会渲染出所有的item,通过translate3d来实现切换和滑动,这种方式会启动硬件加速提升性能。但是毕竟是在所有轮播图的基础上的渲染。
  • 第二种方式,通过切换item的display属性来实现对应item的显示和隐藏,虽然会引起回流和重绘,但是每个item的position为absolute,脱离文档流,所以并不会引起其他dom的回流和重绘。每个item的translate3d引发的渲染只是在当前item的基础上。
  • 通过上面分析,可以得出: 如果轮播图的数量不多,第一种方式不会引起回流和重绘,并且translate引发渲染的item不多,性能相对好;但是轮播图的数量比较多的话,第二种性能相对比较好。

swipe接入示例


  • html代码

    <div id="app">
        <div class="swipe-wrapper">
            <mt-swipe :auto="0" ref="swipeWrapper">
                <mt-swipe-item class="swip-item-1 item">1</mt-swipe-item>
                <mt-swipe-item class="swip-item-2 item">2</mt-swipe-item>
                <mt-swipe-item class="swip-item-3 item">3</mt-swipe-item>
            </mt-swipe>
        </div>
    
        <div class="button-wrapper">
            <button class="prev-button flex-item" @click="prev">prev</button>
            <button class="next-button flex-item" @click="next">next</button>
        </div>
    </div>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • css代码

    <!-- 引入组件库css -->
    <link rel="stylesheet" href="../css/mint-style.css">
    <style>
        html,body{
            width: 100%;
            height: 100%;
            margin: 0;
        }
        #app{
            width: 100%;
            height: 100%;
        }
        .swipe-wrapper{
            width: 100%;
            height: 300px;
        }
        .swip-item-1{
            background: red;
        }
        .swip-item-2{
            background: blue;
        }
        .swip-item-3{
            background: green;
        }
        .item{
            text-align: center;
            font-size: 40px;
            color: white;
        }
    
        .button-wrapper{
            display: flex;
            height: 100px;
        }
        .flex-item{
            flex: 1;
            display: inline-block;
            text-align: center;
            height: 100%;
            line-height: 100%;
            font-size: 40px;
        }
        .prev-button{
            background: darkorange;
        }
        .next-button{
            background: green;
        }
    
    </style>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • js代码

    <!-- 先引入 Vue -->
    <script src="../js/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../js/index.js"></script>
    <script>
        new Vue({
            el: '#app',
            methods: {
                prev: function () {
                    this.$refs.swipeWrapper.prev();
                    console.log(this.$children);
                },
                next: function () {
                    this.$refs.swipeWrapper.next();
                }
            }
        });
    </script>

猜你喜欢

转载自blog.csdn.net/zgpeterliu/article/details/80803878