css+js实现液态水珠跟随鼠标移动交互动画效果代码

代码语言:html

所属分类:动画

代码描述:css+js实现液态水珠跟随鼠标移动交互动画效果代码

代码标签: css js 液态 水珠 跟随 鼠标 移动 交互 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        :root {
      --bg: #d9d9d9;
      --size: 160px;
      --transition-time: 0.15s;
    }
    body {
      margin: 0;
      width: 100dvw;
      height: 100dvh;
      background: var(--bg);
      display: grid;
      place-content: center;
      place-items: center;
    }
    
    .water-drop {
        margin-top: 40px;
      width: var(--size);
      aspect-ratio: 1;
      
      border-radius: var(--x0, 52%) var(--x1, 48%) var(--x3, 61%) var(--x2, 39%) / var(--y0, 44%) var(--y1, 50%) var(--y3, 50%) var(--y2, 56%);
      transition: border-radius var(--transition-time);
      box-shadow: 0.25rem 0.5rem 0.75rem #42424233, 0.25rem 0.5rem 1rem #42424233 inset;
      position: relative;
      & .light-1 {
        width: 3.5rem;
        height: 1rem;
        background: #fafafa99;
        border-radius: 50%;
        transform: rotate(var(--rotate, -20deg));
        transition: all var(--transition-time);
        position: absolute;
        left: 20%;
        top: var(--top, 10%);
      }
      & .light-2 {
        width: 0.5rem;
        height: 0.5rem;
        background: #fafafacc;
        border-radius: 50%;
        position: absolute;
        left: var(--left, 10%);
        top: var(--top, 33%);
        transition: all var(--transition-time);
      }
    }
    </style>

</head>

<body>

    <h3>将鼠标移入水滴内部或在边缘触摸以移动它</h3>
    <div class="water-drop">
        <div class="light-1"></div>
        <div class="light-2"></div>
    </div>

    <script>
        const waterDrop = document.querySelector(".water-drop");
    const light1 = document.querySelector(".light-1");
    const light2 = document.querySelector(".light-2");
    
    const waterDropWidth = waterDrop.clientWidth;
    
    waterDrop.addEventListener("mousemove", ({ layerX: x, layerY: y }) => moveWaterDrop(x, y));
    
    waterDrop.addEventListener("touchmove", ({ layerX: x, layerY: y }) => moveWaterDrop(x, y));
    
    function moveWaterDrop(x, y) {
      
      let initialXPercentage = (((x * 100) / waterDropWidth) < 20) ? 20 : (((x * 100) / waterDropWidth) > 80) ? 80 : ((x * 100) / waterDr.........完整代码请登录后点击上方下载按钮下载查看

网友评论0