gsap+jquery实现鼠标点击模拟压力网格动画效果代码

代码语言:html

所属分类:动画

代码描述:gsap+jquery实现鼠标点击模拟压力网格动画效果代码

代码标签: gsap jquery 鼠标 点击 模拟 压力 网格 动画

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

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

<head>
  <meta charset="UTF-8">

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

body {
  cursor: none;
  background-color: #000;
}

.cursor, .ripple {
  position: fixed;
  width: 14px;
  aspect-ratio: 1;
  background: white;
  border-radius: 50%;
  pointer-events: none;
  z-index: 9999;
  transform: translate(-50%, -50%);
}

main {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
main #grid {
  width: 200px;
  height: 200px;
  background: radial-gradient(circle at top left, #c124a4, transparent 100%), radial-gradient(circle at top right, #8c00ff, transparent 100%), radial-gradient(circle at bottom left, #670ba9, transparent 100%), radial-gradient(circle at bottom right, #541ed1, transparent 100%);
  border-radius: 30px;
  overflow: hidden;
  position: relative;
  padding: 10px;
  display: grid;
  place-items: center;
}
main #grid .cell {
  aspect-ratio: 1;
  width: 5px;
  background: white;
  opacity: 0.2;
  border-radius: 9999px;
}
main #grid .center {
  background: transparent;
  border: 2px solid white;
  width: 7px;
}
main #grid .selected {
  opacity: 0.8 !important;
  background: white;
  box-shadow: 0 0 5px 1px white;
  width: 10px;
  transition: width 0.2s ease-in-out;
}
</style>

  
  
</head>

<body translate="no">
  <main>
    <div id="grid"></div>
</main>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.10.4.js"></script>
      <script >
"use strict";
// Constants
const GRID_SIZE = 11;
const CELL_EFFECT_RADIUS = 4;
const MAX_SCALE = 4;
// State
let isMouseDown = false;
// Cursor module
const Cursor = {
    setup() {
        $("<div>").addClass("cursor").appendTo("body");
        $(document).on("mousemove", this.updatePosition);
        $(document).on("mousedown", this.createRippleEffect);
        $(document).on("mouseup", () => this.scale(1));
        $(document).on("dragstart", (e) => e.preventDefault());
    },
    updatePosition(e) {
        $(".cursor, .ripple").css({
            left: `${e.clientX}px`,
            top: `${e.clientY}px`
        });
    },
    createRippleEffect(e) {
        Cursor.scale(1.5);
        const $ripple = $("<div>").addClass("ripple").appendTo("body");
        $ripple.css({
            left: `${e.clientX}px`,
            top: `${e.clientY}px`
        });
        gsap.to($ripple, {
            scale: 2.5,
            opacity: 0,
            duration: 0.75,
            onComplete: () => {
                $ripple.remove();
            }
        });
    },
    scale(scale) {
        gsap.to(".cursor", { scale, duration: 0.2 });
    }
};
// Grid module
const Grid = {
    create(size) {
        const $grid = $("#grid");
        const center = Math.floor(size / 2);
        $grid.css({
            "grid-template-columns": `repeat(${size}, 1fr)`,
            "grid-template-rows": `repeat(${size}, 1fr)`
        });
        for (let i = 0; i < size; i++) {
            for (let j = 0; j < size; j++) {
                const $cell = $("<div>")
                    .addClass("cell")
                    .data("pos", { x: j, y: i });
                if (i === center && j === center) {
                    $cell.addClass(["center", "selected"]);
                }
                else if (i === center || j === center) {
                    $cell.css("opacity", "0.6");
                }
                $grid.append($cell);
            }
        }
   .........完整代码请登录后点击上方下载按钮下载查看

网友评论0