vue中如何使用vue-touch插件
vue中使用vue-touch
如果想讓vue能夠監(jiān)聽(tīng)移動(dòng)端的上滑、下滑、左滑、點(diǎn)擊等等動(dòng)作,可以使用vue-touch插件。vue-touch的使用十分簡(jiǎn)單。
首先在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>來(lái)包裹要使用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ù)名( )"來(lái)使用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)屏幕切換頁(yè)面(左右滑動(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.在某頁(yè)面中
<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頁(yè) this.$router.push({'path':'/goods'}); }, swiperright: function () { //右滑切換到detail頁(yè) this.$router.push({'path':'/detail'}); } } } </script>
到此這篇關(guān)于vue中使用vue-touch的文章就介紹到這了,更多相關(guān)vue使用vue-touch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2 el-table行懸停時(shí)彈出提示信息el-popover的實(shí)現(xiàn)
本文主要介紹了vue2 el-table行懸停時(shí)彈出提示信息el-popover的實(shí)現(xiàn),用到了cell-mouse-enter、cell-mouse-leave兩個(gè)事件,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Vue + Webpack + Vue-loader學(xué)習(xí)教程之功能介紹篇
這篇文章主要介紹了關(guān)于Vue + Webpack + Vue-loader功能介紹的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03vue百度地圖通過(guò)地址名稱(chēng)獲取地址的經(jīng)緯度gps問(wèn)題(具體步驟)
在Vue項(xiàng)目中,可以通過(guò)使用百度地圖JavaScript?API來(lái)實(shí)現(xiàn)根據(jù)地址名稱(chēng)獲取經(jīng)緯度GPS的功能,本文分步驟給大家詳細(xì)講解vue百度地圖獲取經(jīng)緯度的實(shí)例,感興趣的朋友一起看看吧2023-05-05vue3父子通信ref,toRef,toRefs使用實(shí)例詳解
這篇文章主要介紹了vue3父子通信ref,toRef,toRefs使用實(shí)例詳解,分別介紹了ref是什么、toRef是什么及toRefs是什么和最佳使用方式,結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2023-10-10使用element ui中el-table-column進(jìn)行自定義校驗(yàn)
這篇文章主要介紹了使用element ui中el-table-column進(jìn)行自定義校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue 數(shù)值改變頁(yè)面沒(méi)有刷新的問(wèn)題解決(數(shù)據(jù)改變視圖不更新的問(wèn)題)
這篇文章主要介紹了Vue 數(shù)值改變頁(yè)面沒(méi)有刷新的問(wèn)題解決(數(shù)據(jù)改變視圖不更新的問(wèn)題),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用
這篇文章主要為大家介紹了Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06