https://codepen.io/mprquinn/pen/OmOMrR/
来源:Mike Quinn
以下代码在将鼠标悬停在链接上时触发动画。据我了解代码,x 和 y 坐标应在每次调用函数时更新位置,因为 getBoundingClientRect() 应该在文档滚动时更新坐标...
如果您将鼠标悬停在链接上而不滚动页面,您将看到动画按预期环绕链接,但如果滚动文档,则在链接上方触发动画。我在控制台中注意到滚动文档并调用 getBoundingClientRect() 时 X 和 Y 没有更新......
const links = document.querySelectorAll('a');
links.forEach(link => link.addEventListener('mouseenter', shootLines));
function shootLines(e) {
const itemDim = this.getBoundingClientRect(),
itemSize = {
x: itemDim.right - itemDim.left,
y: itemDim.bottom - itemDim.top,
},
shapes = ['line'],
colors = ['#2FB5F3',
'#FF0A47',
'#FF0AC2',
'#47FF0A'];
const chosenC = Math.floor(Math.random() * colors.length),
chosenS = Math.floor(Math.random() * shapes.length);
// create shape
const burst = new mojs.Burst({
left: itemDim.left + (itemSize.x/2),
top: itemDim.top + (itemSize.y/2),
radiusX: itemSize.x,
radiusY: itemSize.y,
count: 8,
children: {
shape: shapes[chosenS],
radius: 10,
scale: {0.8: 1},
fill: 'none',
points: 7,
stroke: colors[chosenC],
strokeDasharray: '100%',
strokeDashoffset: { '-100%' : '100%' },
duration: 450,
delay: 100,
easing: 'quad.out',
isShowEnd: false,
}
});
burst.play();
}
.container {
margin-top: 20%;
height: 110vh;
}
<div class="container"><a href="javascript:void(0);">test</a></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mo-js/0.288.2/mo.min.js"></script>