vue實(shí)現(xiàn)左右滑動(dòng)效果實(shí)例代碼
前言
個(gè)人實(shí)際開(kāi)發(fā)中用到的效果問(wèn)題總結(jié)出來(lái)便于自己以后開(kāi)發(fā)查看調(diào)用,如果也適用其他人請(qǐng)隨意拿走勿噴就行!
vue.js是現(xiàn)在流行的js框架之一,vue 是一套用于構(gòu)建用戶(hù)界面的漸進(jìn)式j(luò)avascript框架,與其它大型框架不同的是:vue被設(shè)計(jì)為可以自底向上逐層應(yīng)用。vue的核心庫(kù)只關(guān)注視圖層,不僅易于上手,還便于與第三方庫(kù)或既有項(xiàng)目整合,另外一個(gè)方面,當(dāng)vue與現(xiàn)代化的工具鏈以及各種支持類(lèi)庫(kù)結(jié)合使用時(shí),vue也完全能夠?yàn)閺?fù)雜的單頁(yè)應(yīng)用提供驅(qū)動(dòng)。
vue.js是用于構(gòu)建交互式的Web界面的庫(kù),它提供MVVM數(shù)據(jù)綁定和一個(gè)可組合的組件系統(tǒng),具有簡(jiǎn)單、靈活的API。從技術(shù)上講,vue.js集中在MVVM模式上的視圖模型層(viewModel),并通過(guò)雙向數(shù)據(jù)綁定連接視圖(view) 和模型(model)。實(shí)際的DOM操作和輸出格式被抽象出來(lái)成指令和過(guò)濾器。相比其他的MVVM框架,vue.js更容易上手,它讓你通過(guò)簡(jiǎn)單而靈活的API創(chuàng)建由數(shù)據(jù)驅(qū)動(dòng)的UI組件。
實(shí)例代碼
HTML代碼
<template>
<div id="SlideBar" class="box">
<div class="item" ref="slide" :style="slideStyle" @touchstart="start($event)" @touchmove="move($event)" @touchend="end($event)">
<img src="http://img2.imgtn.bdimg.com/it/u=2555191195,2735808065&fm=26&gp=0.jpg" alt="">
<div class="right">
<div class="title">你好!</div>
<p class="text">哈哈哈</p>
<p class="price">好不</p>
</div>
</div>
<div class="btn" ref="btn">
<button>編輯</button>
<button style="background:#387ef5;color:#fff">收藏</button>
</div>
</div>
</template>
CSS代碼
<style>
.box{
position:relative;
border-bottom:0.026667rem solid #666666;
}
.btn{
height:100%;
position:absolute;
right:0;
top:0;
background:red;
display:flex;
}
button{
width:1.6rem;
height:100%;
background:#f8f8f8;
border:none;
}
.item{
padding:0.266667rem;
display:flex;
position:relative;
background:#fff;
z-index: 2;
box-shadow: 0.026667rem 0 0.053333rem #ddd;
}
.item img{
width:2.133333rem;
height:2.133333rem;
margin-right:0.4rem;
border-radius: 0.133333rem;
}
.item .title{
font-size:0.48rem;
float: left;
}
.item .text{
font-size:0.426667rem;
color:#888;
float: left;
margin: 0 1.33rem;
}
.item .price{
color:#888;
float: left;
margin: 0 1.33rem;
}
</style>
JS代碼
<script>
export default {
name: 'SlideBar',
props: {
},
data (){
return {
flag: false,
startX: 0,
endX: 0,
slideStyle: {
left: 0,
transition: 'none'
}
}
},
methods: {
start (e){ //記錄開(kāi)始滑動(dòng)屏幕的X軸的位置
this.flag = true;
this.startX = e.touches[0].clientX;
this.endX = this.$refs.slide.offsetLeft;
this.slideStyle.transition = 'none';
},
move (e){
if(this.flag){
// 處理鼠標(biāo)移動(dòng)的邏輯
var moveX = this.endX + (e.touches[0].clientX - this.startX); //計(jì)算滑動(dòng)的距離
if(Math.abs(moveX) >= this.$refs.btn.offsetWidth && moveX < 0){ //判斷滑動(dòng)的距離是否大于class:btn的寬度
moveX = (Math.abs(moveX) - this.$refs.btn.offsetWidth) * 0.1; // 0.3阻力系數(shù)
this.slideStyle.left = - this.$refs.btn.offsetWidth - moveX + 'px';
}else if(moveX >= 0){ //滑動(dòng)距離是否大于等于0
this.slideStyle.left = 0 + 'px'; //大于等于0讓class:item等于0
}else{
this.slideStyle.left = moveX + 'px'; //小于0讓class:item等于滑動(dòng)的距離
}
}
},
end (e){
if(this.flag){
this.flag = false;
// endX = slide.offsetLeft;
var moveX = e.changedTouches[0].clientX - this.startX; //計(jì)算滑動(dòng)的距離
this.slideStyle.transition = 'left .3s';
var btnWidth = this.$refs.btn.offsetWidth; //class:btn的寬度
if(moveX < 0){
if(Math.abs(moveX) >= btnWidth / 2 || Math.abs(this.$refs.slide.offsetLeft) >= this.$refs.btn.offsetWidth){ //是否大于class:btn寬度的一半
this.slideStyle.left = - btnWidth + 'px'; //左滑超過(guò)class:btn寬度的一半就滑回去
}else if(Math.abs(moveX) < btnWidth / 2){ //小于class:btn寬度的一半
this.slideStyle.left = 0 + 'px'; //左滑沒(méi)有超過(guò)class:btn寬度的一半回原位
}
}else if(moveX > 0 && this.endX != 0){
if(Math.abs(moveX) >= btnWidth / 2){
this.slideStyle.left = 0 + 'px'; //右滑超過(guò)class:btn寬度的一半就滑回去
}else if(Math.abs(moveX) < btnWidth / 2){
this.slideStyle.left = - btnWidth + 'px'; //右滑沒(méi)有超過(guò)class:btn寬度的一半回原位
}
}
}
}
},
mounted (){
var _this = this;
// 使用js的現(xiàn)代事件監(jiān)聽(tīng)transition過(guò)渡結(jié)束
this.$refs.slide.addEventListener('transitionend',function(){
_this.endX = this.offsetLeft;
})
}
}
</script>
總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)左右滑動(dòng)效果的文章就介紹到這了,更多相關(guān)vue左右滑動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)頂部左右滑動(dòng)導(dǎo)航
- Vue實(shí)現(xiàn)tab導(dǎo)航欄并支持左右滑動(dòng)功能
- vue+swiper實(shí)現(xiàn)左右滑動(dòng)的測(cè)試題功能
- vue使用swiper實(shí)現(xiàn)左右滑動(dòng)切換圖片
- vue使用better-scroll實(shí)現(xiàn)滑動(dòng)以及左右聯(lián)動(dòng)
- vue移動(dòng)端的左右滑動(dòng)事件詳解
- vue移動(dòng)端實(shí)現(xiàn)手機(jī)左右滑動(dòng)入場(chǎng)動(dòng)畫(huà)
- Vue實(shí)現(xiàn)移動(dòng)端左右滑動(dòng)效果的方法
- 基于Vue實(shí)現(xiàn)頁(yè)面切換左右滑動(dòng)效果
- Vue可左右滑動(dòng)按鈕組組件使用詳解
相關(guān)文章
Vue入口文件index.html緩存的問(wèn)題及解決
這篇文章主要介紹了Vue入口文件index.html緩存的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue設(shè)置長(zhǎng)時(shí)間未操作登錄自動(dòng)到期返回登錄頁(yè)
這篇文章主要介紹了Vue設(shè)置長(zhǎng)時(shí)間未操作登錄以后自動(dòng)到期返回登錄頁(yè),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-01-01
vue之this.$router.push頁(yè)面刷新問(wèn)題
這篇文章主要介紹了vue之this.$router.push頁(yè)面刷新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
vue進(jìn)入頁(yè)面加載數(shù)據(jù)緩慢實(shí)現(xiàn)loading提示過(guò)程
這篇文章主要介紹了vue進(jìn)入頁(yè)面加載數(shù)據(jù)緩慢實(shí)現(xiàn)loading提示過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
動(dòng)態(tài)加載權(quán)限管理模塊中的Vue組件
本篇文章給大家詳細(xì)講解了如何在權(quán)限管理模塊中動(dòng)態(tài)的加載VUE組件的過(guò)程,有這方面需求的朋友跟著學(xué)習(xí)下吧。2018-01-01
如何解決npm i下載依賴(lài)的時(shí)候出現(xiàn)某依賴(lài)版本沖突
這篇文章主要介紹了如何解決npm i 下載依賴(lài)的時(shí)候出現(xiàn)某依賴(lài)版本沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue2.0使用v-for循環(huán)制作多級(jí)嵌套菜單欄
這篇文章主要介紹了vue2.0制作多級(jí)嵌套菜單欄,主要使用v-for循環(huán)生成一個(gè)多級(jí)嵌套菜單欄,這個(gè)方法應(yīng)用非常廣泛,需要的朋友可以參考下2018-06-06

