不用自己再去計算按鈕的位置直接使用
class App extends Components {
render () {
return (
<MovableArea style='height: 200px; width: 200px; background: red;'>
<MovableView style='height: 50px; width: 50px; background: blue;' direction='all'>旅行的意義</MovableView>
</MovableArea>
)
}
}
但是這里有一個小坑,對於初次使用來說,就是設定他的可移動區域,在底層的所有點擊事件就無法點擊,這里就要用到css中事件穿透屬性:
pointer-events:
none
;
/* 這個屬性設置為none,讓所有事件穿透過去 */
如下代碼:
View Code
.movable-area{ pointer-events:none; /* 這個屬性設置為none,讓所有事件穿透過去 */ z-index: 100; width: 100%; height: 100%; position: fixed; top: 0; left: 0; right: 0; bottom: 0; } .movable-view{ pointer-events:auto; /* 重設為auto,覆蓋父屬性設置 */ height: 100rpx; width: 120rpx; /* background: red; */ }
當然自己也可以寫移動位置,但是人家寫出啦就直接用,不過還是自己去寫比較好喲,taro小程序懸浮按鈕移動參考:
https://blog.csdn.net/weixin_43497384/article/details/106690717
用taro組件寫的如下:

<MovableArea className='customPage-MovableArea'> <MovableView direction='vertical' y={windowData.safeArea.height-300} x={windowData.safeArea.width-30} className='customPage-MovableView' > <View className={`home-service ${isChooseMore ? 'active' : ''}`}> { isChooseMore ? <View className='home-service-choose'> <View className='choose-wechat' onClick={this.copyWechat.bind(this)}> <View className='choose-call-logo'></View> 客服微信 </View> <Button openType='contact' hoverClass='none' className='choose-call'> <View className='choose-call-logo'></View> 客服聊天 </Button> <View className='choose-phone' onClick={this.callPhone.bind(this)}> <View className='choose-call-logo'></View> 客服電話 </View> <View className='choose-hide' onClick={this.setChooseState.bind(this, false)}></View> </View> : <View className='home-service-pic' onClick={this.setChooseState.bind(this, true)} ></View> } </View> </MovableView> </MovableArea>
樣式如下:
.customPage-MovableArea { width: 100%; height: 100vh; position: fixed; z-index: 2; pointer-events: none; .customPage-MovableView { padding: 20% 13% 20% 5%; pointer-events: auto; .home-service { width: 100px; height: 100px; transition: height 0.3s; z-index: 11; background: #fff; border-radius: 50px; border: 1px solid #eee; &.active { height: 350px; overflow: hidden; } .home-service-pic { width: 100px; height: 100px; background: url('../../static/image/service.png'); background-size: 100%; } .home-service-choose { height: 232px; background: #FFFFFF; border-radius: 40px; .choose-call { height: 100px; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 18px; padding: 0; background-color: transparent; line-height: 1.5; .choose-call-logo { width: 31.28px; height: 45.42px; background-image: url('./image/music.png'); background-repeat: no-repeat; background-size: 100%; margin-bottom: 2px; } &::after { opacity: 0; } } .choose-phone { height: 100px; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 18px; .choose-call-logo { width: 31.28px; height: 31.28px; background-image: url('./image/phone.png'); background-repeat: no-repeat; background-size: 100%; margin-bottom: 6px; } } .choose-wechat { height: 100px; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 18px; padding: 0; background-color: transparent; line-height: 1.5; .choose-call-logo { width: 31.28px; height: 31.28px; background-image: url('./image/wechat.png'); background-repeat: no-repeat; background-size: 100%; margin-bottom: 6px; } } .choose-hide { width: 58.71px; height: 36.39px; margin: 8px auto; background-image: url('./image/hide.png'); background-repeat: no-repeat; background-size: 18.71px 16.39px; background-position: center; } } } } }
感謝每次遇到的問題。初學者的筆記