【微信小程序】wxs页面手指上下滑动范围与盒子左右移动关联

wxs页面手指上下滑动范围与盒子左右移动关联

使用场景

要求页面在滑动到一定范围时,如果出现横向盒子列表,则在靠近这个横向盒子列表时,列表会根据当前页面手指上下滑动的范围去实时控制这个横向盒子左右移动,通过这种方式来提示用户此处可以左右滑动。

实现经历

1、scroll-view:scrollTop不能够满足手指当前的实时移动,且只能在scroll-view标签范围内,才能监听到 bindscroll;
2、swiper:不适用整个页面;
3、touchmove:虽然有做节流,但还是因频繁调用setData,在安卓上,还是有很明显的卡顿,ios上好一些;
4、wxs:最后采用在wxs里面去做touchmove相关的操作

代码片段

index.js

const app = getApp()

Page({
  data: {
    test_info: [
      1,2,3,4,5,6,7,8,9,10
    ],
  }
})

index.wxs

var lastX, lastY, allStatusX, allStatusY, scrollLeft

var scrollMove = function (e, ins) {
    //console.log("pageY = " + JSON.stringify(e.changedTouches[0].pageY));
    var currentX = e.changedTouches[0].pageX;
    var currentY = e.changedTouches[0].pageY;
    allStatusX = currentX - lastX;
    allStatusY = currentY - lastY;
    // var scrollLeft = 0 - e.changedTouches[0].pageY + "px";

    //上下方向实时滑动
    if (Math.abs(allStatusX) < Math.abs(allStatusY)) {
        //console.log(allStatusY);
        if (allStatusY < 0) {
            console.log('实时向上滑动')
            scrollLeft = -160
        } else {
            console.log('实时向下滑动')
            scrollLeft = 0
        } 
    }

    // console.log("scrollXMove = " + JSON.stringify(e.detail));
    ins.selectComponent(".head-box").setStyle({
        "transform":"translateX("+scrollLeft+"px);" 
    });
}

var scrollStart = function (e, ins) {
    lastX = e.touches[0].pageX
    lastY = e.touches[0].pageY
    console.log('开始坐标轴' + JSON.stringify(lastX), JSON.stringify(lastY));
}

var scrollEnd = function (e, ins) {
    console.log("scrollEnd");
}

module.exports = {
    scrollMove: scrollMove,
    scrollStart: scrollStart,
    scrollEnd: scrollEnd
}

index.wxml

<wxs module="scrollMove" src="./index.wxs"> </wxs>
<view style="margin-top:50rpx;">
	下面可以左右滑动
</view>

<!-- <scroll-view class="scroll-x-table" scroll-y="true" upper-threshold="0" lower-threshold="0" bindscroll="{{scrollMove.scrollXMove}}">
	<view class="subscribe-box justify">
		<block wx:for="{{test_info}}" wx:for-index="index" wx:for-item="item" wx:key="fieldId">
			<view class="field-info">
				<view class="field-title">{{item}}</view>
			</view>
		</block>
	</view>
</scroll-view> -->

<view capture-bind:touchmove="{{scrollMove.scrollMove}}" capture-bind:touchstart="{{scrollMove.scrollStart}}" capture-bind:touchend="{{scrollMove.scrollEnd}}">
	<view class="subscribe-box justify">
		<block wx:for="{{test_info}}" wx:for-index="index" wx:for-item="item" wx:key="fieldId">
			<view class="field-info">
				<view class="field-title">{{item}}</view>
			</view>
		</block>
	</view>
</view>


<view style="margin:30rpx;">
	底部内容随上面滑动左右移动
</view>
<view class="head-scroll-x head-fixed justify">
	<view class="head-box">
		<block wx:for="{{test_info}}" wx:for-index="index" wx:for-item="item" wx:key="fieldId">
			<view class="head-line">
				<view class="title">底部{{item}}</view>
			</view>
		</block>
	</view>
</view>

index.wxss

page {
  width: 100vw;
  height: 100vh;
}

.head-scroll-x {
  margin: 0;
  height: 80rpx;
  white-space: nowrap;
  background-color: white;
}

.head-box {
  background-color: white;
  padding-left: 15rpx;
  transition: transform .5s;
}

.head-line {
  display: inline-block;
  width: 120rpx;
}

.head-line .title {
  width: 100rpx;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  background-color: greenyellow;
  font-size: 25rpx;
}

.scroll-x-table {
  width: 750rpx;
  margin: 30rpx 0 0;
  padding-bottom: 20rpx;
  height: 200rpx;
}

.subscribe-box {
  margin-top: 5rpx;
}

.field-info {
  margin: 0 10rpx;
  width: 100%;
}

.field-title {
  width: 90%;
  height: 80rpx;
  line-height: 80rpx;
  margin: 0 auto;
  margin-bottom: 20rpx;
  text-align: center;
  background-color: orange;
  box-sizing: border-box;
}

.justify {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

1️⃣instance对象支持的方法:

在这里插入图片描述
最常用的就是获取组件(类似于获取dom节点)、设置样式和class,以及调用app service方法,并且还有强大的触发器函数的使用,可以监听appservice 层中的data变化,具体demo请查看官方文档链接

2️⃣官方Tips:
在这里插入图片描述

GIF演示

在这里插入图片描述

注意事项

1、wxs不能使用ES6+语法,否则会报错(勾选了ES6转ES5也没用)
2、console.log()不能直接打印对象,需要JSON.stringify
3、不能调用app service环境中的方法和wx.xxxx方法

以上就是我在项目中关于使用wxs来实现 “手指上下滑动范围与盒子左右移动关联” 的全部内容啦,如果能够帮助到您,欢迎点赞👍+关注💗+收藏⭐️啦❗️❗️❗️,希望我们彼此能在前端的道路上继续加油努力探索呀😆😆😆

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值