请告诉我如何修复代码。现在,如果有一个 querySelector,那么脚本可以工作,但只有一个 h1,如果我们将其更改为 getElementsByTagName(),那么脚本将完全停止工作。
var h = document.querySelector('h1');
var p = h.getBoundingClientRect();
var c = document.querySelector('.cursor');
document.body.onmousemove = function(e) {
/*Adjust the position of the cursor*/
c.style.left = e.clientX - 50 + 'px';
c.style.top = e.clientY - 50 + 'px';
/*Adjust the radial-gradient*/
h.style.setProperty('--x', (e.clientX - p.top) + 'px');
h.style.setProperty('--y', (e.clientY - p.left) + 'px');
}
h1 {
color: #000;
display: inline-block;
margin: 50px;
text-align: center;
position: relative;
}
h1:before {
position: absolute;
content: attr(data-text);
color: #fff;
background: #000;
clip-path: circle(50px at var(--x, -100px) var(--y, -100px));
}
.cursor {
position: fixed;
background: #000;
border-radius: 50%;
width: 100px;
height: 100px;
top: calc(calc(var(--y) * 1px) - 50px);
left: calc(calc(var(--x) * 1px) - 50px);
z-index: -2;
}
<h1 data-text="WORK1">WORK1</h1>
<h1 data-text="WORK2">WORK2</h1>
<h1 data-text="WORK">WORK</h1>
<span class="cursor"></span>
