我试图在我的站点内创建一个跟随光标的球:www.effevisual.altervista.org 使用 wordpress 和 divi 主题。
我多次尝试这段代码没有任何问题,但实际上它看起来像对象挡住了球。
如果可能的话,我还想在光标悬停在链接上时将球更改为较低的不透明度。
<body onload = "followMouse();">
<div class="wrap">
<div id="ball"></div>
</div></body>
.wrap {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
#ball {
width: 20px;
height: 20px;
background: #0034fc;
border-radius: 50%;
position: absolute;
left: 50%;
top: 50%;
margin: -10px 0 0 -10px;
pointer-events: none;
}
var $ = document.querySelector.bind(document);
var $on = document.addEventListener.bind(document);
var xmouse, ymouse;
$on('mousemove', function (e) {
xmouse = e.clientX || e.pageX;
ymouse = e.clientY || e.pageY;
});
var ball = $('#ball');
var x = void 0,
y = void 0,
dx = void 0,
dy = void 0,
tx = 0,
ty = 0,
key = -1;
var followMouse = function followMouse() {
key = requestAnimationFrame(followMouse);
if(!x || !y) {
x = xmouse;
y = ymouse;
} else {
dx = (xmouse - x) * 0.125;
dy = (ymouse - y) * 0.125;
if(Math.abs(dx) + Math.abs(dy) < 0.1) {
x = xmouse;
y = ymouse;
} else {
x += dx;
y += dy;
}
}
ball.style.left = x + 'px';
ball.style.top = y + 'px';
};
</script>
任何消息错误,只是球没有正确跟随。
最佳答案
所以我发现您正在对类 .et_pb_code_0
应用转换。 transform: transformX(-89px) translateY(-81px) rotateX(0deg) rotateY(0deg) rotateZ(90deg);
仅此一项就将你的球发送到不同的方向,左右上下现在上下左右。
除此之外,您的包装器和球类包含在许多其他 div 中,这些 div 将其定位在远离页面左上角的位置。
我可以通过将 wrap
类拖到 main-content1
类下的顶部来在检查器中修复它。
您还必须将 position:fixed
应用于 wrap
类,以使其始终显示在屏幕上。以及页面下方公园的 z-index
。和 pointer-events:none
这样你就可以点击链接了。
像这样的东西可以让你开始:
jQuery( "li").mouseenter(function() {
jQuery( "#ball").fadeTo( "慢", 0, function() {
//动画完成。
});
});
jQuery( "li").mouseleave(函数() {
jQuery( "#ball").fadeTo( "慢", 1, function() {
//动画完成。
});
});
关于javascript - 如何修复仅在 wordpress 网站上不起作用的 javascript onmousemove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57916172/