欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue + ts實現(xiàn)輪播插件的示例

 更新時間:2020年11月10日 11:28:21   作者:jiwenjie  
這篇文章主要介紹了Vue + ts實現(xiàn)輪播插件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下

背景

最近在學(xué)習(xí) ts,打算用 ts 寫一個練手項目,參照的網(wǎng)站內(nèi)容是 wanandroid,這個接觸過android開發(fā)的同學(xué)可能更i了解一些,其實一開始是打算后臺全部都自己寫的,不過奈何一個懶字,所以現(xiàn)在的打算就是自己實現(xiàn)登錄注冊簡單的邏輯。這些都不重要,一開始實現(xiàn)輪播是打算在 vue 中引入輪播圖 swiper.js,后來想想還是自己寫算了。也當(dāng)作熟悉 ts。先上效果圖(這里沒有動態(tài)圖片,各位同學(xué)可以自己實現(xiàn))

代碼已經(jīng)上傳 git,進(jìn)度比較慢,如果可以各位大佬點個 star。 github.com/jiwenjie/vu…

<!-- vue 實現(xiàn)輪播圖 -->
<template>
 <div id="swiperDIV" :style="{height: height + 'px'}" @mouseover="suspend" @mouseout="autoPlay" @blur="suspend"
  @focus="autoPlay">
  <!-- 淡入淡出效果 -->
  <transition-group tag="ul" class="img-list" :name="animation">
   <li v-for="(item, index) in bannerList" :key="item.id" v-show="curIndex === index">
    <img :src="item[nameField]">
   </li>
  </transition-group>
  <!-- 操作按鈕部分(底部導(dǎo)航器) -->
  <ul class="option-list" v-if="showPagination">
   <li class="option-list-item" :class="curIndex === index ? 'cur-option-style':''"
    v-for="(item, index) in bannerList" :key="item.id" @click="jump(item, index)"></li>
  </ul>

  <!-- 左側(cè)右側(cè)切換按鈕 -->
  <template v-if="showBtn">
   <div class="common-btn-space pre-btn-space">
    <span class="common-btn-span pre-btn-span"></span>
   </div>
   <div class="common-btn-space next-btn-space">
    <span class="common-btn-span next-btn-span"></span>
   </div>
  </template>
 </div>
</template>

<!-- ts 文件拆分 -->
<script lang="ts">
 // 兩種動畫背景
 import {
  Component,
  Prop,
  Vue
 } from 'vue-property-decorator'
 import swiper from './ts/swiper'

 @Component({
  name: 'Swiper',
  mixins: [swiper],
 })
 export default class extends Vue {}

</script>

<!-- 樣式文件拆分 -->
<style lang="scss" scoped>
 @import url("./css/swiper.scss");

</style>

ts文件

import {
 Component,
 Prop,
 Vue
} from 'vue-property-decorator'
import { Banner } from '@/beans/index' // 首頁banner圖
@Component({
 name: 'Swiper',
 components: {},
})
export default class IndexPage extends Vue {
 @Prop({ default: 6000 }) private timeout: number; // 默認(rèn)的切換banner圖的時長
 @Prop({ default: 400 }) private height: number | string; // banner區(qū)域高度
 @Prop({ default: () => [] }) private bannerList: Banner[]; // 傳入的圖片數(shù)組
 @Prop({ default: 'imagePath' }) private nameField: string; // 圖片地址對應(yīng)的字段名
 @Prop({ default: true }) private showPagination: boolean; // 是否顯示底部原點分頁器
 @Prop({ default: false }) private showBtn: boolean; // 是否顯示左右的切換按鈕
 @Prop({
  default: 'fade', validator: function (value) {
   let arr = ['fade', 'translate']
   return arr.includes(value);
 } }) private animation: string; // 是否顯示左右的切換按鈕

 private timer: any;
 private curIndex: number = 0;

 created(): void {
  this.autoPlay()
 }

 // lifecycle hook
 mounted(): void {

 }

 // method
 private handleSelect() {

 }

 // 自動播放圖片
 private autoPlay() {
  clearInterval(this.timer)//還是一樣,開啟定時器之前需要先清除一下,防止bug
  this.timer = setInterval(this.nextClick, this.timeout as number)
 }

 // 切換下一個 banner 圖片
 private nextClick() {
  this.curIndex++;
  if (this.curIndex >= this.bannerList.length) {
   this.curIndex = 0;
  }
 }

 // 切換上一個圖片
 private preClick() {
  this.curIndex++;
  if (this.curIndex >= this.bannerList.length) {
   this.curIndex = 0;
  }
 }

 // 暫停的方法
 private suspend() {
  clearInterval(this.timer)
 }

 // 點擊底部原點按鈕調(diào)整方法
 private jump(bannerItem: Banner, index: number) {
  this.curIndex = index;
 }

 // private animationMethodValidator(): string {

 // }
}

css文件

#swiperDIV {
 position: relative;
 display: block;
 width: 100%;
}

.img-list {
 width: 100%;
 height: 100%;
 position: relative;
 margin: 0;
 padding: 0;
 z-index: 9;
}

.img-list li {
 position: absolute;
 left: 0;
 width: 100%;
 height: 100%;
}

.img-list img {
 width: 100%;
 height: 100%;
}

.option-list {
 position: absolute;
 left: 0;
 right: 0;
 bottom: 10px;
 height: 30px;
 line-height: 30px;
 z-index: 99;
 text-align: center;
}

.option-list-item {
 display: inline-block;
 background-color: rgba(255, 255, 255, .4);
 width: 10px;
 height: 10px;
 border-radius: 50%;
 margin: 0 3px;
 cursor: pointer;
}

.cur-option-style {
 background-color: #fff;
}

.common-btn-space {
 position: absolute;
 top: 0;
 bottom: 0;
 z-index: 99;
 width: 22px;
}

.pre-btn-space {
 left: 20px;
}

.next-btn-space {
 right: 20px;
}

.common-btn-span {
 display: inline-block;
 width: 22px;
 height: 22px;
 background-color: transparent;
 cursor: pointer;
 position: absolute;
 top: 0;
 bottom: 0;
 margin: auto;
 border-top: 2px solid transparent;
 border-right: 2px solid transparent;
 border-bottom: 2px solid red;
 border-left: 2px solid red;
}

.pre-btn-span {
 transform: rotate(45deg);
}

.next-btn-span {
 transform: rotate(-135deg);
}

/* 實現(xiàn)動畫的兩組類(淡入淡出) */
.fade-enter-active,
.fade-leave-active {
 transition: opacity .6s;
}

.fade-enter,
.fade-leave-to {
 opacity: 0;
}

/* (滾動) */
.translate-enter {
 transform: translateX(100%)
}

.translate-enter-to {
 transition: all .6s ease;
 transform: translateX(0);
}

.translate-leave {
 transform: translateX(0)
}

.translate-leave-active {
 transition: all .6s ease;
 transform: translateX(-100%)
}

很多地方做了配置,包括底部的分頁器,左右兩側(cè)的按鈕。動畫目前只實現(xiàn)了兩種,一種是淡入淡出,一種是平滑滾動。

這里我把整個 vue 文件做了拆分。有多種原因,一個是我司做項目時整體就是這種拆分,不過我司用的就是正常的 vue 和 js。主要原因就是考慮到頁面復(fù)雜和邏輯交互很多的時候,一個 vue 文件可能超過萬行,所以做了拆分,這里我也延續(xù)了這種寫法,基本思想其實就是使用 mixins 引入 ts。還有一個原因是 ts 在 vue 文件中會有很多莫名其妙的報錯,明明代碼沒問題,但就是有錯誤警告。如果提到 ts 文件中就正常,這也是我拆分的一個原因。

其他的功能可以自己在加,如果有問題可以留言,有錯誤希望各位大佬指正。

以上就是Vue + ts實現(xiàn)輪播插件的示例的詳細(xì)內(nèi)容,更多關(guān)于Vue + ts 輪播插件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue3實現(xiàn)按鈕權(quán)限管理的項目實踐

    vue3實現(xiàn)按鈕權(quán)限管理的項目實踐

    在做后臺管理系統(tǒng)時,經(jīng)常會有權(quán)限管理的功能,本文主要介紹了vue3實現(xiàn)按鈕權(quán)限管理的項目實踐,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • vue腳手架vue-cli的卸載與安裝方式

    vue腳手架vue-cli的卸載與安裝方式

    pm是nodejs的包管理和分發(fā)工具,它可以讓javascript開發(fā)者能夠更加輕松的共享代碼和共用代碼片段,下面這篇文章主要給大家介紹了關(guān)于vue腳手架vue-cli卸載與安裝的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • vue-router中 query傳參和params傳參的使用和區(qū)別講解

    vue-router中 query傳參和params傳參的使用和區(qū)別講解

    這篇文章主要介紹了vue-router中 query傳參和params傳參的使用和區(qū)別講解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • vuejs+element-ui+laravel5.4上傳文件的示例代碼

    vuejs+element-ui+laravel5.4上傳文件的示例代碼

    本篇文章主要介紹了vuejs+element-ui+laravel5.4上傳文件的示例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • nuxt使用vuex存儲及獲取用戶信息踩坑的解決

    nuxt使用vuex存儲及獲取用戶信息踩坑的解決

    這篇文章主要介紹了nuxt使用vuex存儲及獲取用戶信息踩坑的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue打開子組件彈窗都刷新功能的實現(xiàn)

    vue打開子組件彈窗都刷新功能的實現(xiàn)

    這篇文章主要介紹了vue打開子組件彈窗都刷新功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • vue自定義指令實現(xiàn)方法詳解

    vue自定義指令實現(xiàn)方法詳解

    這篇文章主要介紹了vue自定義指令實現(xiàn)方法,結(jié)合實例形式分析了vue.js自定義指令相關(guān)定義、注冊、使用方法及操作注意事項,需要的朋友可以參考下
    2019-02-02
  • Vue實現(xiàn)簡易記事本功能

    Vue實現(xiàn)簡易記事本功能

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)簡易記事本功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • vue?動態(tài)添加el-input的實現(xiàn)邏輯

    vue?動態(tài)添加el-input的實現(xiàn)邏輯

    這篇文章主要介紹了vue?動態(tài)添加el-input的實現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 一文詳解Vue3中的setup函數(shù)的用法和原理

    一文詳解Vue3中的setup函數(shù)的用法和原理

    在 Vue3 中,setup 函數(shù)是一個新引入的概念,它代替了之前版本中的 data、computed、methods 等選項,用于設(shè)置組件的初始狀態(tài)和邏輯,本文將主要介紹Setup的基本用法和少量原理
    2024-02-02

最新評論