vue中如何使用vue-touch插件
vue中使用vue-touch
如果想让vue能够监听移动端的上滑、下滑、左滑、点击等等动作,可以使用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的事件名="函数名( )"来使用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实现移动端左右滑动屏幕切换页面(左右滑动切换路由)
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"> 内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容 </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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
vue-music 使用better-scroll遇到轮播图不能自动轮播问题
根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播。如何解决这个问题呢,下面小编给大家带来了vue-music 使用better-scroll遇到轮播图不能自动轮播问题,感兴趣的朋友一起看看吧2018-12-12Vue + element实现动态显示后台数据到options的操作方法
最近遇到一个需求需要实现selector选择器中选项值options 数据的动态显示,而非写死的数据,本文通过实例代码给大家分享实现方法,感兴趣的朋友一起看看吧2021-07-07vue+element DatePicker实现日期选择器封装
Vue Element DatePicker是一款基于Vue.js的日期选择控件,它提供了丰富的日期选择功能,支持日期范围选择、日期格式化、自定义日期格式、快捷选择等功能,极大地提高了用户的体验,是开发者必备的日期选择控件。2023-02-02详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)
本篇文章主要介绍了详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli),具有一定的参考价值,有需要的可以了解下2017-08-08
最新评论