惯例,先问百度AI:
微信小程序中,前端创建客服按钮后怎么做手指点击按钮时呼出客服,手指触摸移动按钮跟随移动,并且移动单位换算为vmin,并提供对应的前端代码
得到:
在微信小程序中,要创建一个客服按钮,使得当用户点击按钮时能够呼出客服,并且当用户触摸并移动按钮时按钮能够跟随手指移动,同时移动的单位换算为vmin,你可以使用以下步骤和代码来实现:
在WXML中创建客服按钮,并为其绑定触摸和点击事件。
在WXSS中设置按钮的样式,确保它能够移动并且位置可以通过样式来动态调整。
在JS中处理触摸事件和点击事件,实现按钮的跟随移动和客服的呼出。
以下是具体的代码示例:
WXML (index.wxml):
<view class="container">
<button class="customer-service-button"
bindtap="handleCustomerServiceTap"
catchtouchstart="handleTouchStart"
catchtouchmove="handleTouchMove"
catchtouchend="handleTouchEnd"
style="{{ buttonStyle }}">
联系客服
</button>
</view>
WXSS (index.wxss):
.container {
position: relative;
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
.customer-service-button {
position: absolute;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
z-index: 10; /* 确保按钮在其他内容之上 */
touch-action: none; /* 禁止系统默认的触摸行为 */
}
JS (index.js):
Page({
data: {
buttonPosition: {
x: 0,
y: 0
},
isDragging: false,
initialTouchPosition: {
x: 0,
y: 0
}
},
// 处理客服按钮点击事件
handleCustomerServiceTap: function () {
wx.openCustomerServiceChat({
// 这里可以配置客服的相关信息,如客服ID等
});
},
// 处理触摸开始事件
handleTouchStart: function (e) {
this.setData({
isDragging: true,
initialTouchPosition: {
x: e.touches[0].clientX,
y: e.touches[0].clientY
}
});
},
// 处理触摸移动事件
handleTouchMove: function (e) {
if (!this.data.isDragging) return;
const deltaX = e.touches[0].clientX - this.data.initialTouchPosition.x;
const deltaY = e.touches[0].clientY - this.data.initialTouchPosition.y;
// 将像素值转换为vmin
const vminScale = Math.min(wx.getSystemInfoSync().windowWidth, wx.getSystemInfoSync().windowHeight) / 100;
const newX = this.data.buttonPosition.x + (deltaX / vminScale);
const newY = this.data.buttonPosition.y + (deltaY / vminScale);
this.setData({
buttonPosition: {
x: newX,
y: newY
},
initialTouchPosition: {
x: e.touches[0].clientX,
y: e.touches[0].clientY
}
});
},
// 处理触摸结束事件
handleTouchEnd: function () {
this.setData({
isDragging: false
});
}
});
在这个例子中,我们首先创建了一个按钮,并为它绑定了点击事件(handleCustomerServiceTap)和触摸事件(handleTouchStart、handleTouchMove、handleTouchEnd)。在点击事件中,我们调用了wx.openCustomerServiceChat方法来呼出客服。
触摸事件的处理中,我们记录了触摸开始的初始位置,并在触摸移动时计算了移动的距离,然后将这个距离转换为vmin单位并更新按钮的位置。注意,vmin的转换是基于当前视口较小的一边来计算的,我们使用wx.getSystemInfoSync().windowWidth和wx.getSystemInfoSync().windowHeight来获取视口的尺寸。
现在我不想通过wx.openCustomerServiceChat来打开客服窗口,但是这个触摸事件与点击事件有冲突,尝试过用时间差解决,但是对于官方提供的<button open-type="contact" bindcontact="handleContact" >
中的handleContact没有办法处理,只能再次请教大佬:
微信小程序开发的客服悬浮窗怎么设置为可移动
找到:可拖动悬浮窗的实现
ok,基本perfect了,再在悬浮窗插入自己的图片就完工,下面是个人代码(wxml)
<movable-area style="pointer-events: none;height: 100%;width: 100%;position:absolute;left:0px;top:0px;">
<movable-view x="{{mx}}" y="{{my}}" direction="all" style="pointer-events: auto;height: 12vmin; width: 12vmin; background:#AAAAAA;border-radius: 50%;">
<button open-type="contact" bindcontact="handleContact" style="width: 12vmin;height: 12vmin;background-color: #AAAAAA;border-radius: 50%;z-index: 999999;position: absolute;">
<image src="{{kefu}}" style="width: 30vmin;height: auto;position: relative;margin-top: -11vmin;margin-left: -15vmin;border-radius: 50%;" mode="widthFix"></image>
</button>
</movable-view>
</movable-area>
当然啦,一开始我也用了movable-area加movable-view组合,movable-view忘记给宽高了……然后就是图片中为啥margin-top: -11vmin;margin-left: -15vmin;这个是根据自己所需调整即可。