Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
.eye { --pupil-x: 0; --pupil-y: 0; --color-whites: var(--fg, #fff); --color-lid: var(--bg, #fff); --color-pupil: var(--bg, #000); --color-glint: var(--fg, #fff); --scale: 0.75; max-width: 100%; max-height: 100vh; fill: none; transform: scale(var(--scale)); } .lids { stroke: var(--color-lid); stroke-width: 5%; } .whites { fill: var(--color-whites); } .pupil { fill: var(--color-pupil); cx: 500px; cy: 500px; } .glint { fill: var(--color-glint); } .pupil-group { transform: translate(calc(var(--pupil-x) * 1px), calc(var(--pupil-y) * 1px)); } #base-eye { display: none; } #grid { --num-columns: 5; --num-rows: 5; width: 100%; height: 100vh; display: grid; grid-template-columns: repeat(var(--num-columns), 1fr); grid-template-rows: repeat(var(--num-rows), 1fr); } body { --bg: #111; --fg: #fff; min-height: 100vh; background: var(--bg); overflow: hidden; }
JavaScript
const baseObject = document.getElementById("base-eye"); const grid = document.getElementById("grid"); let eyes, eyeCenters; let eyeDensity = 5; let numEyesX, numEyesY; const center = new THREE.Vector2(); const mousePos = new THREE.Vector2(); const PI = Math.PI; let maxDist; const maxEyeTravelX = 275; const maxEyeTravelY = 100; init(); function init() { window.addEventListener("resize", throttled(handleResize)); window.addEventListener("mousemove", throttled(handleMouseMove)); handleResize(); console.log("init"); } function handleMouseMove(event) { mousePos.set(event.clientX, event.clientY); eyes.forEach((eye, i) => { const vecToMouse = new THREE.Vector2().subVectors(mousePos, eyeCenters[i]); const angle = vecToMouse.angle(); const dist = mousePos.distanceTo(eyeCenters[i]); const distPercent = map(dist, 0, maxDist, 0, 1); const clampedMouseX = clamp(vecToMouse.x, maxEyeTravelX * -1, maxEyeTravelX); const clampedMouseY = clamp(vecToMouse.y, maxEyeTravelY * -1, maxEyeTravelY); const pupilX = map(clampedMouseX, 0, maxEyeTravelX, 0, maxEyeTravelX); const pupilY = map(clampedMouseY, 0, maxEyeTravelY, 0, maxEyeTravelY); const scale = map(dist, 0, maxDist, 0.5, 1.25); eye.style.setProperty("--pupil-x", pupilX); eye.style.setProperty("--pupil-y", pupilY); eye.style.setProperty("--scale", scale); }); } function handleResize() { // recreate the grid and elements const largeSide = Math.max(innerWidth, innerHeight); const size = Math.round(largeSide / eyeDensity); numEyesX = Math.ceil(innerWidth / size); numEyesY = Math.ceil(innerHeight / size); grid.style.setProperty("--num-columns", numEyesX); grid.style.setProperty("--num-rows", numEyesY); grid.innerHTML = ""; generateArrowGrid(); center.set(innerWidth * 0.5, innerHeight * 0.5); maxDist = center.length() * 2; // send a fake mouse event to trigger the initial point handleMouseMove({clientX: center.x, clientY: center.y}); } function generateArrowGrid() { eyes = []; eyeCenters = []; for (let i = 0; i < numEyesX * numEyesY; i += 1) { // add the eye to the grid const newArrow = baseObject.cloneNode(true); newArrow.id = `eye-${i}`; newArrow.setAttribute("class", "eye"); grid.appendChild(newArrow); eyes.push(newArrow); // save its center point for use later const eyeRect = newArrow.getBoundingClientRect(); const eyeCenter = new THREE.Vector2( eyeRect.left + (newArrow.clientWidth * 0.5), eyeRect.top + (newArrow.clientHeight * 0.5), ); eyeCenters.push(eyeCenter); } } // USEFUL FUNCTIONS function throttled(fn) { let didRequest = false; return param => { if (!didRequest) { requestAnimationFrame(() => { fn(param); didRequest = false; }); didRequest = true; } }; } function map(value, min1, max1, min2, max2) { return (value - min1) * (max2 - min2) / (max1 - min1) + min2; } function clamp (value, min = 0, max = 1) { return value <= min ? min : value >= max ? max : value; }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>眼睛跟随光标-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号