我有这个标志,它有两只眼睛在里面.我想得到动画效果,它会跟踪光标的位置,并一直看着它.

我需要这个:

enter image description here

我已经创建了这个解决方案,但仍然有一些眼睛宽度的问题,一旦我把它变得更宽,它就会离开边界.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style media="screen">
  @import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");

* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: "Bebas Neue", cursive;
background: #0c3b5a;  
 }

.container {
    display: flex;
    background: white;
    border-radius: 48px;
    width: auto;
    padding: 1em 2em;
}

.container .eyes {
position: relative;
width: 80px;
height: 80px;
display: block;
background-color: #fff;
margin: 0 -10px;
border-radius: 50%;
border: 2px solid black;
}
.eyes:first-child{
  z-index:9
 }
.container .eyes::before {
content: "";
top: 50%;
left: 35px;
transform: translate(-50%, -50%);
width: 70px;
height: 70px;
border-radius: 50%;
background: #000;
position: absolute;
box-sizing: border-box;
}

  </style>
</head>
<body>

      <div class="container">
        <div class="eyes"></div>
        <div class="eyes"></div>
      </div>

    <script type="text/javascript">
    document.querySelector("body").addEventListener("mousemove", eyeball);

    function eyeball() {
      const eye = document.querySelectorAll(".eyes");
      eye.forEach(function (eye) {
        let x = eye.getBoundingClientRect().left + eye.clientWidth / 2;
        let y = eye.getBoundingClientRect().top + eye.clientHeight / 2;

        let radian = Math.atan2(event.pageX - x, event.pageY - y);
        let rotate = radian * (180 / Math.PI) * -1 + 270;
        eye.style.transform = "rotate(" + rotate + "deg)";
      });
    }

    </script>
</body>
</html>

推荐答案

1-不要把containereye放在一起.实际上,你有一些不必要的款式.

2-在eye的基础上加overflow: auto;.

3-你最好区分eye border coloreyeballs,因为如果是一样的,眼球看起来有点大.由你决定.

以下是代码.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style media="screen">
  @import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");

* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: "Bebas Neue", cursive;
background: #0c3b5a;  
 }

.container {
    display: flex;
    background: white;
    border-radius: 48px;
    width: auto;
    padding: 1em 2em;
}

.eyes {
position: relative;
width: 100px;
height: 100px;
display: block;
background-color: #fff;
margin: 0 -10px;
border-radius: 50%;
border: 1px solid orange;
overflow: auto;
}
.eyes:first-child{
  z-index:9
 }
.container .eyes::before {
content: "";
top: 50%;
left: 35px;
transform: translate(-50%, -50%);
width: 80px;
height: 80px;
border-radius: 50%;
background: #000;
position: absolute;
box-sizing: border-box;
}

  </style>
</head>
<body>

      <div class="container">
        <div class="eyes"></div>
        <div class="eyes"></div>
      </div>

    <script type="text/javascript">
    document.querySelector("body").addEventListener("mousemove", eyeball);

    function eyeball() {
      const eye = document.querySelectorAll(".eyes");
      eye.forEach(function (eye) {
        let x = eye.getBoundingClientRect().left + eye.clientWidth / 2;
        let y = eye.getBoundingClientRect().top + eye.clientHeight / 2;

        let radian = Math.atan2(event.pageX - x, event.pageY - y);
        let rotate = radian * (180 / Math.PI) * -1 + 270;
        eye.style.transform = "rotate(" + rotate + "deg)";
      });
    }

    </script>
</body>
</html>

Javascript相关问答推荐

如何将剧作家请求对象转换为字符串,因为它只提供promise ?

如何在我的Web应用程序中使用JavaScript显示和隐藏喜欢/不喜欢按钮?

导入图像与内联包含它们NextJS

d3可排序表标题行中有收件箱--如何使收件箱不触发排序?

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

用相器进行向内碰撞检测

为什么从liveWire info js代码传递数组我出现错误?

通过使用100%间隔时间来代表我们还剩多少时间来倒计时

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

如何在输入元素中附加一个属性为checkbox?

Chart.js-显示值应该在其中的引用区域

如何修复我的数据表,以使stateSave正常工作?

JQuery Click事件不适用于动态创建的按钮

如何 for each 输入动态设置输入变更值

一个实体一刀VS每个实体多刀S

打字脚本中方括号符号属性访问和拾取实用程序的区别

ngOnChanges仅在第二次调用时才触发

如何根据输入数量正确显示alert ?

为什么NULL不能在构造函数的.Prototype中工作