小程序——左右页面来回滑动

这篇博客介绍了如何在小程序中实现左右页面的滑动切换功能。通过监听`touchStart`和`touchEnd`事件,结合CSS样式和JavaScript的计时器,判断用户的滑动方向并调用`wx.navigateTo`进行页面跳转。示例代码中,当用户向左或向右滑动超过一定距离且时间小于100ms时,页面会相应地跳转到左边或右边的页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<view class="class_first" bindtouchstart="touchStart" bindtouchend="touchEnd">

滑动切换页面

</view>

 

 

 

.class_first {

 

width: 100%;

height: 1334rpx;

background-color: red

}

 

 

 

var time = 0;

var touchDot = 0;//触摸时的原点

var interval = "";

var flag_hd = true;

 

Page({

onLoad: function () {

var that = this

},

onShow: function () {

flag_hd = true; //重新进入页面之后,可以再次执行滑动切换页面代码

clearInterval(interval); // 清除setInterval

time = 0;

},

// 触摸开始事件

touchStart: function (e) {

touchDot = e.touches[0].pageX; // 获取触摸时的原点

// 使用js计时器记录时间

interval = setInterval(function () {

time++;

}, 100);

},

// 触摸结束事件

touchEnd: function (e) {

var touchMove = e.changedTouches[0].pageX;

// 向左滑动

if (touchMove - touchDot <= -40 && time < 10 && flag_hd == true) {

flag_hd = false;

//执行切换页面的方法

console.log("向右滑动");

wx.navigateTo({

url: '../right/right'

})

}

// 向右滑动

if (touchMove - touchDot >= 40 && time < 10 && flag_hd == true) {

flag_hd = false;

//执行切换页面的方法

console.log("向左滑动");

wx.navigateTo({

url: '../left/left'

})

}

clearInterval(interval); // 清除setInterval

time = 0;

}

})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值