需求是在list页中带有搜索框,下滑时隐藏顶部输入框,上滑时显示输入框,提高一点点用户体验。避免想要搜索就必须上滑至顶部。。
实现:
首先输入框得固定在顶部。
wxml:class="search-box {
{showSearch? '':'hide'}} bindtouchstart="handletouchstart" bindtouchmove="handletouchmove"
wxss:
.hide{
display: none;
}
.search-box {
position: fixed;
top: 0;
left: 0;
}
js:
data: {
startY: 0, //滑动开始y轴位置
lastY: 0, //滑动开始y轴位置
showSearch: true, //是否显示搜索框
},
// 监听页面滚动事件
handletouchmove (event) {
let currentY = event.changedTouches[0].clientY
if (currentY <= this.data.startY) {
this.setData({
showSearch: false
})
console.log("下滑")
} else {
this.setData({
showSearch: true
})
console.log("上滑")
}
},
//滑动开始事件
handletouchstart: function (event) {
this.data.startY = event.