-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<script setup lang="ts"> | ||
import { | ||
useElementBounding, | ||
useEventListener, | ||
useWindowScroll, | ||
useWindowSize, | ||
useStyleTag, | ||
} from '@vueuse/core'; | ||
import { computed, onMounted, shallowRef, type CSSProperties } from 'vue'; | ||
useStyleTag( | ||
` | ||
body::-webkit-scrollbar { | ||
display: none; | ||
} | ||
html { | ||
scrollbar-width: none; | ||
} | ||
`.trim(), | ||
); | ||
const { y, x } = useWindowScroll(); | ||
const { height: winH, width: winW } = useWindowSize(); | ||
const bodyRef = shallowRef<HTMLElement>(); // support ssr | ||
onMounted(() => { | ||
bodyRef.value = document.body; | ||
}); | ||
const body = useElementBounding(bodyRef); | ||
const yShow = computed(() => body.height.value > winH.value); | ||
const yHeight = computed(() => { | ||
const clientHeight = body.height.value; | ||
const bodyHeight = clientHeight; | ||
return (winH.value / bodyHeight) * winH.value; | ||
}); | ||
const translateY = computed(() => { | ||
const clientHeight = body.height.value; | ||
const height = yHeight.value; | ||
return (y.value / (clientHeight - winH.value)) * (winH.value - height); | ||
}); | ||
const yStyle = computed<CSSProperties>(() => { | ||
if (!yShow.value) return {}; | ||
return { | ||
transform: `translateY(${translateY.value}px)`, | ||
height: `${yHeight.value}px`, | ||
}; | ||
}); | ||
const clickBoxY = async (e: MouseEvent) => { | ||
const deltaY = | ||
yHeight.value * | ||
0.9 * | ||
(e.clientY < yHeight.value + translateY.value ? -1 : 1); | ||
const clientHeight = body.height.value; | ||
const bodyHeight = clientHeight; | ||
const height = (winH.value / bodyHeight) * winH.value; | ||
y.value += (deltaY / (winH.value - height)) * (clientHeight - winH.value); | ||
}; | ||
const yDragging = shallowRef(false); | ||
let lastYEvent: MouseEvent | undefined = undefined; | ||
const pointerdownY = (e: MouseEvent) => { | ||
lastYEvent = e; | ||
yDragging.value = true; | ||
}; | ||
useEventListener('pointermove', (e) => { | ||
if (!lastYEvent) return; | ||
const deltaY = e.clientY - lastYEvent.clientY; | ||
lastYEvent = e; | ||
const clientHeight = body.height.value; | ||
const bodyHeight = clientHeight; | ||
const height = (winH.value / bodyHeight) * winH.value; | ||
y.value += (deltaY / (winH.value - height)) * (clientHeight - winH.value); | ||
}); | ||
useEventListener('pointerup', () => { | ||
lastYEvent = undefined; | ||
yDragging.value = false; | ||
}); | ||
const xShow = computed(() => body.width.value > winW.value); | ||
const xWidth = computed(() => { | ||
const clientWidth = body.width.value; | ||
const bodyWidth = clientWidth; | ||
return (winW.value / bodyWidth) * winW.value; | ||
}); | ||
const translateX = computed(() => { | ||
const clientWidth = body.width.value; | ||
const width = xWidth.value; | ||
return (x.value / (clientWidth - winW.value)) * (winW.value - width); | ||
}); | ||
const xStyle = computed<CSSProperties>(() => { | ||
if (!xShow.value) return {}; | ||
return { | ||
transform: `translateX(${translateX.value}px)`, | ||
width: `${xWidth.value}px`, | ||
}; | ||
}); | ||
const clickBoxX = (e: MouseEvent) => { | ||
const deltaX = | ||
xWidth.value * 0.9 * (e.clientX < xWidth.value + translateX.value ? -1 : 1); | ||
const clientWidth = body.width.value; | ||
const bodyWidth = clientWidth; | ||
const width = (winW.value / bodyWidth) * winW.value; | ||
const newX = | ||
x.value + (deltaX / (winW.value - width)) * (clientWidth - winW.value); | ||
x.value = newX; | ||
}; | ||
const xDragging = shallowRef(false); | ||
let lastXEvent: MouseEvent | undefined = undefined; | ||
const pointerdownX = (e: MouseEvent) => { | ||
lastXEvent = e; | ||
xDragging.value = true; | ||
}; | ||
useEventListener('pointermove', (e) => { | ||
if (!lastXEvent) return; | ||
const deltaX = e.clientX - lastXEvent.clientX; | ||
lastXEvent = e; | ||
const clientWidth = body.width.value; | ||
const bodyWidth = clientWidth; | ||
const width = (winW.value / bodyWidth) * winW.value; | ||
x.value += (deltaX / (winW.value - width)) * (clientWidth - winW.value); | ||
}); | ||
useEventListener('pointerup', () => { | ||
lastXEvent = undefined; | ||
xDragging.value = false; | ||
}); | ||
useEventListener('selectstart', (e) => { | ||
if (lastXEvent || lastYEvent) { | ||
e.preventDefault(); | ||
} | ||
}); | ||
</script> | ||
<template> | ||
<div fixed class="BodyScrollbar"> | ||
<div | ||
v-show="yShow" | ||
scrollbar-y | ||
fixed | ||
right-2px | ||
top-0 | ||
bottom-0 | ||
z-100 | ||
w-8px | ||
@pointerdown="pointerdownY" | ||
@click="clickBoxY" | ||
> | ||
<div | ||
@click.stop | ||
w-full | ||
bg="#909399" | ||
opacity-30 | ||
hover:opacity="50" | ||
:style="yStyle" | ||
:class="{ | ||
'opacity-50': yDragging, | ||
}" | ||
></div> | ||
</div> | ||
<div | ||
v-if="xShow" | ||
scrollbar-x | ||
fixed | ||
bottom-2px | ||
left-0 | ||
right-0 | ||
z-100 | ||
h-8px | ||
@pointerdown="pointerdownX" | ||
@click="clickBoxX" | ||
> | ||
<div | ||
@click.stop | ||
h-full | ||
bg="#909399" | ||
opacity-30 | ||
hover:opacity="50" | ||
:style="xStyle" | ||
:class="{ | ||
'opacity-50': xDragging, | ||
}" | ||
></div> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters