Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
html, body { height: 100%; } body { display: flex; align-items: center; justify-content: center; background: #1e2779; margin: 0; } .peas { width: 200px; } .pea-1 .body, .pea-1 .cheek { fill:#6BC566; } .pea-2 .body, .pea-2 .cheek { fill:#B9EA77; } .pea-3 .body, .pea-3 .cheek { fill:#89D770; } .cheek { transition: transform 0.3s; } .pea:hover .cheek { transform: translateY(-1.5px); } .mouth, .pupil {fill:#002B3E;} .tongue {fill:#CE4C59;} .eye {fill:#FFFFFF;} .base-bottom{fill:#007E61;} .base-top{fill:#00905F;} .pea-1 .pupil {transition: transform 0.4s linear;} .pea-2 .pupil {transition: transform 0.15s linear;} .pea-3 .pupil {transition: transform 0.7s linear;} .test { width: 4px; height: 4px; background:red; border-radius: 100%; position: absolute; top: 0; left: 0; transform: translate(-50%, -50%); }
JavaScript
'use strict'; const svg = document.querySelector('.peas'); let mouseX = 0; let mouseY = 0; let pupilPadding = 1.5; // ------------ // Mouse events // ------------ window.addEventListener('mousemove', mouseMove); function mouseMove(e) { mouseX = e.clientX; mouseY = e.clientY; } // --- // Pea // --- class Pea { constructor(className) { this.pea = svg.querySelector(className); this.eyeLeft = { element: this.pea.querySelector('.eye-left'), get radius() { return parseInt(this.element.getAttribute('rx')) }, get xMin() { return parseInt(this.element.getAttribute('cx')) - this.radius + pupilPadding }, get xMax() { return parseInt(this.element.getAttribute('cx')) + this.radius - pupilPadding }, get yMin() { return parseInt(this.element.getAttribute('cy')) - this.radius + pupilPadding }, get yMax() { return parseInt(this.element.getAttribute('cy')) + this.radius - pupilPadding }, get center() { return getItemCenter(this.element.getAttribute('cx'), this.element.getAttribute('cy')) } }; this.eyeRight = { element: this.pea.querySelector('.eye-right'), get radius() { return parseInt(this.element.getAttribute('rx')) }, get xMin() { return parseInt(this.element.getAttribute('cx')) - this.radius + pupilPadding }, get xMax() { return parseInt(this.element.getAttribute('cx')) + this.radius - pupilPadding }, get yMin() { return parseInt(this.element.getAttribute('cy')) - this.radius + pupilPadding }, get yMax() { return parseInt(this.element.getAttribute('cy')) + this.radius - pupilPadding }, get center() { return getItemCenter(this.element.getAttribute('cx'), this.element.getAttribute('cy')) } }; this.pupilLeft = { element: this.pea.querySelector('.pupil-left'), get radius() { return parseInt(this.element.getAttribute('rx')) }, get center() { return getItemCenter(this.element.getAttribute('cx'), this.element.getAttribute('cy')) } } this.pupilRight = { element: this.pea.querySelector('.pupil-right'), get radius() { return parseInt(this.element.getAttribute('rx')) }, get center() { return getItemCenter(this.element.getAttribute('cx'), this.element.getAttribute('cy')) } } this.pupilLeft.element.setAttribute('cx', this.eyeLeft.xMin + (this.pupilLeft.radius * 2)); this.pupilLeft.element.setAttribute('cy', this.eyeLeft.yMin + (this.pupilLeft.radius * 2)); this.pupilRight.element.setAttribute('cx', this.eyeRight.xMin + (this.pupilRight.radius * 2)); this.pupilRight.element.setAttribute('cy', this.eyeRight.yMin + (this.pupilRight.radius * 2)); this.pea.addEventListener('mouseenter', function() { this.classList.add('pea-hovered'); }); this.pea.addEventListener('mouseleave', function() { this.classList.remove('pea-hovered'); }); } update() { let amountY = (this.eyeLeft.yMax - this.eyeLeft.yMin - (this.pupilLeft.radius * 3)) * (mouseY/document.body.clientHeight); let amountX = (this.eyeLeft.xMax - this.eyeLeft.xMin - (this.pupilLeft.radius * 3)) * (mouseX/document.body.clientWidth); this.pupilLeft.element.style.transform = `translate(${amountX}px, ${amountY}px)`; this.pupilRight.element.style.transform = `translate(${amountX}px, ${amountY}px)`; } } const peas = [ new Pea('.pea-1'), new Pea('.pea-2'), new Pea('.pea-3') ]; // -------------- // Animation loop // -------------- function loop() { for (let pea of peas) { pea.update(); } requestAnimationFrame(loop); } loop(); // ---------------- // Helper functions // ---------------- function getItemCenter(x, y) { return { x: document.body.clientWidth/2 - svg.clientWidth/2 + (x * window.devicePixelRatio), y: document.body.clientHeight/2 - svg.clientHeight/2 + (y * window.devicePixelRatio) }; }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>可爱的svg豆子动画-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号