JSFiddle - React, Tailwind, and code Playground

by HDL52

HTML

<div class="face">
  <div class="eyes">
    <div class="eye"></div>
    <div class="eye"></div>
  </div>
</div>

CSS

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

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.face {
  position: relative;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: #ffcd00;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 10px 10px 100px rgba(0, 0, 0, 0.2);

  /* &:hover {
    &::before {
      top: 210px;
      width: 150px;
      height: 20px;
      background: #b57700;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
      transition: 0.5s;
    }
  } */

  &::before {
    content: "";
    position: absolute;
    top: 180px;
    width: 150px;
    height: 70px;
    background: #b57700;
    border-bottom-left-radius: 70px;
    border-bottom-right-radius: 70px;
    transition: 0.5s;
  }

  .eyes {
    position: relative;
    top: -40px;
    display: flex;

    .eye {
      position: relative;
      width: 80px;
      height: 80px;
      background: #fff;
      margin: 0 15px;
      border-radius: 50%;

      &::before {
        content: "";
        position: absolute;
        top: 50%;
        left: 25px;
        transform: translate(-50%, -50%);
        width: 40px;
        height: 40px;
        background: #333;
        border-radius: 50%;
      }
    }
  }
}

JavaScript

// https://juejin.cn/post/7143899396646797342

// 获取相关元素
const eyes = document.querySelectorAll(".eye")
const body = document.querySelector("body")

// 给body添加鼠标移动事件
body.addEventListener("mousemove", (event) => {
  eyes.forEach((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 rot = radian * (180 / Math.PI) * -1 + 270
    eye.style.transform = `rotate(${rot}deg)`
  })
})