文本/链接

js+CSS3自定义鼠标特效

阿里云


这是一款 js 和 CSS3 自定义鼠标特效,该特效中,使用一个 DIV 元素来自定义鼠标,通过 CSS 代码来构建鼠标的形状,并通过 js 代码来驱动鼠标的动画。

HTML 结构

基本的 HTML 结构如下:

也想出现在这里?联系我们
创客主机
  1. <!--鼠标形状元素-->
  2. <div id="cursorBlob"></div>
  3. <!--页面内容-->
  4. <div class="wrap">
  5.  
  6. </div>

CSS 样式

为鼠标元素添加一些基础的 CSS 样式。

  1. #cursorBlob {
  2.   width: 50px;
  3.   height: 50px;
  4.   background: linear-gradient(120deg, #FF1744, #E040FB, #2979FF, #00E5FF, #76FF03);
  5.   background-size: 1600% 1600%;
  6.   position: absolute;
  7.   mix-blend-mode: difference;
  8.   pointer-events: none;
  9.   z-index: 1;
  10.   transition: 0.15s linear;
  11.   animation: blobRadius 5s ease infinite, blobBackground 15s ease infinite;
  12. }
  13.  
  14. @keyframes blobRadius {
  15.   0%, 100% {
  16.     border-radius: 43% 77% 80% 40% / 40% 40% 80% 80%;
  17.   }
  18.   20% {
  19.     border-radius: 47% 73% 61% 59% / 47% 75% 45% 73%;
  20.   }
  21.   40% {
  22.     border-radius: 46% 74% 74% 46% / 74% 58% 62% 46%;
  23.   }
  24.   60% {
  25.     border-radius: 47% 73% 61% 59% / 40% 40% 80% 80%;
  26.   }
  27.   80% {
  28.     border-radius: 50% 70% 52% 68% / 51% 61% 59% 69%;
  29.   }
  30. }
  31. @keyframes blobBackground {
  32.   0%, 100% {
  33.     background-position: 0% 50%;
  34.   }
  35.   50% {
  36.     background-position: 100% 50%;
  37.   }
  38. }

Javascript

最后通过下面的 JS 代码来在鼠标移动时,将制定的鼠标形状元素跟随鼠标一起移动。

  1. const blobCursor = (() => {
  2. const CURSOR = document.querySelector('#cursorBlob');
  3. const LINKS = document.querySelectorAll('.nav__link');
  4. const setCursorPos = e => {
  5. const { pageX: posX, pageY: posY } = e;
  6. CURSOR.style.top = `${posY - CURSOR.offsetHeight / 2}px`;
  7. CURSOR.style.left = `${posX - CURSOR.offsetWidth / 2}px`;
  8. };
  9. document.addEventListener('mousemove', setCursorPos);
  10.  
  11. const setCursorHover = () => CURSOR.style.transform = 'scale(2.5)';
  12. const removeCursorHover = () => CURSOR.style.transform = '';
  13. LINKS.forEach(link => link.addEventListener('mouseover', setCursorHover));
  14. LINKS.forEach(link => link.addEventListener('mouseleave', removeCursorHover));
  15.  
  16. })();

Codepen 网址:https://codepen.io/hiMRK/pen/ErMoKO

js+CSS3 自定义鼠标特效

已有 481 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!