知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序:實(shí)現(xiàn)可拖動(dòng)懸浮圖標(biāo)
發(fā)表時(shí)間:2021-1-4
發(fā)布人:葵宇科技
瀏覽次數(shù):87
在制作商城類微信小程序的過程中,我們經(jīng)常會(huì)碰到需要增加可拖動(dòng)懸浮圖標(biāo)的情況,本文簡單的介紹一下可拖動(dòng)懸浮按鈕的實(shí)現(xiàn)。
運(yùn)行截圖:js:
var startPoint
Page({
data: {
//按鈕位置參數(shù)
buttonTop: 0,
buttonLeft: 0,
windowHeight: '',
windowWidth: '',
//角標(biāo)顯示數(shù)字
corner_data:0,
},
onLoad:function(){
//定義角標(biāo)數(shù)字
this.setData({
corner_data:3
})
// 獲取購物車控件適配參數(shù)
var that =this;
wx.getSystemInfo({
success: function (res) {
console.log(res);
// 屏幕寬度、高度
console.log('height=' + res.windowHeight);
console.log('width=' + res.windowWidth);
// 高度,寬度 單位為px
that.setData({
windowHeight: res.windowHeight,
windowWidth: res.windowWidth,
buttonTop:res.windowHeight*0.70,//這里定義按鈕的初始位置
buttonLeft:res.windowWidth*0.70,//這里定義按鈕的初始位置
})
}
})
},
//可拖動(dòng)懸浮按鈕點(diǎn)擊事件
btn_Suspension_click:function(){
//這里是點(diǎn)擊購物車之后將要執(zhí)行的操作
wx.showToast({
title: '點(diǎn)擊成功',
icon:'success',
duration:1000
})
},
//以下是按鈕拖動(dòng)事件
buttonStart: function (e) {
startPoint = e.touches[0]//獲取拖動(dòng)開始點(diǎn)
},
buttonMove: function (e) {
var endPoint = e.touches[e.touches.length - 1]//獲取拖動(dòng)結(jié)束點(diǎn)
//計(jì)算在X軸上拖動(dòng)的距離和在Y軸上拖動(dòng)的距離
var translateX = endPoint.clientX - startPoint.clientX
var translateY = endPoint.clientY - startPoint.clientY
startPoint = endPoint//重置開始位置
var buttonTop = this.data.buttonTop + translateY
var buttonLeft = this.data.buttonLeft + translateX
//判斷是移動(dòng)否超出屏幕
if (buttonLeft+50 >= this.data.windowWidth){
buttonLeft = this.data.windowWidth-50;
}
if (buttonLeft<=0){
buttonLeft=0;
}
if (buttonTop<=0){
buttonTop=0
}
if (buttonTop + 50 >= this.data.windowHeight){
buttonTop = this.data.windowHeight-50;
}
this.setData({
buttonTop: buttonTop,
buttonLeft: buttonLeft
})
},
buttonEnd: function (e) {
}
})
wxml:
<!--可拖動(dòng)按鈕控件表-->
<!--buttonStart和buttonEnd一定不能用catch事件,否則按鈕點(diǎn)擊事件會(huì)失效-->
<view class="btn_Suspension" bindtap="btn_Suspension_click" catchtouchmove="buttonMove" bindtouchstart="buttonStart" bindtouchend="buttonEnd" style="top:{
{
buttonTop}}px;left:{
{
buttonLeft}}px;">
<image class="Suspension_logo" src="../images/Suspension.png"></image><!--這里是按鈕圖標(biāo),下載地址會(huì)在文章底部說明-->
<view wx:if="{
{corner_data=http://www.wxapp-union.com/=0}}"></view>
<view wx:else>
<view class="cornorMark">
<text>{
{corner_data}}</text>
</view>
</view>
</view>
wxss:
Page{
background: #f5f5f5;
}
/**可拖動(dòng)懸浮按鈕樣式表**/
.btn_Suspension{
position: fixed;
height: 100rpx;
width: 100rpx;
background-color: rgba(255, 255, 255, 0.755);
border-radius: 100%;
display: flex;
align-items: center;
justify-content: center;
z-index: 99999;
box-shadow: 1px 0px 1px 1px #ede7e7;
}
.Suspension_logo{
position:absolute;
height:50%;
width:50%;
left:23%;
top:27%
}
.cornorMark{
position:absolute;
background: rgb(242, 109, 38);
border:1px solid rgb(242, 109, 38);
border-radius: 100px;
width:30rpx;
height:30rpx;
top:-17rpx;
right:3rpx;
color:#fff;
font-size: 12px;
text-align: center;
}
.cornorMark text{
position:absolute;
width:100%;
left:0%;
text-align: center;
top:-1px;
}
相關(guān)案例查看更多
相關(guān)閱讀
- web開發(fā)
- 小程序生成海報(bào)
- 云南軟件公司
- 河南小程序制作
- 云南網(wǎng)站建設(shè)高手
- 小程序被騙退款成功
- 云南軟件定制公司
- 重慶網(wǎng)站建設(shè)公司
- 云南小程序開發(fā)公司哪家好
- 小程序定制開發(fā)
- 云南etc小程序
- 報(bào)廢車回收
- 云南小程序哪家好
- 云南小程序公司
- 北京小程序開發(fā)
- 表單
- 昆明小程序開發(fā)
- 安家微信小程序
- 網(wǎng)站建設(shè)優(yōu)化
- 開發(fā)微信小程序
- 政府網(wǎng)站建設(shè)服務(wù)
- 楚雄小程序開發(fā)
- 昆明小程序開發(fā)聯(lián)系方式
- 網(wǎng)站建設(shè)公司哪家好
- 智慧農(nóng)貿(mào)市場(chǎng)
- 昆明做網(wǎng)站
- 云南網(wǎng)站建設(shè)
- 網(wǎng)站建設(shè)特性
- 云南花農(nóng)小程序
- 昆明小程序設(shè)計(jì)