效果展示
即为蓝色猫猫虫,如果没有看到,可以右键,选择跟随我按钮
代码展示
配置
STATIC_URL - 追到鼠标时显示的(支持GIF)
WALKING_GIF - 没追到鼠标时显示的(支持GIF)
easing - 从0.01到0.1,越大速度越快,太快的话会卡顿
开关该功能的函数:
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| (function() { if (window.innerWidth < 768) return;
const STATIC_URL = '/img/sleep.jpg'; const WALKING_GIF = '/img/run.jpg'; const easing = 0.05; let isCatActive = localStorage.getItem('cat-active') !== 'false'; let targetX = window.innerWidth / 2, targetY = window.innerHeight / 2; let currentX = window.innerWidth / 2, currentY = window.innerHeight / 2; let isMoving = false; let animationId = null;
const catContainer = document.createElement('div'); catContainer.id = 'hexo-cat-container'; catContainer.style.cssText = `position:fixed; z-index:9998; pointer-events:none; user-select:none; display:${isCatActive ? 'block' : 'none'};`; catContainer.innerHTML = `<img id="hexo-cat-img" src="${STATIC_URL}" style="width:60px; height:auto; transition:transform 0.2s; will-change:transform;">`; document.body.appendChild(catContainer); const img = document.getElementById('hexo-cat-img');
window.toggleCat = function() { isCatActive = !isCatActive; localStorage.setItem('cat-active', isCatActive); if (isCatActive) { catContainer.style.display = 'block'; currentX = targetX; currentY = targetY; console.log("小猫已召唤 🐱"); } else { catContainer.style.display = 'none'; console.log("小猫已躲起来了 💤"); } };
window.addEventListener('mousemove', (e) => { if (!isCatActive) return; targetX = e.clientX; targetY = e.clientY; });
function animate() { if (isCatActive) { const dx = targetX - currentX; const dy = targetY - currentY; const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 1.5) { if (!isMoving) { img.src = WALKING_GIF; isMoving = true; } if (Math.abs(dx) > 0.5) img.style.transform = dx > 0 ? 'scaleX(-1)' : 'scaleX(1)'; } else { if (isMoving) { img.src = STATIC_URL; isMoving = false; } }
currentX += dx * easing; currentY += dy * easing; catContainer.style.left = `${currentX - 30}px`; catContainer.style.top = `${currentY - 30}px`; } requestAnimationFrame(animate); }
animate(); })();
|
启用
在_config.butterfly.yml的inject的head加入
1
| - <script src="/js/catrun.js"></script>
|
Hexo三连