uni-app拖拽悬浮球优化
<div>使用uni-app拖拽悬浮球,插件不错 ,插件地址 ext.dcloud.net.cn/plugin?id=5…<div>
<p> </p>
<div>
<p>插件挺不错的,有几点需求我改了下<br>1、背景图片保持纵横比缩放图片,使用aspectFit好点<br>2、初始化球位置时使用%比较符合实际,如果放到右底部使用px还要适配<br>3、拖拽超出边框没有做限制。</p>
</div>
</div>
</div>
<p>
<br><br></p>
<div class="cnblogs_code">
<pre><template>
<view class="holdon" >
<imageclass="ball" :style="'left:'+(moveX == 0 & x>0? x+'%':moveX+'px')+';top:'+(moveY == 0 & y>0? y+'%':moveY+'px')"<span style="color: rgba(0, 0, 0, 1)">
@touchstart</span>="drag_start" @touchmove.prevent="drag_hmove" :src="image"mode="aspectFit">
</image>
</view>
</template>
<script><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
props: {
x: {
type: Number,
</span><span style="color: rgba(0, 0, 255, 1)">default</span>:0<span style="color: rgba(0, 0, 0, 1)">
},
y: {
type: Number,
</span><span style="color: rgba(0, 0, 255, 1)">default</span>:0<span style="color: rgba(0, 0, 0, 1)">
},
image:{
type:String,
</span><span style="color: rgba(0, 0, 255, 1)">default</span>: ''<span style="color: rgba(0, 0, 0, 1)">
}
},
data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
start:[</span>0,0<span style="color: rgba(0, 0, 0, 1)">],
moveY:</span>0<span style="color: rgba(0, 0, 0, 1)">,
moveX:</span>0<span style="color: rgba(0, 0, 0, 1)">,
windowWidth:</span>''<span style="color: rgba(0, 0, 0, 1)">,
windowHeight:</span>''<span style="color: rgba(0, 0, 0, 1)">,
}
},
mounted() {
const { windowWidth, windowHeight } </span>=<span style="color: rgba(0, 0, 0, 1)"> uni.getSystemInfoSync();
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.windowWidth =<span style="color: rgba(0, 0, 0, 1)"> windowWidth
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.windowHeight =<span style="color: rgba(0, 0, 0, 1)"> windowHeight
},
methods: {
drag_start(event){
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.start= event.touches.clientX-<span style="color: rgba(0, 0, 0, 1)">event.target.offsetLeft;
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.start= event.touches.clientY-<span style="color: rgba(0, 0, 0, 1)">event.target.offsetTop;
},
drag_hmove(event){
let tag </span>=<span style="color: rgba(0, 0, 0, 1)"> event.touches;
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(tag.clientX < 0<span style="color: rgba(0, 0, 0, 1)"> ){
tag[</span>0].clientX = 0<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(tag.clientY < 0<span style="color: rgba(0, 0, 0, 1)"> ){
tag[</span>0].clientY = 0<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(tag.clientX > <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.windowWidth ){
tag[</span>0].clientX = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.windowWidth
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(tag.clientY > <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.windowHeight ){
tag[</span>0].clientY = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.windowHeight
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.moveX = tag.clientX-<span style="color: rgba(0, 0, 255, 1)">this</span>.start;
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.moveY = tag.clientY-<span style="color: rgba(0, 0, 255, 1)">this</span>.start;
}
}}
</span></script>
<style lang="less"><span style="color: rgba(0, 0, 0, 1)">
.holdon{
width: </span>100%<span style="color: rgba(0, 0, 0, 1)">;
height: </span>100%<span style="color: rgba(0, 0, 0, 1)">;
}
.ball{
width: 70upx;height: 70upx;
background:linear</span>-<span style="color: rgba(0, 0, 0, 1)">gradient(to bottom, #F8F8FF,#87CEFA);
border</span>-radius: 50%<span style="color: rgba(0, 0, 0, 1)">;
box</span>-shadow: 0 0<span style="color: rgba(0, 0, 0, 1)"> 15upx #87CEFA;
color: #fff;
font</span>-<span style="color: rgba(0, 0, 0, 1)">size: 30upx;
display: flex;justify</span>-content: center;align-<span style="color: rgba(0, 0, 0, 1)">items: center;
position: fixed </span>!<span style="color: rgba(0, 0, 0, 1)">important;
z</span>-index: 1000000<span style="color: rgba(0, 0, 0, 1)">;
}
</span></style></pre>
</div>
<p><img src="https://user-gold-cdn.xitu.io/2020/5/6/171e9418301935f4?imageslim"></p>
<p> </p>
<p><img src="https://img2020.cnblogs.com/blog/1625255/202005/1625255-20200512151804802-1242770336.png"></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
小小菜鸟一个,写此博客为了以后不要走重复坑,如有不足请多多指教<br><br>
来源:https://www.cnblogs.com/lpz1096/p/12876426.html
頁:
[1]