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