Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body{ background-color: #CB9805; margin: 0; overflow: hidden; } canvas{ touch-action: none; }
JavaScript
// Define an object that will be used to draw plus signs var Plus = function() { this.x = 0; this.y = 0; this.top = 0; this.left = 0; this.height = 0; this.width = 0; this.scale = 1; }; //Add draw method to the object Plus.prototype.draw = function(ctx, x, y) { ctx.save(); ctx.beginPath(); ctx.setTransform( this.scale, 0, 0, this.scale, this.left + this.x, this.top + this.y ); ctx.lineWidth = 2; ctx.moveTo(0, -this.height / 2); ctx.lineTo(0, this.height / 2); ctx.moveTo(-this.width / 2, 0); ctx.lineTo(this.width / 2, 0); ctx.stroke(); ctx.closePath(); ctx.restore(); }; var c = document.getElementById("c"); var context = c.getContext("2d"); var signs = []; var mouse = { x: 0, y: 0 }; var gridLength = 9; var mouseOver = false; var mouseMoved = false; c.width = window.innerWidth; c.height = window.innerHeight; // Create sign grid using 2D array for (var i = 0; i < gridLength; i++) { signs[i] = []; for (var j = 0; j < gridLength; j++) { var min = Math.min(c.width, c.height); signs[i][j] = new Plus(); signs[i][j].left = c.width / (gridLength + 1) * (i + 1); signs[i][j].top = c.height / (gridLength + 1) * (j + 1); signs[i][j].width = min / 50; signs[i][j].height = min / 50; } } // Use GSAP ticker to call draw function on every frame that will draw signs to the canvas // You can use requestAnimationFrame as well TweenLite.ticker.addEventListener("tick", draw); function draw() { context.clearRect(0, 0, c.width, c.height); if (mouseOver && mouseMoved) { calculateSigns(); mouseMoved = false; } for (var i = 0; i < gridLength; i++) { for (var j = 0; j < gridLength; j++) { var sign = signs[i][j]; sign.draw(context); } } } function calculateSigns() { for (var i = 0; i < gridLength; i++) { for (var j = 0; j < gridLength; j++) { var sign = signs[i][j]; var hyp = Math.min(c.width, c.height) / (gridLength + 1) / 2; var d = dist([sign.left, sign.top], [mouse.x, mouse.y]); var ax = mouse.x - sign.left; var ay = mouse.y - sign.top; var angle = Math.atan2(ay, ax); if (d < hyp + sign.width) { hyp = d; TweenMax.to(sign, 0.3, { scale: 2 }); } else { TweenMax.to(sign, 0.3, { scale: 1 }); } TweenMax.to(sign, 0.3, { x: Math.cos(angle) * hyp, y: Math.sin(angle) * hyp }); } } } c.addEventListener("mousemove", mouseMove); c.addEventListener("touchmove", mouseMove); function mouseMove(e) { if (e.targetTouches && e.targetTouches[0]) { e = e.targetTouches[0]; } var rect = c.getBoundingClientRect(); mouse.x = e.clientX - rect.left; mouse.y = e.clientY - rect.top; mouseMoved = true; } c.addEventListener("mouseenter", function() { mouseOver = true; }); c.addEventListener("touchstart", function(e) { mouseOver = true; mouseMove(e); }); c.addEventListener("mouseleave", mouseLeave); c.addEventListener("touchend", mouseLeave); function mouseLeave() { mouseOver = false; for (var i = 0; i < gridLength; i++) { for (var j = 0; j < gridLength; j++) { var sign = signs[i][j]; if (!mouseOver) { TweenMax.to(sign, 0.3, { x: 0, y: 0, scale: 1 }); } } } } window.addEventListener("resize", function() { c.width = window.innerWidth; c.height = window.innerHeight; for (var i = 0; i < gridLength; i++) { for (var j = 0; j < gridLength; j++) { var min = Math.min(c.width, c.height); sign = signs[i][j]; sign.left = c.width / (gridLength + 1) * (i + 1); sign.top = c.height / (gridLength + 1) * (j + 1); sign.width = min / 50; sign.height = min / 50; } } }); function dist([x1, y1], [x2, y2]) { var dx = x1 - x2; var dy = y1 - y2; return Math.sqrt(dx * dx + dy * dy) || 1; }
粒子
时间
文字
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号