【发布时间】:2023-03-09 23:31:01
【问题描述】:
我有一个 Nuxt.js 项目,我正在使用自定义按钮。
按钮是一个链接,带有一个svg path和一个span。我还有按钮动画,它在 mouse over 和 mouse out 事件 上触发。
这是我的按钮代码。
<template>
<a class="button green" href="/"
@mouseover="buttonEnter"
@mouseout="buttonLeave">
<svg viewBox="0 0 180 60">
<path d="M10,10 C10,10 50,9.98999977 90,9.98999977 C130,9.98999977 170,10 170,10 C170,10 170.009995,20 170.009995,30 C170.009995,40 170,50 170,50 C170,50 130,50.0099983 90,50.0099983 C50,50.0099983 10,50 10,50 C10,50 9.98999977,40 9.98999977,30 C9.98999977,20 10,10 10,10 Z"/>
</svg>
<span>Go Home</span>
</a>
</template>
<script>
import { buttonEnter } from '~/assets/animate'
import { buttonleave } from '~/assets/animate'
export default {
methods: {
buttonEnter(event) {
const buttonPath = event.currentTarget.querySelector('path')
const buttonSpan = event.currentTarget.querySelector('span')
buttonEnter(buttonPath, buttonSpan)
},
buttonLeave(event) {
const buttonPath = event.currentTarget.querySelector('path')
const buttonSpan = event.currentTarget.querySelector('span')
buttonleave(buttonPath, buttonSpan)
},
},
}
</script>
我在很多页面中都使用了这个按钮,我觉得我在复制自己,拥有一个按钮组件会让事情变得更好、更整洁。问题是我是 Vue 的新手,但我没有做到这一点。有人可以指出我或给我一个示例代码如何正确地编写可重用组件吗?
这里还有动画的 buttonEnter 和 buttonleave 函数。
import anime from "animejs";
export function buttonEnter(buttonPath, buttonSpan) {
anime.remove([buttonPath, buttonSpan]);
anime({
targets: buttonPath,
d:
"M10,10 C10,10 50,7 90,7 C130,7 170,10 170,10 C170,10 172,20 172,30 C172,40 170,50 170,50 C170,50 130,53 90,53 C50,53 10,50 10,50 C10,50 8,40 8,30 C8,20 10,10 10,10 Z",
elasticity: 700,
offset: 0
});
anime({
targets: buttonSpan,
scale: 1.15,
duration: 800,
offset: 0
});
}
export function buttonleave(buttonPath, buttonSpan) {
anime.remove([buttonPath, buttonSpan]);
anime({
targets: buttonPath,
d:
"M10,10 C10,10 50,9.98999977 90,9.98999977 C130,9.98999977 170,10 170,10 C170,10 170.009995,20 170.009995,30 C170.009995,40 170,50 170,50 C170,50 130,50.0099983 90,50.0099983 C50,50.0099983 10,50 10,50 C10,50 9.98999977,40 9.98999977,30 C9.98999977,20 10,10 10,10 Z",
elasticity: 700,
offset: 0
});
anime({
targets: buttonSpan,
scale: 1,
duration: 800,
offset: 0
});
}
这是按钮的演示 codesandbox
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component nuxt.js