75 lines
1.9 KiB
Vue
75 lines
1.9 KiB
Vue
<script setup lang="ts">
|
|
import { ref } from "vue";
|
|
import { Button } from "@heroui-vue/components";
|
|
const waves = ref<{ id: number; x: number; y: number; size: number }[]>([]);
|
|
let id = 0;
|
|
function add(target: HTMLElement, x: number, y: number) {
|
|
const size = Math.max(target.offsetWidth, target.offsetHeight) * 2;
|
|
waves.value.push({ id: ++id, x: x - size / 2, y: y - size / 2, size });
|
|
}
|
|
function ripple(event: PointerEvent) {
|
|
const target = event.currentTarget as HTMLElement;
|
|
const rect = target.getBoundingClientRect();
|
|
add(target, event.clientX - rect.left, event.clientY - rect.top);
|
|
}
|
|
function keyboardRipple(event: KeyboardEvent) {
|
|
if (event.repeat || !["Enter", " "].includes(event.key)) return;
|
|
const target = event.currentTarget as HTMLElement;
|
|
add(target, target.offsetWidth / 2, target.offsetHeight / 2);
|
|
}
|
|
function remove(id: number) {
|
|
waves.value = waves.value.filter((wave) => wave.id !== id);
|
|
}
|
|
</script>
|
|
<template>
|
|
<Button
|
|
variant="secondary"
|
|
class="ripple-button"
|
|
@pointerdown="ripple"
|
|
@keydown="keyboardRipple"
|
|
>
|
|
点我
|
|
<span
|
|
v-for="wave in waves"
|
|
:key="wave.id"
|
|
class="ripple-wave"
|
|
:style="{
|
|
left: wave.x + 'px',
|
|
top: wave.y + 'px',
|
|
width: wave.size + 'px',
|
|
height: wave.size + 'px',
|
|
}"
|
|
aria-hidden="true"
|
|
@animationend="remove(wave.id)"
|
|
/>
|
|
</Button>
|
|
</template>
|
|
<style scoped>
|
|
.ripple-button {
|
|
overflow: hidden;
|
|
}
|
|
.ripple-wave {
|
|
position: absolute;
|
|
pointer-events: none;
|
|
border-radius: 50%;
|
|
background: currentColor;
|
|
opacity: 0.18;
|
|
transform: scale(0);
|
|
animation: button-ripple 0.6s ease-out forwards;
|
|
}
|
|
@keyframes button-ripple {
|
|
to {
|
|
transform: scale(1);
|
|
opacity: 0;
|
|
}
|
|
}
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.ripple-wave {
|
|
animation-duration: 1ms;
|
|
}
|
|
}
|
|
:global([data-reduce-motion="true"]) .ripple-wave {
|
|
animation-duration: 1ms;
|
|
}
|
|
</style>
|