【问题标题】:Animated Eyes - Why are they following when scrolling?动画眼睛 - 为什么滚动时它们会跟随?
【发布时间】:2012-06-22 14:36:58
【问题描述】:

如果您导航到: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));
}

【问题讨论】:

  • 天哪,看起来很吓人:)
  • 也许你应该复制/粘贴移动眼睛的代码,问题肯定存在
  • 鼠标移动通过eyes.js中的基于jQuery的脚本转换为眼睛移动。
  • 请通过发布相关代码使这个问题“自成一体”。挖掘整个网站的代码不仅不切实际,未来的读者在修复后也不会从中受益。见meta.stackexchange.com/a/129787

标签: jquery cursor fixed


【解决方案1】:

这一行:

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

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

换行试试

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

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

弥补这一点。

【讨论】:

  • 太棒了!差不多就这样了。现在眼睛不会移动,但是当您向下滚动时,它们不会正确跟随鼠标。
【解决方案2】:

要在滚动时将某些元素保持在屏幕上的固定位置,您可以指定 CSS 样式属性 position: fixed

W3C documentation on position property.

更新:

当你查看源代码时,你会看到包含眼睛的 div &lt;div class="big_face_holder"&gt; 确实设置了position: fixed

【讨论】:

  • 为什么我投了反对票?问题是为什么眼睛在向下滚动时会一直盯着可见的屏幕。我想,我描述了原因。
  • 问题是,为什么向下滚动时眼睛不停留在头部内部。
  • 好的,但是通过将它们定位到 fixed 或事件更好的 relative 到它们的容器并删除改变它们位置的代码来解决这个问题不是更好吗?
  • 如果你不能改变他们的位置,那么他们就不能跟随鼠标光标。
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多