简体   繁体   English

如何让眼睛在div之外跟随鼠标

[英]How to make eyes follow mouse outside of div

I'm trying to add eyes that follow the mouse to the head of my site.我正在尝试将跟随鼠标的眼睛添加到我网站的头部。 I'm using code I found and here is my jsfiddle .我正在使用我找到的代码,这是我的jsfiddle I've found many examples and posts about some object following the mouse.我找到了许多关于鼠标跟随某个对象的示例和帖子。 But the problem is that they all work only for the div they are in. To show this, I added red lines around the eyes in my example.但问题是它们都只适用于它们所在的 div。为了说明这一点,我在示例中在眼睛周围添加了红线。 The eyes follow all mouse movement while the mouse is in that box.当鼠标在该框中时,眼睛会跟随鼠标的所有移动。 But if the mouse is scrolled outside of the box, it is no longer followed.但如果鼠标滚动到框外,则不再跟随。 Is there a way to have the eyes follow the mouse no matter where the mouse is on the page?无论鼠标在页面上的哪个位置,有没有办法让眼睛跟随鼠标?

    <style>
    .move-area{/*normally use body*/
      width: 100vw;
      height: 100vh;
      padding: 10% 45%;
    }
    .container {
      width: 100%;
    }
    .eye {
      position: relative;
      display: inline-block;
      border-radius: 50%;
      height: 30px;
      width: 30px;
      background: #CCC;
    }
    .eye:after { /*pupil*/
      position: absolute;
      bottom: 17px;
      right: 10px;
      width: 10px;
      height: 10px;
      background: #000;
      border-radius: 50%;
      content: " ";
    }
    </style>

    <div style="height:200px;">
      <div class="move-area" style="height:30px;border:1px solid red;">
        <div class='.container'>
          <div class='eye'></div>
          <div class='eye'></div>
        </div>
      </div>
      <div>Text below the eyes</div>
    </div>

    <script>
    //This is a pen based off of Codewoofy's eyes follow mouse. It is just cleaned up, face removed, and then made to be a little more cartoony. https://codepen.io/Codewoofy/pen/VeBJEP

    $(".move-area").mousemove(function(event) {
      var eye = $(".eye");
      var x = (eye.offset().left) + (eye.width() / 2);
      var y = (eye.offset().top) + (eye.height() / 2);
      var rad = Math.atan2(event.pageX - x, event.pageY - y);
      var rot = (rad * (180 / Math.PI) * -1) + 180;
      eye.css({
        '-webkit-transform': 'rotate(' + rot + 'deg)',
        '-moz-transform': 'rotate(' + rot + 'deg)',
        '-ms-transform': 'rotate(' + rot + 'deg)',
        'transform': 'rotate(' + rot + 'deg)'
      });
    });
    </script>

Attach the mousemove listener to document rather than to .move-area :mousemove侦听器附加到document而不是.move-area

 $(document).mousemove(function(event) { var eye = $(".eye"); var x = (eye.offset().left) + (eye.width() / 2); var y = (eye.offset().top) + (eye.height() / 2); var rad = Math.atan2(event.pageX - x, event.pageY - y); var rot = (rad * (180 / Math.PI) * -1) + 180; eye.css({ '-webkit-transform': 'rotate(' + rot + 'deg)', '-moz-transform': 'rotate(' + rot + 'deg)', '-ms-transform': 'rotate(' + rot + 'deg)', 'transform': 'rotate(' + rot + 'deg)' }); });
 .move-area{/*normally use body*/ width: 100vw; height: 100vh; padding: 10% 45%; } .container { width: 100%; } .eye { position: relative; display: inline-block; border-radius: 50%; height: 30px; width: 30px; background: #CCC; } .eye:after { /*pupil*/ position: absolute; bottom: 17px; right: 10px; width: 10px; height: 10px; background: #000; border-radius: 50%; content: " "; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="height:200px;"> <div class="move-area" style="height:30px;border:1px solid red;"> <div class='.container'> <div class='eye'></div> <div class='eye'></div> </div> </div> <div>Text below the eyes</div> </div>

If you want "cockeyed" try this.如果你想“傻眼”试试这个。 I modified CertaniPerformance's code by separating the two eyes.我通过将两只眼睛分开来修改 CertaniPerformance 的代码。

 $(document).mousemove(function(event) { var eye = $(".eye"); var eye2 = $(".eye2"); var x = (eye.offset().left) + (eye.width() / 2); var x2 = (eye2.offset().left) + (eye2.width() / 2); var y = (eye.offset().top) + (eye.height() / 2); var y2 = (eye2.offset().top) + (eye2.height() / 2); var rad = Math.atan2(event.pageX - x, event.pageY - y); var rad2 = Math.atan2(event.pageX - x2, event.pageY - y2); var rot = (rad * (180 / Math.PI) * -1) + 180; var rot2 = (rad2 * (180 / Math.PI) * -1) + 180; eye.css({ '-webkit-transform': 'rotate(' + rot + 'deg)', '-moz-transform': 'rotate(' + rot + 'deg)', '-ms-transform': 'rotate(' + rot + 'deg)', 'transform': 'rotate(' + rot + 'deg)' }); eye2.css({ '-webkit-transform': 'rotate(' + rot2 + 'deg)', '-moz-transform': 'rotate(' + rot2 + 'deg)', '-ms-transform': 'rotate(' + rot2 + 'deg)', 'transform': 'rotate(' + rot2 + 'deg)' }); });
 .move-area { position: relative; width: 100%; margin:0; padding:0; left:30px; top:30px; } .eye { position: relative; display: inline-block; border: solid 10px black; border-radius: 50%; height: 270px; width: 270px; background: white; } .eye:after { /*pupil*/ position: absolute; top: 0; /* bottom: 34px; */ right: 90px; width: 90px; height: 90px; background: #000; border-radius: 50%; content: " "; } .eye2 { position: relative; display: inline-block; border: solid 10px black; border-radius: 50%; height: 270px; width: 270px; background: white; } .eye2:after { /*pupil*/ position: absolute; top: 0; /* bottom: 34px; */ right: 90px; width: 90px; height: 90px; background: #000; border-radius: 50%; content: " "; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div style="height:200px;"> <div class="move-area" > <div class='.container'> <div class='eye'></div> <div class='eye2'></div> </div> </div> </div>

图片跟随鼠标<div></div><div id="text_translate"><p>如何在特定的&lt;div&gt;中使图片跟随鼠标?</p><p> 我知道我可以从e.pageX &amp; e.pageY和代码document.onmousemove = followmouse;获得鼠标 position . Run the followmouse function every moment the mouse move in a page and in the followmouse function, set the picture position to the mouse position. 对于我在这里提出的确切问题(如何在特定的&lt;div&gt;中使图片跟随鼠标),我有这个想法:</p><p> 获取我的 div <em>top</em> 、 <em>left</em> 、 <em>width</em>和<em>height</em>并做一些数学运算,如果鼠标 go 离开div ,设置图片的visibility:hidden 。</p><p> 但是没有任何简单的方法可以做到这一点吗?</p></div> - Picture follow a mouse in a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让眼睛跟随 object? - How do I make the eyes follow the object? 使眼睛跟随此 SVG 中的光标 - Make eyes follow cursor in this SVG 如何让div在被拖动时跟随鼠标 - How to make div follow mouse when being dragged 眼睛跟随鼠标光标Mootools-&gt; JQuery - Eyes follow mouse cursor Mootools -> JQuery 如何使div跟随鼠标? - How to get a div to follow mouse? 我们如何使 div 在鼠标悬停时跟随 angular 中的 cursor 并在鼠标离开时消失? - How can we make the div follow the cursor in angular when mouse over and disappear when mouse is out? 当鼠标处于mousedown时,使用鼠标跟随鼠标 - Make a div follow the mouse just when the mouse is on mousedown 图片跟随鼠标<div></div><div id="text_translate"><p>如何在特定的&lt;div&gt;中使图片跟随鼠标?</p><p> 我知道我可以从e.pageX &amp; e.pageY和代码document.onmousemove = followmouse;获得鼠标 position . Run the followmouse function every moment the mouse move in a page and in the followmouse function, set the picture position to the mouse position. 对于我在这里提出的确切问题(如何在特定的&lt;div&gt;中使图片跟随鼠标),我有这个想法:</p><p> 获取我的 div <em>top</em> 、 <em>left</em> 、 <em>width</em>和<em>height</em>并做一些数学运算,如果鼠标 go 离开div ,设置图片的visibility:hidden 。</p><p> 但是没有任何简单的方法可以做到这一点吗?</p></div> - Picture follow a mouse in a <div> 如何使div仅在跨度时才跟随鼠标指针 - How to make a div follow the mouse's pointer only when over a span 仅在悬停框时如何使 div 跟随鼠标指针? - How to make a div follow the mouse's pointer only when hovering a box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM