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

Vue左滑組件slider使用詳解

 更新時(shí)間:2020年08月21日 11:30:00   作者:latency_cheng  
這篇文章主要為大家詳細(xì)介紹了Vue左滑組件slider的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

slider組件與swiper組件不同,slider滑動(dòng)時(shí)并不翻頁(yè),實(shí)現(xiàn)的是左滑時(shí),顯示右側(cè)內(nèi)容的功能

1、主要思路

思路和swiper組件類似,主要的也就是對(duì)三個(gè)觸摸事件的處理:touchstart、touchmove、touchend

在touchstart事件處理程序中記錄一些初始值,比如原始坐標(biāo),偏移距離;在touchmove事件處理程序中計(jì)算實(shí)時(shí)滑動(dòng)的距離,讓元素隨之一起偏移,與swiper不同的是,slider在左滑之前,不能右滑,以及滑動(dòng)時(shí),右側(cè)元素的寬度要同步變化;在touchend事件處理程序中計(jì)算最終的滑動(dòng)距離,左滑且大于闕值則滑動(dòng)固定值,右滑或小于闕值則回到起始位置,右側(cè)元素的寬度要同步變化。

slider組件可以接收三個(gè)參數(shù):

rightWidth: 右側(cè)滑出寬度的百分比

isClickBack: 點(diǎn)擊是否收起右側(cè)

isMainSlide: 左側(cè)是否滑動(dòng)(false則覆蓋左側(cè))

2、代碼實(shí)現(xiàn)

頁(yè)面結(jié)構(gòu):有兩個(gè)slot來展示左右兩邊的內(nèi)容

<template>
 <div class="ths_slider" ref="slider">
 <div class="main"
  @touchstart="touchstart"
  @touchmove="touchmove"
  @touchend="touchend">
  <slot name="main"></slot>
 </div>
 <div class="right" ref="right">
  <slot name="right"></slot>
 </div>
 </div>
</template>

初始設(shè)置:記錄頁(yè)面寬度,根據(jù)參數(shù)isMainSlide來判斷滑動(dòng)的元素

mounted () {
 this.pageWidth = document.documentElement.clientWidth
 this.sliderEle = this.isMainSlide ? this.$refs.slider : this.$refs.right
}

事件處理:

touchstart (e) {
 this.originalPos = e.touches[0].pageX
 const transform = this.sliderEle.style.transform
 this.originalLeft = Number(transform ? transform.split('(')[1].split('px')[0] : 0)
 this.oriRigWidth = this.originalLeft < 0 ? Number(this.$refs.right.style.width.split('px')[0]) : 0
},
touchmove (e) {
 let moveDistance = e.touches[0].pageX - this.originalPos // >0 右滑,<0 左滑
 if (moveDistance > 0 && this.originalLeft >= 0) { // 未向左邊滑動(dòng)時(shí),不能右滑
 return false
 }
 this.doSlide(moveDistance / 2 + this.originalLeft) // 除以2來控制滑動(dòng)速度
},
touchend (e) {
 const moveDistance = e.changedTouches[0].pageX - this.originalPos // >0 右滑,<0 左滑
 let distance
 if (!this.isClickBack && moveDistance === 0) { // 點(diǎn)擊時(shí)不收起右側(cè)
 return false
 }
 if ((-moveDistance) > 50) { // 向左滑動(dòng)超過闕值時(shí),右側(cè)滑出固定距離
 distance = this.pageWidth * this.rightWidth / 100
 } else { // 向左滑動(dòng)未超過闕值,或向右滑動(dòng)時(shí),回原位
 distance = 0
 }
 this.doSlide(-distance, true)
},
/**
 * 滑動(dòng)方法---位置變化 && 右側(cè)寬度變化
 * @param {Number} distance 滑動(dòng)距離
 * @param {Boolean} animate 滑動(dòng)是否有動(dòng)畫效果
 */
doSlide (distance, animate = false) {
 this.sliderEle.style.transform = `translateX(${distance}px)`
 this.$refs.right.style.width = -distance + 'px'
 if (this.isMainSlide) {
 this.sliderEle.style.transition = animate ? 'transform .5s' : 'initial'
 this.$refs.right.style.transition = animate ? 'width .5s' : 'initial'
 } else {
 this.sliderEle.style.transition = animate ? 'transform .5s, width .5s' : 'initial'
 }
}

3、組件使用

父組件可以調(diào)用slider組件的doSlide()方法來實(shí)現(xiàn)點(diǎn)擊喚出右側(cè)元素

<t-slider class="slider">
 <template slot="main">左側(cè)滑動(dòng)</template>
 <template slot="right">
 <div class="edit">編輯</div>
 <div class="delete">刪除</div>
 </template>
</t-slider>
<t-slider class="slider" :rightWidth="rightWidth" :isMainSlide="false">
 <template slot="main">
 <div>覆蓋左側(cè)</div>
 <div class="btn" @click="showRight">點(diǎn)擊喚出</div>
 </template>
 <template slot="right">
 <div class="newContent">newContent</div>
 </template>
</t-slider>
<t-slider class="slider" :isClickBack="false">
 <template slot="main">點(diǎn)擊不收起</template>
 <template slot="right">
 <div class="edit">編輯</div>
 <div class="delete">刪除</div>
 </template>
</t-slider>
components: {TSlider},
data () {
 return {
 pageWidth: null,
 rightWidth: 80
 }
},
mounted () {
 this.pageWidth = document.documentElement.clientWidth
},
 
methods: {
 showRight () {
 this.$refs.mainSlider.doSlide(-this.pageWidth * this.rightWidth / 100, true)
 }
}

4、效果展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue富文本插件(quill-editor)的使用及說明

    Vue富文本插件(quill-editor)的使用及說明

    這篇文章主要介紹了Vue富文本插件(quill-editor)的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2023-02-02
  • vue父組件如何獲取子組件的值

    vue父組件如何獲取子組件的值

    這篇文章主要介紹了vue父組件如何獲取子組件的值,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • Sublime Text新建.vue模板并高亮(圖文教程)

    Sublime Text新建.vue模板并高亮(圖文教程)

    這篇文章主要介紹了Sublime Text新建.vue模板并高亮(圖文教程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • Vue 單文件中的數(shù)據(jù)傳遞示例

    Vue 單文件中的數(shù)據(jù)傳遞示例

    本篇文章主要介紹了Vue 單文件中的數(shù)據(jù)傳遞示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • 前端vue3使用axios調(diào)用后端接口的實(shí)現(xiàn)方法

    前端vue3使用axios調(diào)用后端接口的實(shí)現(xiàn)方法

    vue本身不支持ajax接口的請(qǐng)求,所以在vue中經(jīng)常使用axios這個(gè)接口請(qǐng)求工具,下面這篇文章主要給大家介紹了關(guān)于前端vue3使用axios調(diào)用后端接口的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2022-12-12
  • Vue數(shù)字輸入框組件示例代碼詳解

    Vue數(shù)字輸入框組件示例代碼詳解

    很多朋友經(jīng)常遇到這樣的功能,只允許輸入數(shù)字,允許設(shè)置初始值、最大值、最小值,今天小編給大家分享示例代碼給大家介紹vue數(shù)字輸入框功能,感興趣的朋友一起看看吧
    2020-01-01
  • vue中關(guān)于$emit和$on的使用及說明

    vue中關(guān)于$emit和$on的使用及說明

    這篇文章主要介紹了vue中關(guān)于$emit和$on的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue加載自定義的js文件方法

    vue加載自定義的js文件方法

    下面小編就為大家分享一篇vue加載自定義的js文件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue 過渡實(shí)現(xiàn)輪播圖效果

    Vue 過渡實(shí)現(xiàn)輪播圖效果

    本篇文章主要介紹了Vue 過渡實(shí)現(xiàn)輪播圖效果,Vue 的過渡系統(tǒng)是內(nèi)置的,在元素從 DOM 中插入或移除時(shí)自動(dòng)應(yīng)用過渡效果。有需要的小伙伴可以參考下。
    2017-03-03
  • 深入了解Vue組件七種通信方式

    深入了解Vue組件七種通信方式

    vue組件通信的方式,這是在面試中一個(gè)非常高頻的問題。其實(shí)Vue組件的通信方式除了props和?$emit還有很多,本文將對(duì)vue組件通信方式進(jìn)行一下總結(jié),感興趣的可以學(xué)習(xí)一下
    2021-12-12

最新評(píng)論