This commit is contained in:
xingyy 2025-02-11 14:56:19 +08:00
parent 3c69236caa
commit 48108c5e84

View File

@ -47,22 +47,18 @@ const props = defineProps({
const router = useRouter() const router = useRouter()
const dragRef = shallowRef(null) const dragRef = shallowRef(null)
//
const isDragging = ref(false) const isDragging = ref(false)
const startX = ref(0) const startX = ref(0)
const startY = ref(0) const startY = ref(0)
const left = ref(0) const left = ref(0)
const top = ref(0) const top = ref(0)
//
onMounted(() => { onMounted(() => {
const rect = dragRef.value.getBoundingClientRect() const rect = dragRef.value.getBoundingClientRect()
// right left
left.value = window.innerWidth - rect.width - parseInt(props.initialPosition.right) left.value = window.innerWidth - rect.width - parseInt(props.initialPosition.right)
top.value = parseInt(props.initialPosition.top) top.value = parseInt(props.initialPosition.top)
}) })
//
const style = computed(() => ({ const style = computed(() => ({
left: left.value ? `${left.value}px` : 'auto', left: left.value ? `${left.value}px` : 'auto',
top: top.value ? `${top.value}px` : 'auto', top: top.value ? `${top.value}px` : 'auto',
@ -70,19 +66,16 @@ const style = computed(() => ({
transition: isDragging.value ? 'none' : 'all 0.3s ease', transition: isDragging.value ? 'none' : 'all 0.3s ease',
})) }))
//
const handleDragStart = (event) => { const handleDragStart = (event) => {
event.stopPropagation() event.stopPropagation()
isDragging.value = true isDragging.value = true
const point = event.touches ? event.touches[0] : event const point = event.touches ? event.touches[0] : event
//
const rect = dragRef.value.getBoundingClientRect() const rect = dragRef.value.getBoundingClientRect()
left.value = rect.left left.value = rect.left
top.value = rect.top top.value = rect.top
//
startX.value = point.clientX - left.value startX.value = point.clientX - left.value
startY.value = point.clientY - top.value startY.value = point.clientY - top.value
@ -95,7 +88,6 @@ const handleDragStart = (event) => {
} }
} }
//
const handleDragMove = useThrottleFn((event) => { const handleDragMove = useThrottleFn((event) => {
if (!isDragging.value) return if (!isDragging.value) return
@ -111,7 +103,6 @@ const handleDragMove = useThrottleFn((event) => {
top.value = Math.min(Math.max(0, point.clientY - startY.value), maxY) top.value = Math.min(Math.max(0, point.clientY - startY.value), maxY)
}, 16) }, 16)
//
const handleEdgeSnap = useThrottleFn(() => { const handleEdgeSnap = useThrottleFn(() => {
const rect = dragRef.value.getBoundingClientRect() const rect = dragRef.value.getBoundingClientRect()
const centerX = rect.left + rect.width / 2 const centerX = rect.left + rect.width / 2
@ -121,7 +112,6 @@ const handleEdgeSnap = useThrottleFn(() => {
: 16 : 16
}, 100) }, 100)
//
const handleDragEnd = () => { const handleDragEnd = () => {
if (!isDragging.value) return if (!isDragging.value) return
@ -134,14 +124,12 @@ const handleDragEnd = () => {
document.removeEventListener('touchend', handleDragEnd) document.removeEventListener('touchend', handleDragEnd)
} }
//
const handleClick = (event) => { const handleClick = (event) => {
event.stopPropagation() event.stopPropagation()
if (!isDragging.value) { if (!isDragging.value) {
handleReturnLive() handleReturnLive()
} }
} }
const handleReturnLive = () => { const handleReturnLive = () => {
router.push('/') router.push('/')
props.onClose() props.onClose()
@ -151,7 +139,6 @@ const handleClose = (event) => {
event?.stopPropagation() event?.stopPropagation()
props.onClose() props.onClose()
} }
onBeforeUnmount(() => { onBeforeUnmount(() => {
document.removeEventListener('mousemove', handleDragMove) document.removeEventListener('mousemove', handleDragMove)
document.removeEventListener('mouseup', handleDragEnd) document.removeEventListener('mouseup', handleDragEnd)
@ -159,17 +146,15 @@ onBeforeUnmount(() => {
document.removeEventListener('touchend', handleDragEnd) document.removeEventListener('touchend', handleDragEnd)
}) })
</script> </script>
<style scoped> <style scoped>
.fixed { .fixed {
will-change: transform, opacity; will-change: transform, opacity;
touch-action: none; touch-action: none;
-webkit-user-select: none; -webkit-user-select: none;
user-select: none; user-select: none;
transform: translateZ(0); /* 开启GPU加速 */ transform: translateZ(0);
} }
/* 添加动画类 */
.min-window-enter-active, .min-window-enter-active,
.min-window-leave-active { .min-window-leave-active {
transition: transform 0.3s ease, opacity 0.3s ease; transition: transform 0.3s ease, opacity 0.3s ease;