vue中如何使用vue-touch插件
vue中使用vue-touch
如果想讓vue能夠監(jiān)聽移動(dòng)端的上滑、下滑、左滑、點(diǎn)擊等等動(dòng)作,可以使用vue-touch插件。vue-touch的使用十分簡單。
首先在vue項(xiàng)目中安裝vue-touch
npm install vue-touch@next --save
然后在main.js或mian.ts上導(dǎo)入并使用:
import VueTouch from "vue-touch";
Vue.use(VueTouch, {name: "v-touch"});使用<v-touch></v-touch>來包裹要使用vue-touch的元素
<v-touch @swipeleft="nowLeft()" @swiperight="nowRight()">
<ul class="list">
<li v-for="(item, index) in move_nowData" :key="item.id">
<div>
<img :src="item.cover" />
</div>
<h3>{{ item.title }}</h3>
<p>{{ item.rate }}</p>
</li>
</ul>
</v-touch>使用@touch.js的事件名="函數(shù)名( )"來使用vue-touch
methods: {
nowLeft() {
// 獲取list
let now = document.getElementsByClassName("list")[0];
now.style.transform += "translateX(-30rem)";
now.style.transition = "all ease 0.5s";
},
nowRight() {
// 獲取list
let now = document.getElementsByClassName("list")[0];
now.style.transform += "translateX(30rem)";
now.style.transition = "all ease 0.5s";
},
},touch.js常用事件

使用vue-touch實(shí)現(xiàn)移動(dòng)端左右滑動(dòng)屏幕切換頁面(左右滑動(dòng)切換路由)
1.安裝vue-touch
npm install vue-touch@next --save
2.在main.js中引入
import VueTouch from 'vue-touch'
Vue.use(VueTouch,{name:'v-touch'})
VueTouch.config.swipe = {
threshold:50 //設(shè)置左右滑動(dòng)的距離
}import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import GoodsList from '@/view/GoodList'
import GoodDetail from '@/view/GoodDetail'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},{
path:'/goods',
name:'GoodsList',
component:GoodsList
},{
path:'/detail',
name:'GoodDetail',
component:GoodDetail
}
]
})4.在某頁面中
<template>
<div class="hello">
<v-touch v-on:swipeleft="swiperleft" v-on:swiperight="swiperright" class="wrapper">
內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容
</v-touch>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
methods:{
swiperleft: function () { //左劃切換到goods頁
this.$router.push({'path':'/goods'});
},
swiperright: function () { //右滑切換到detail頁
this.$router.push({'path':'/detail'});
}
}
}
</script>到此這篇關(guān)于vue中使用vue-touch的文章就介紹到這了,更多相關(guān)vue使用vue-touch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pinia進(jìn)階setup函數(shù)式寫法封裝到企業(yè)項(xiàng)目
這篇文章主要為大家介紹了Pinia進(jìn)階setup函數(shù)式寫法封裝到企業(yè)項(xiàng)目實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取
這篇文章主要介紹了vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue利用Blob下載原生二進(jìn)制數(shù)組文件
這篇文章主要為大家詳細(xì)介紹了Vue利用Blob下載原生二進(jìn)制數(shù)組文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Vue中關(guān)于異步請求數(shù)據(jù)未更新的解決
本文將探討Vue中異步請求數(shù)據(jù)未更新的常見原因,并提供解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
vue.js2.0 實(shí)現(xiàn)better-scroll的滾動(dòng)效果實(shí)例詳解
better-scroll 是一個(gè)移動(dòng)端滾動(dòng)的解決方案,它是基于 iscroll 的重寫。better-scroll 也很強(qiáng)大,不僅可以做普通的滾動(dòng)列表,還可以做輪播圖、picker 等等,下面通過本文給大家介紹vue.js2.0 實(shí)現(xiàn)better-scroll的滾動(dòng)效果,感興趣的朋友一起看看吧2018-08-08

