DDR爱好者之家 Design By 杰米
悬浮球插件简单的效果图:
很遗憾,没找到太好的视频转gif的软件,压缩出来的大小超过了限制,就不放图了
可以参考其他人的图,效果一致:
简单实用案例:
<!-- 给定一个初始位置position,插槽中填写想滑动的部分 --> <xuanfuqiu :position="position"> <d-add-button @click="addPigFarm" add-item="猪场"></d-add-button> </xuanfuqiu>
原理示意图
请结合代码注释来理解
悬浮球插件代码如下:
<template>
<div>
<div class="xuanfu" id="moveDiv" :style="position"
@mousedown="down" @touchstart="down"
@mousemove="move" @touchmove="move"
@mouseup="end" @touchend="end">
<slot></slot>
</div>
</div>
</template>
<script>
export default {
name: "",
components: {},
props: {
// 通过position来设置初始定位
position: {
type: Object,
default: function() {
return {
top: "32.25rem",
left: "18.34375rem"
}
}
},
// 通过fixed来禁用自由移动
fixed: {
type: Boolean,
default: false
}
},
data() {
return {
flags: false,
positionTemp: { x: 0, y: 0 }, // 记录手指点击的位置
nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
}
},
watch: {},
computed: {},
methods: {
// 实现移动端拖拽
down(){
if (this.fixed) {
return
}
this.flags = true;
var touch;
// 该if判断是用touch还是mouse来移动
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
this.positionTemp.x = touch.clientX; // 手指点击后的位置
this.positionTemp.y = touch.clientY;
this.dx = moveDiv.offsetLeft; // 移动的div元素的位置
this.dy = moveDiv.offsetTop;
// console.log("moveDiv.offsetLeft", moveDiv.offsetLeft)
// console.log("touch.clientX", touch.clientX)
},
move(){
if(this.flags) {
var touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
this.nx = touch.clientX - this.positionTemp.x; // 手指移动的变化量
this.ny = touch.clientY - this.positionTemp.y;
this.xPum = this.dx + this.nx; // 移动后,div元素的位置
this.yPum = this.dy + this.ny;
let windowWidth = document.documentElement.clientWidth
let windowHeight = document.documentElement.clientHeight
// console.log("window.clientWidth", windowWidth)
// console.log(this.xPum)
// console.log(" moveDiv.clientWidth", moveDiv.clientWidth)
if (this.xPum > 0 && (this.xPum + moveDiv.clientWidth < windowWidth)) {
// movediv的左右边,未出界
moveDiv.style.left = this.xPum + "px";
} else if (this.xPum <= 0) {
// 左边出界,则左边缘贴边
moveDiv.style.left = 0 + "px";
} else if (this.xPum + moveDiv.clientWidth >= windowWidth) {
// 右边缘出界
moveDiv.style.left = (windowWidth - moveDiv.clientWidth) + "px";
// console.log("dx", windowWidth - moveDiv.clientWidth)
}
// 上下未出界
if (this.yPum > 0 && (this.yPum + moveDiv.clientHeight < windowHeight)) {
moveDiv.style.top = this.yPum +"px";
} else if (this.yPum <= 0) {
// 上边缘出界
moveDiv.style.top = 0 + "px"
} else if (this.yPum + moveDiv.clientHeight >= windowHeight) {
// 下边缘
// console.log("windowHeight:", windowHeight)
// console.log("moveDiv.clientHeight:", moveDiv.clientHeight)
// console.log(this.yPum + moveDiv.clientHeight)
moveDiv.style.top = windowHeight - moveDiv.clientHeight + "px"
}
// 阻止页面的滑动默认事件,为了只让悬浮球滑动,其他部分不滑动;如果碰到滑动问题,1.2 请注意是否获取到 touchmove, 系统默认passive: true,无法使用preventDefault
// document.addEventListener("touchmove", function(){
// event.preventDefault();
// }, { passive: false });
// document.addEventListener("mousemove", function(){
// event.preventDefault();
// }, { passive: false });
document.addEventListener("touchmove", this.preventDefault, { passive: false })
document.addEventListener("mousemove", this.preventDefault, { passive: false })
}
},
//鼠标释放时候的函数,鼠标释放,移除之前添加的侦听事件,将passive设置为true,不然背景会滑动不了
end(){
this.flags = false
// 注意事项,在添加和删除监听事件时,其function必须是同名的函数,不能为匿名函数。
document.removeEventListener('touchmove',this.preventDefault, false)
document.removeEventListener('mousemove',this.preventDefault, false)
// 下面两句是保证在移除监听事件后,除了悬浮球的部分还能够滑动,如果不添加,则无法滑动
document.addEventListener("touchmove", function(e) {
window.event.returnValue = true
})
document.addEventListener("mousemove", function(e) {
window.event.returnValue = true
})
},
preventDefault(e) {
e.preventDefault()
}
},
created() {},
mounted() {}
}
</script>
<style lang="scss" scoped>
.xuanfu {
/* 如果碰到滑动问题,1.3 请检查 z-index。z-index需比web大一级*/
z-index: 999;
position: fixed; // 这里的定位方式有待考量,fixed的话存在未知设置不合理,跑出屏幕不显示的问题
}
</style>
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2025年10月26日
2025年10月26日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]

