gpt4 book ai didi

jquery - 动画眼睛 - 为什么滚动时它们会跟随?

转载 作者:行者123 更新时间:2023-12-01 03:48:19 25 4
gpt4 key购买 nike

如果您导航至:http://laboratory.stratusweb.co.uk/lea/ (尚未完成)

您会注意到眼睛跟随光标移动。

我不明白的是,为什么当你滚动时眼睛会向下移动?

欢迎提出任何建议!

忘记添加 eyes.js 代码:

var windowX = -1;
var windowY = -1;
jQuery(document).ready(function() {
var canvas = document.getElementById("debugCanvas");
canvas.width = document.width;
canvas.height = document.height;
jQuery(document).mousemove(function(e) {
var mousePosition = {
'x' : e.pageX,
'y' : e.pageY
};
var context = document.getElementById("debugCanvas").getContext("2d");
jQuery(".eyeContainer").each(function(i, i2) {
var eyeContainerPosition = $(this).offset();
var eyePosition = {
'x' : eyeContainerPosition.left + $(this).width() / 2 +1,
'y' : eyeContainerPosition.top - $('body').scrollTop() + $(this).height() / 2 +1
}
var slope = getSlope(eyePosition, mousePosition);
var toCenterdistance = getDistance(eyePosition, mousePosition);
var targetDistance = toCenterdistance - ($(this).width() / 2);
if(toCenterdistance > ($(this).width() / 2)) {
var x = Math.cos(Math.atan(slope)) * targetDistance;
if(eyePosition.x > mousePosition.x) {
x += mousePosition.x;
} else if(eyePosition.x < mousePosition.x) {
x = -x + mousePosition.x;
}
var y = Math.sin(Math.atan(slope)) * targetDistance;
if(eyePosition.x > mousePosition.x) {
y += mousePosition.y;
} else if(eyePosition.x < mousePosition.x) {
y = -y + mousePosition.y;
}
x -= $(this).height() / 2;
y -= $(this).height() / 2;
} else {
x = mousePosition.x - ($(this).width() / 2);
y = mousePosition.y - ($(this).width() / 2);
}
var element=$("#eyeBall_" + $(this).attr("rel"));
element.css({
'left' : x + 'px',
'top' : y + 'px',
});
});
})
});
function getSlope(loc1, loc2) {
return (loc1.y - loc2.y) / (loc1.x - loc2.x);
}
function getDistance(loc1, loc2) {
return Math.sqrt(Math.pow((loc1.x - loc2.x), 2) + Math.pow((loc1.y - loc2.y), 2));
}

最佳答案

这一行:

var eyeContainerPosition = $(this).offset();

根据页面滚动的距离返回不同的值 - 它返回相对于文档顶部的偏移量,而不是窗口顶部的偏移量。

尝试更换线路

'y' : eyeContainerPosition.top + $(this).height() / 2 +1

'y' : eyeContainerPosition.top - $('body').scrollTop() + $(this).height() / 2 +1

弥补这一点。

关于jquery - 动画眼睛 - 为什么滚动时它们会跟随?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com