提交 6cae6489 authored 作者: lidongxu's avatar lidongxu

refactor(levitatedsphere): 重构悬浮工具球

从 Vue2 重构成 Vue3 的语法
上级 d292a5f1
...@@ -15,9 +15,8 @@ ...@@ -15,9 +15,8 @@
</div> </div>
</template> </template>
<script> <script setup>
export default { defineProps({
props: {
/** /**
* 数据格式: [ * 数据格式: [
* { // 每一列组 * { // 每一列组
...@@ -38,7 +37,7 @@ export default { ...@@ -38,7 +37,7 @@ export default {
* orient: '一组内的排列方式' // verticalAlign 垂直排列(默认),horizontal 水平排列 * orient: '一组内的排列方式' // verticalAlign 垂直排列(默认),horizontal 水平排列
* } * }
* ] * ]
*/ */
legendData: { legendData: {
type: Array, type: Array,
default: () => [] default: () => []
...@@ -47,13 +46,11 @@ export default { ...@@ -47,13 +46,11 @@ export default {
type: String, type: String,
default: 'horizontal' // 每组之间排列方式: verticalAlign 垂直,horizontal 水平 default: 'horizontal' // 每组之间排列方式: verticalAlign 垂直,horizontal 水平
} }
}, })
methods: { const emit = defineEmits(['clickItem']);
handleClick(item) { const handleClick = (item) => {
item.effective = !item.effective item.effective = !item.effective
this.$emit('clickItem') emit('clickItem', item)
}
}
} }
</script> </script>
...@@ -179,7 +176,8 @@ export default { ...@@ -179,7 +176,8 @@ export default {
.e-icon { .e-icon {
background: lightgray !important; background: lightgray !important;
border-color: lightgray !important; border-color: lightgray !important;
&:before{
&:before {
border-color: lightgray !important; border-color: lightgray !important;
} }
} }
......
...@@ -22,10 +22,8 @@ ...@@ -22,10 +22,8 @@
</div> </div>
</template> </template>
<script> <script setup>
export default { const props = defineProps({
name: 'LevitatedSphere',
props: {
itemWidth: { itemWidth: {
// 悬浮按钮宽度 // 悬浮按钮宽度
type: Number, type: Number,
...@@ -46,76 +44,68 @@ export default { ...@@ -46,76 +44,68 @@ export default {
type: Number, type: Number,
default: 0.85, default: 0.85,
} }
}, })
data() {
return { const levitatedSphere = ref(null)
top: 0, const top = ref(0)
left: 0, const left = ref(0)
clientWidth: 0, const clientWidth = ref(0)
clientHeight: 0, const clientHeight = ref(0)
drawer: false, const drawer = ref(false)
downPoint: {}, const downPoint = reactive({})
upPoint: {} const upPoint = reactive({})
};
}, const resize = () => {
created() { clientWidth.value = document.documentElement.clientWidth;
this.resize() clientHeight.value = document.documentElement.clientHeight;
}, left.value = clientWidth.value - props.itemWidth - props.gapWidth;
mounted() { top.value = clientHeight.value * props.coefficientHeight;
window.addEventListener('resize', this.resize) }
this.$nextTick(() => { resize()
const sphereBtn = this.$refs.levitatedSphere;
onMounted(() => {
window.addEventListener('resize', resize)
})
nextTick(() => {
let isDown = false let isDown = false
sphereBtn.addEventListener('mousedown', (e) => { levitatedSphere.value.addEventListener('mousedown', (e) => {
e.preventDefault(); e.preventDefault();
sphereBtn.style.transition = 'none'; levitatedSphere.value.style.transition = 'none';
this.downPoint = { downPoint.x = e.clientX
x: e.clientX, downPoint.y = e.clientY
y: e.clientY
};
isDown = true isDown = true
}); })
// 在拖拽过程中,组件应该跟随手指的移动而移动 // 在拖拽过程中,组件应该跟随手指的移动而移动
window.addEventListener('mousemove', (e) => { window.addEventListener('mousemove', (e) => {
e.preventDefault(); e.preventDefault();
if (isDown) { if (isDown) {
let touch = e; let touch = e;
// 限制 left 和 top 的值,使其不会超出可视区域 // 限制 left 和 top 的值,使其不会超出可视区域
this.left = Math.max(this.gapWidth, Math.min(touch.clientX - 20, this.clientWidth - this.itemWidth - this.gapWidth)); left.value = Math.max(props.gapWidth, Math.min(touch.clientX - 20, clientWidth.value - props.itemWidth - props.gapWidth));
this.top = Math.max(0, Math.min(touch.clientY - 25, this.clientHeight - this.itemHeight)); top.value = Math.max(0, Math.min(touch.clientY - 25, clientHeight.value - props.itemHeight));
} }
}); });
// 拖拽结束后,重新调整组件的位置并重新设置过渡动画 // 拖拽结束后,重新调整组件的位置并重新设置过渡动画
window.addEventListener('mouseup', (e) => { window.addEventListener('mouseup', (e) => {
sphereBtn.style.transition = 'all 0.5s'; levitatedSphere.value.style.transition = 'all 0.5s';
if (this.left > document.documentElement.clientWidth / 2) { if (left.value > document.documentElement.clientWidth / 2) {
this.left = document.documentElement.clientWidth - this.itemWidth - this.gapWidth; left.value = document.documentElement.clientWidth - props.itemWidth - props.gapWidth;
} else { } else {
this.left = this.gapWidth; left.value = props.gapWidth;
} }
isDown = false isDown = false
this.upPoint = { upPoint.x = e.clientX
x: e.clientX, upPoint.y = e.clientY
y: e.clientY })
}; });
});
}); onBeforeUnmount(() => {
}, window.removeEventListener('resize', resize)
beforeDestroy() { })
window.removeEventListener('resize', this.resize)
}, const handleClick = () => {
methods: { if (downPoint.x === upPoint.x && downPoint.y === upPoint.y) {
resize() { drawer.value = !drawer.value
this.clientWidth = document.documentElement.clientWidth;
this.clientHeight = document.documentElement.clientHeight;
this.left = this.clientWidth - this.itemWidth - this.gapWidth;
this.top = this.clientHeight * this.coefficientHeight;
},
handleClick() {
if (this.downPoint.x === this.upPoint.x && this.downPoint.y === this.upPoint.y) {
this.drawer = !this.drawer
}
}
} }
} }
</script> </script>
...@@ -139,9 +129,12 @@ export default { ...@@ -139,9 +129,12 @@ export default {
padding: 10px; padding: 10px;
display: grid; display: grid;
grid-template-columns: repeat(3, 1fr); grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(auto-fill, 30px); /* 明确指定行高为100px */ grid-template-rows: repeat(auto-fill, 30px);
grid-auto-rows: 30px; /* 自动生成的行高也为100px */ /* 明确指定行高为100px */
grid-gap: 20px 5px; /* 上下间距20px,左右间距5px */ grid-auto-rows: 30px;
/* 自动生成的行高也为100px */
grid-gap: 20px 5px;
/* 上下间距20px,左右间距5px */
width: 100%; width: 100%;
.el-button { .el-button {
...@@ -149,6 +142,7 @@ export default { ...@@ -149,6 +142,7 @@ export default {
margin: 0; margin: 0;
} }
} }
@media (max-width: 768px) { @media (max-width: 768px) {
.wrap { .wrap {
width: 30px; width: 30px;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论