u
This commit is contained in:
1 parent
9aabf2b61c
commit
4012be621b
2 files changed
+196
-92
No files matched your search
@@ -1,143 +1,144 @@
|
||||
<script setup>
|
||||
function createRipple(event) {
|
||||
const button = event.currentTarget
|
||||
const circle = document.createElement('span')
|
||||
const diameter = Math.max(button.clientWidth, button.clientHeight)
|
||||
const radius = diameter / 2
|
||||
<script>
|
||||
export default {
|
||||
directives: {
|
||||
flash: {
|
||||
beforeMount(el) {
|
||||
el.style.position = 'relative'
|
||||
el.style.overflow = 'hidden'
|
||||
|
||||
circle.style.width = circle.style.height = `${diameter}px`
|
||||
circle.style.left = `${event.clientX - button.getBoundingClientRect().left - radius}px`
|
||||
circle.style.top = `${event.clientY - button.getBoundingClientRect().top - radius}px`
|
||||
circle.classList.add('ripple')
|
||||
el.addEventListener('click', (e) => {
|
||||
if (el.disabled)
|
||||
return
|
||||
|
||||
// 如果已有 ripple,先清除
|
||||
const ripple = button.querySelector('.ripple')
|
||||
if (ripple)
|
||||
ripple.remove()
|
||||
const rect = el.getBoundingClientRect()
|
||||
const size = Math.max(rect.width, rect.height) * 2
|
||||
const flash = document.createElement('span')
|
||||
|
||||
button.appendChild(circle)
|
||||
flash.style.position = 'absolute'
|
||||
flash.style.width = flash.style.height = `${size}px`
|
||||
flash.style.left = `${(rect.width - size) / 2}px`
|
||||
flash.style.top = `${(rect.height - size) / 2}px`
|
||||
flash.style.background = 'rgba(255,255,255,0.2)'
|
||||
flash.style.borderRadius = '50%'
|
||||
flash.style.pointerEvents = 'none'
|
||||
flash.style.transform = 'scale(0)'
|
||||
flash.style.opacity = '0.4'
|
||||
flash.style.transition = 'transform 0.3s ease-out, opacity 0.3s ease-out'
|
||||
|
||||
el.appendChild(flash)
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
flash.style.transform = 'scale(1)'
|
||||
flash.style.opacity = '0'
|
||||
})
|
||||
|
||||
flash.addEventListener('transitionend', () => flash.remove())
|
||||
})
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="button-demo">
|
||||
<button class="btn btn-primary" @click="createRipple($event)">
|
||||
<span>主按钮</span>
|
||||
<h3>现代后台按钮 - 瞬态扩散 + 内凹反馈</h3>
|
||||
<button v-flash class="btn btn-primary">
|
||||
主按钮
|
||||
</button>
|
||||
<button class="btn btn-outline" @click="createRipple($event)">
|
||||
<span>次按钮</span>
|
||||
<button v-flash class="btn btn-secondary">
|
||||
次按钮
|
||||
</button>
|
||||
<button class="btn btn-warning" @click="createRipple($event)">
|
||||
<span>警告按钮</span>
|
||||
<button v-flash class="btn btn-danger">
|
||||
危险按钮
|
||||
</button>
|
||||
<button class="btn btn-danger" @click="createRipple($event)">
|
||||
<span>危险按钮</span>
|
||||
<button v-flash class="btn btn-link">
|
||||
链接按钮
|
||||
</button>
|
||||
<button class="btn btn-primary" disabled>
|
||||
<span>禁用按钮</span>
|
||||
禁用按钮
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
<style scoped>
|
||||
.button-demo {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
padding: 20px;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
padding: 24px;
|
||||
background: #f0f2f5;
|
||||
}
|
||||
|
||||
/* 基础样式 */
|
||||
/* 基础按钮 */
|
||||
.btn {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 6px 16px;
|
||||
padding: 10px 22px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
border-radius: 4px;
|
||||
border-radius: 6px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.2s ease-in-out;
|
||||
border: 1px solid transparent;
|
||||
outline: none;
|
||||
user-select: none;
|
||||
|
||||
span {
|
||||
position: relative;
|
||||
z-index: 1; // 文字在上层
|
||||
}
|
||||
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
overflow: hidden;
|
||||
|
||||
&:disabled {
|
||||
opacity: 0.6;
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* 水波纹效果 */
|
||||
.ripple {
|
||||
position: absolute;
|
||||
border-radius: 50%;
|
||||
transform: scale(0);
|
||||
transform-origin: center; // 保证扩散中心
|
||||
animation: ripple 600ms linear;
|
||||
background-color: rgba(255, 255, 255, 0.5);
|
||||
pointer-events: none;
|
||||
z-index: 0; // 在文字下
|
||||
&:focus-visible {
|
||||
box-shadow: 0 0 0 3px rgba(0, 100, 250, 0.15);
|
||||
}
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
filter: brightness(1.08);
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
&:active:not(:disabled) {
|
||||
filter: brightness(0.95);
|
||||
box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes ripple {
|
||||
to {
|
||||
transform: scale(4);
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== 按钮类型 ===== */
|
||||
/* 主按钮 */
|
||||
.btn-primary {
|
||||
background-color: #0064fa;
|
||||
background: linear-gradient(180deg, #0072ff, #0053d6);
|
||||
color: #fff;
|
||||
border-color: #0064fa;
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background-color: #0053d6;
|
||||
}
|
||||
|
||||
&:active:not(:disabled) {
|
||||
background-color: #0041ad;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-outline {
|
||||
background-color: #fff;
|
||||
/* 次按钮 */
|
||||
.btn-secondary {
|
||||
background: #fff;
|
||||
border: 1px solid #d9d9d9;
|
||||
color: #0064fa;
|
||||
border-color: #0064fa;
|
||||
}
|
||||
|
||||
/* 危险按钮 */
|
||||
.btn-danger {
|
||||
background: linear-gradient(180deg, #ef4444, #b91c1c);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* 链接按钮 */
|
||||
.btn-link {
|
||||
background: transparent;
|
||||
color: #0064fa;
|
||||
padding: 8px 16px;
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background-color: #f3f7ff;
|
||||
text-decoration: underline;
|
||||
background: rgba(0, 100, 250, 0.05);
|
||||
}
|
||||
|
||||
&:active:not(:disabled) {
|
||||
background-color: #e1ecff;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-warning {
|
||||
background-color: #f59e0b;
|
||||
border-color: #f59e0b;
|
||||
color: #fff;
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background-color: #d97706;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background-color: #dc2626;
|
||||
border-color: #dc2626;
|
||||
color: #fff;
|
||||
|
||||
&:hover:not(:disabled) {
|
||||
background-color: #b91c1c;
|
||||
background: rgba(0, 100, 250, 0.1);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user