如何用JavaScript实现十种令人惊叹的网页动态效果?
- 内容介绍
- 文章标签
- 相关推荐
本文共计3394个文字,预计阅读时间需要14分钟。
目录
1.鼠标点击弹出爱心
2.鼠标点击显示文字
3.鼠标点击显示烟花波纹
4.鼠标跟随星星
5.鼠标跟随雪花
6.鼠标跟随笑脸+仙女+泡泡+雪花+点击烟花效果(自由组合)
目录
- 1、鼠标点击弹出爱心
- 2、鼠标点击弹出文字
- 3、鼠标点击弹出烟花波纹
- 4、鼠标小星星拖尾跟随
- 5、鼠标粒子随心拖尾跟随
- 6、鼠标笑脸跟随+仙女棒+泡泡+雪花+点击烟花效果(自由组合)
- 7、樱花特效
- 8、蜘蛛网特效
- 9、看板娘(左下角的小人)
- 10、烟花
1、鼠标点击弹出爱心
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!-- 网页鼠标点击特效(爱心) --> <script type="text/javascript"> ! function (e, t, a) { function r() { for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[ e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e] .scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999"); requestAnimationFrame(r) } function n() { var t = "function" == typeof e.onclick && e.onclick; e.onclick = function (e) { t && t(), o(e) } } function o(e) { var a = t.createElement("div"); a.className = "heart", s.push({ el: a, x: e.clientX - 5, y: e.clientY - 5, scale: 1, alpha: 1, color: c() }), t.body.appendChild(a) } function i(e) { var a = t.createElement("style"); a.type = "text/css"; try { a.appendChild(t.createTextNode(e)) } catch (t) { a.styleSheet.cssText = e } t.getElementsByTagName("head")[0].appendChild(a) } function c() { return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math .random()) + ")" } var s = []; e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e .mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) { setTimeout(e, 1e3 / 60) }, i( ".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}" ), n(), r() }(window, document); </script> </body> </html>
2、鼠标点击弹出文字
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> (function () { var a_idx = 0; window.onclick = function (event) { var a = new Array("❤富强❤", "❤民主❤", "❤文明❤", "❤和谐❤", "❤自由❤", "❤平等❤", "❤公正❤", "❤法治❤", "❤爱国❤", "❤敬业❤", "❤诚信❤", "❤友善❤"); var heart = document.createElement("b"); //创建b元素 heart.onselectstart = new Function('event.returnValue=false'); //防止拖动 document.body.appendChild(heart).innerHTML = a[a_idx]; //将b元素添加到页面上 a_idx = (a_idx + 1) % a.length; heart.style.cssText = "position: fixed;left:-100%;"; //给p元素设置样式 var f = 16, // 字体大小 x = event.clientX - f / 2, // 横坐标 y = event.clientY - f, // 纵坐标 c = randomColor(), // 随机颜色 a = 1, // 透明度 s = 1.2; // 放大缩小 var timer = setInterval(function () { //添加定时器 if (a <= 0) { document.body.removeChild(heart); clearInterval(timer); } else { heart.style.cssText = "font-size:16px;cursor: default;position: fixed;color:" + c + ";left:" + x + "px;top:" + y + "px;opacity:" + a + ";transform:scale(" + s + ");"; y--; a -= 0.016; s += 0.002; } }, 15) } // 随机颜色 function randomColor() { return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math .random() * 255)) + ")"; } }()); </script> </body> </html>
3、鼠标点击弹出烟花波纹
代码
<html> <head> </head> <body> <script> function clickEffect() { let balls = []; let longPressed = false; let longPress; let multiplier = 0; let width, height; let origin; let normal; let ctx; const colours = ["#F73859", "#14FFEC", "#00E0FF", "#FF99FE", "#FAF15D"]; const canvas = document.createElement("canvas"); document.body.appendChild(canvas); canvas.setAttribute("style", "width: 100%; height: 100%; top: 0; left: 0; z-index: 99999; position: fixed; pointer-events: none;"); const pointer = document.createElement("span"); pointer.classList.add("pointer"); document.body.appendChild(pointer); if (canvas.getContext && window.addEventListener) { ctx = canvas.getContext("2d"); updateSize(); window.addEventListener('resize', updateSize, false); loop(); window.addEventListener("mousedown", function(e) { pushBalls(randBetween(10, 20), e.clientX, e.clientY); document.body.classList.add("is-pressed"); longPress = setTimeout(function(){ document.body.classList.add("is-longpress"); longPressed = true; }, 500); }, false); window.addEventListener("mouseup", function(e) { clearInterval(longPress); if (longPressed == true) { document.body.classList.remove("is-longpress"); pushBalls(randBetween(50 + Math.ceil(multiplier), 100 + Math.ceil(multiplier)), e.clientX, e.clientY); longPressed = false; } document.body.classList.remove("is-pressed"); }, false); window.addEventListener("mousemove", function(e) { let x = e.clientX; let y = e.clientY; pointer.style.top = y + "px"; pointer.style.left = x + "px"; }, false); } else { console.log("canvas or addEventListener is unsupported!"); } function updateSize() { canvas.width = window.innerWidth * 2; canvas.height = window.innerHeight * 2; canvas.style.width = window.innerWidth + 'px'; canvas.style.height = window.innerHeight + 'px'; ctx.scale(2, 2); width = (canvas.width = window.innerWidth); height = (canvas.height = window.innerHeight); origin = { x: width / 2, y: height / 2 }; normal = { x: width / 2, y: height / 2 }; } class Ball { constructor(x = origin.x, y = origin.y) { this.x = x; this.y = y; this.angle = Math.PI * 2 * Math.random(); if (longPressed == true) { this.multiplier = randBetween(14 + multiplier, 15 + multiplier); } else { this.multiplier = randBetween(6, 12); } this.vx = (this.multiplier + Math.random() * 0.5) * Math.cos(this.angle); this.vy = (this.multiplier + Math.random() * 0.5) * Math.sin(this.angle); this.r = randBetween(8, 12) + 3 * Math.random(); this.color = colours[Math.floor(Math.random() * colours.length)]; } update() { this.x += this.vx - normal.x; this.y += this.vy - normal.y; normal.x = -2 / window.innerWidth * Math.sin(this.angle); normal.y = -2 / window.innerHeight * Math.cos(this.angle); this.r -= 0.3; this.vx *= 0.9; this.vy *= 0.9; } } function pushBalls(count = 1, x = origin.x, y = origin.y) { for (let i = 0; i < count; i++) { balls.push(new Ball(x, y)); } } function randBetween(min, max) { return Math.floor(Math.random() * max) + min; } function loop() { ctx.fillStyle = "rgba(255, 255, 255, 0)"; ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < balls.length; i++) { let b = balls[i]; if (b.r < 0) continue; ctx.fillStyle = b.color; ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2, false); ctx.fill(); b.update(); } if (longPressed == true) { multiplier += 0.2; } else if (!longPressed && multiplier >= 0) { multiplier -= 0.4; } removeBall(); requestAnimationFrame(loop); } function removeBall() { for (let i = 0; i < balls.length; i++) { let b = balls[i]; if (b.x + b.r < 0 || b.x - b.r > width || b.y + b.r < 0 || b.y - b.r > height || b.r < 0) { balls.splice(i, 1); } } } } clickEffect();//调用特效函数 </script> </body> </html>
4、鼠标小星星拖尾跟随
代码
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <span class="js-cursor-container"></span> <script> (function fairyDustCursor() { var possibleColors = ["#D61C59", "#E7D84B", "#1B8798"] var width = window.innerWidth; var height = window.innerHeight; var cursor = { x: width / 2, y: width / 2 }; var particles = []; function init() { bindEvents(); loop(); } // Bind events that are needed function bindEvents() { document.addEventListener('mousemove', onMouseMove); window.addEventListener('resize', onWindowResize); } function onWindowResize(e) { width = window.innerWidth; height = window.innerHeight; } function onMouseMove(e) { cursor.x = e.clientX; cursor.y = e.clientY; addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]); } function addParticle(x, y, color) { var particle = new Particle(); particle.init(x, y, color); particles.push(particle); } function updateParticles() { // Updated for (var i = 0; i < particles.length; i++) { particles[i].update(); } // Remove dead particles for (var i = particles.length - 1; i >= 0; i--) { if (particles[i].lifeSpan < 0) { particles[i].die(); particles.splice(i, 1); } } } function loop() { requestAnimationFrame(loop); updateParticles(); } /** * Particles */ function Particle() { this.character = "*"; this.lifeSpan = 120; //ms this.initialStyles = { "position": "fixed", "display": "inline-block", "top": "0px", "left": "0px", "pointerEvents": "none", "touch-action": "none", "z-index": "10000000", "fontSize": "25px", "will-change": "transform" }; // Init, and set properties this.init = function (x, y, color) { this.velocity = { x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2), y: 1 }; this.position = { x: x + 10, y: y + 10 }; this.initialStyles.color = color; this.element = document.createElement('span'); this.element.innerHTML = this.character; applyProperties(this.element, this.initialStyles); this.update(); document.querySelector('.js-cursor-container').appendChild(this.element); }; this.update = function () { this.position.x += this.velocity.x; this.position.y += this.velocity.y; this.lifeSpan--; this.element.style.transform = "translate3d(" + this.position.x + "px," + this.position.y + "px, 0) scale(" + (this.lifeSpan / 120) + ")"; } this.die = function () { this.element.parentNode.removeChild(this.element); } } /** * Utils */ // Applies css `properties` to an element. function applyProperties(target, properties) { for (var key in properties) { target.style[key] = properties[key]; } } if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init(); })(); </script> </body> </html>
5、鼠标粒子随心拖尾跟随
代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>粒子随心动画</title> <script src="/uploads/allimg/230118/060T960G-5.jpg"></script> <style> body { overflow: hidden; margin: 0; } .twitter:hover a { transform: rotate(-45deg) scale(1.05); } .twitter:hover i { color: #21c2ff; } .twitter a { bottom: -40px; right: -75px; transform: rotate(-45deg); } .twitter i { bottom: 7px; right: 7px; color: #00aced; } .social-icon a { position: absolute; background: white; color: white; box-shadow: -1px -1px 20px 0px rgba(0, 0, 0, 0.3); display: inline-block; width: 150px; height: 80px; transform-origin: 50% 50%; transition: 0.15s ease-out; } .social-icon i { position: absolute; pointer-events: none; z-index: 1000; transition: 0.15s ease-out; } .youtube:hover a { transform: rotate(45deg) scale(1.05); } .youtube:hover i { color: #ec4c44; } .youtube a { bottom: -40px; left: -75px; transform: rotate(45deg); } .youtube i { bottom: 7px; left: 7px; color: #e62117; } </style> </head> <body> <canvas></canvas> <script> "use strict"; // Initial Setup var canvas = document.querySelector("canvas"); var c = canvas.getContext("2d"); canvas.width = innerWidth; canvas.height = innerHeight; // Variables var mouse = { x: innerWidth / 2, y: innerHeight / 2 - 80, }; var colors = ["#00bdff", "#4d39ce", "#088eff"]; // Event Listeners addEventListener("mousemove", function (event) { mouse.x = event.clientX; mouse.y = event.clientY; }); addEventListener("resize", function () { canvas.width = innerWidth; canvas.height = innerHeight; init(); }); // Utility Functions function randomIntFromRange(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } function randomColor(colors) { return colors[Math.floor(Math.random() * colors.length)]; } // Objects function Particle(x, y, radius, color) { var _this = this; var distance = randomIntFromRange(50, 120); this.x = x; this.y = y; this.radius = radius; this.color = color; this.radians = Math.random() * Math.PI * 2; this.velocity = 0.05; this.distanceFromCenter = { x: distance, y: distance, }; this.prevDistanceFromCenter = { x: distance, y: distance, }; this.lastMouse = { x: x, y: y }; this.update = function () { var lastPoint = { x: _this.x, y: _this.y }; // Move points over time _this.radians += _this.velocity; // Drag effect _this.lastMouse.x += (mouse.x - _this.lastMouse.x) * 0.05; _this.lastMouse.y += (mouse.y - _this.lastMouse.y) * 0.05; // Circular Motion _this.distanceFromCenter.x = _this.prevDistanceFromCenter.x + Math.sin(_this.radians) * 100; _this.distanceFromCenter.y = _this.prevDistanceFromCenter.x + Math.sin(_this.radians) * 100; _this.x = _this.lastMouse.x + Math.cos(_this.radians) * _this.distanceFromCenter.x; _this.y = _this.lastMouse.y + Math.sin(_this.radians) * _this.distanceFromCenter.y; _this.draw(lastPoint); }; this.draw = function (lastPoint) { c.beginPath(); c.strokeStyle = _this.color; c.lineWidth = _this.radius; c.moveTo(lastPoint.x, lastPoint.y); c.lineTo(_this.x, _this.y); c.stroke(); c.closePath(); }; } // Implementation var particles = undefined; function init() { particles = []; for (var i = 0; i < 50; i++) { var radius = Math.random() * 2 + 1; particles.push( new Particle( canvas.width / 2, canvas.height / 2, radius, randomColor(colors) ) ); } } // Animation Loop function animate() { requestAnimationFrame(animate); c.fillStyle = "rgba(255, 255, 255, 0.05)"; c.fillRect(0, 0, canvas.width, canvas.height); particles.forEach(function (particle) { particle.update(); }); } init(); animate(); </script> </body> </html>
6、鼠标笑脸跟随+仙女棒+泡泡+雪花+点击烟花效果(自由组合)
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!--光标特效仙女棒--> <script src="/uploads/allimg/230118/060T92D8-7.jpg"></script> <!--光标之泡泡--> <script src="/uploads/allimg/230118/060T95419-8.jpg"></script> <!--笑脸--> <script src="/uploads/allimg/230118/060T942N-9.jpg"></script> <!--雪花--> <script src="/uploads/allimg/230118/060T93555-10.jpg"></script> <!-- 点击后出现烟花效果 --> <script src="/uploads/allimg/230118/060T9A59-11.jpg"></script> </body> </html>
7、樱花特效
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3.org/1999/xhtml"> <head> <meta cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"> <link rel="stylesheet" href="cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css"> <script src="cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script> <script src="cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script> <script src="cdn.jsdelivr.net/npm/jquery.scrollto@2.1.2/jquery.scrollTo.min.js"></script> <script src="cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/autoload.js"></script>
10、烟花
代码
<!doctype html> <html> <head> <meta charset="utf-8"> <title>逼真的烟花</title> <script src="js/jquery.min.js"></script> <style> body { margin: 0; padding: 0; background: #000000; overflow: hidden; } </style> </head> <body> <canvas id="canvas"></canvas> <!-- best viewed in chrome --> <script> // fun options! const PARTICLES_PER_FIREWORK = 150; // 100 - 400 or try 1000 const FIREWORK_CHANCE = 0.02; // percentage, set to 0 and click instead const BASE_PARTICLE_SPEED = 0.6; // between 0-4, controls the size of the overall fireworks const FIREWORK_LIFESPAN = 600; // ms const PARTICLE_INITIAL_SPEED = 4.5; // 2-8 // not so fun options =\ const GRAVITY = 9.8; const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let particles = []; let disableAutoFireworks = false; let resetDisable = 0; let loop = () => { if (!disableAutoFireworks && Math.random() < FIREWORK_CHANCE) { createFirework(); } ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach((particle, i) => { particle.animate(); particle.render(); if (particle.y > canvas.height || particle.x < 0 || particle.x > canvas.width || particle.alpha <= 0 ) { particles.splice(i, 1); } }); requestAnimationFrame(loop); }; let createFirework = ( x = Math.random() * canvas.width, y = Math.random() * canvas.height ) => { let speed = (Math.random() * 2) + BASE_PARTICLE_SPEED; let maxSpeed = speed; let red = ~~(Math.random() * 255); let green = ~~(Math.random() * 255); let blue = ~~(Math.random() * 255); // use brighter colours red = (red < 150 ? red + 150 : red); green = (green < 150 ? green + 150 : green); blue = (blue < 150 ? blue + 150 : blue); // inner firework for (let i = 0; i < PARTICLES_PER_FIREWORK; i++) { let particle = new Particle(x, y, red, green, blue, speed); particles.push(particle); maxSpeed = (speed > maxSpeed ? speed : maxSpeed); } // outer edge particles to make the firework appear more full for (let i = 0; i < 40; i++) { let particle = new Particle(x, y, red, green, blue, maxSpeed, true); particles.push(particle); } }; class Particle { constructor( x = 0, y = 0, red = ~~(Math.random() * 255), green = ~~(Math.random() * 255), blue = ~~(Math.random() * 255), speed, isFixedSpeed ) { this.x = x; this.y = y; this.red = red; this.green = green; this.blue = blue; this.alpha = 0.05; this.radius = 1 + Math.random(); this.angle = Math.random() * 360; this.speed = (Math.random() * speed) + 0.1; this.velocityX = Math.cos(this.angle) * this.speed; this.velocityY = Math.sin(this.angle) * this.speed; this.startTime = (new Date()).getTime(); this.duration = Math.random() * 300 + FIREWORK_LIFESPAN; this.currentDiration = 0; this.dampening = 30; // slowing factor at the end this.colour = this.getColour(); if (isFixedSpeed) { this.speed = speed; this.velocityY = Math.sin(this.angle) * this.speed; this.velocityX = Math.cos(this.angle) * this.speed; } this.initialVelocityX = this.velocityX; this.initialVelocityY = this.velocityY; } animate() { this.currentDuration = (new Date()).getTime() - this.startTime; // initial speed kick if (this.currentDuration <= 200) { this.x += this.initialVelocityX * PARTICLE_INITIAL_SPEED; this.y += this.initialVelocityY * PARTICLE_INITIAL_SPEED; this.alpha += 0.01; this.colour = this.getColour(240, 240, 240, 0.9); } else { // normal expansion this.x += this.velocityX; this.y += this.velocityY; this.colour = this.getColour(this.red, this.green, this.blue, 0.4 + (Math.random() * 0.3)); } this.velocityY += GRAVITY / 1000; // slow down particles at the end if (this.currentDuration >= this.duration) { this.velocityX -= this.velocityX / this.dampening; this.velocityY -= this.velocityY / this.dampening; } if (this.currentDuration >= this.duration + this.duration / 1.1) { // fade out at the end this.alpha -= 0.02; this.colour = this.getColour(); } else { // fade in during expansion if (this.alpha < 1) { this.alpha += 0.03; } } } render() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); ctx.lineWidth = this.lineWidth; ctx.fillStyle = this.colour; ctx.shadowBlur = 8; ctx.shadowColor = this.getColour(this.red + 150, this.green + 150, this.blue + 150, 1); ctx.fill(); } getColour(red, green, blue, alpha) { return `rgba(${red || this.red}, ${green || this.green}, ${blue || this.blue}, ${alpha || this.alpha})`; } } let updateCanvasSize = () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }; // run it! updateCanvasSize(); $(window).resize(updateCanvasSize); $(canvas).on('click', (e) => { createFirework(e.clientX, e.clientY); // stop fireworks when clicked, re-enable after short time disableAutoFireworks = true; clearTimeout(resetDisable); resetDisable = setTimeout(() => { disableAutoFireworks = false; }, 5000); }); loop(); </script> </body> </html>
以上就是基于JS实现十种酷炫的网页特效的详细内容,更多关于JS网页特效的资料请关注自由互联其它相关文章!
本文共计3394个文字,预计阅读时间需要14分钟。
目录
1.鼠标点击弹出爱心
2.鼠标点击显示文字
3.鼠标点击显示烟花波纹
4.鼠标跟随星星
5.鼠标跟随雪花
6.鼠标跟随笑脸+仙女+泡泡+雪花+点击烟花效果(自由组合)
目录
- 1、鼠标点击弹出爱心
- 2、鼠标点击弹出文字
- 3、鼠标点击弹出烟花波纹
- 4、鼠标小星星拖尾跟随
- 5、鼠标粒子随心拖尾跟随
- 6、鼠标笑脸跟随+仙女棒+泡泡+雪花+点击烟花效果(自由组合)
- 7、樱花特效
- 8、蜘蛛网特效
- 9、看板娘(左下角的小人)
- 10、烟花
1、鼠标点击弹出爱心
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!-- 网页鼠标点击特效(爱心) --> <script type="text/javascript"> ! function (e, t, a) { function r() { for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[ e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e] .scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999"); requestAnimationFrame(r) } function n() { var t = "function" == typeof e.onclick && e.onclick; e.onclick = function (e) { t && t(), o(e) } } function o(e) { var a = t.createElement("div"); a.className = "heart", s.push({ el: a, x: e.clientX - 5, y: e.clientY - 5, scale: 1, alpha: 1, color: c() }), t.body.appendChild(a) } function i(e) { var a = t.createElement("style"); a.type = "text/css"; try { a.appendChild(t.createTextNode(e)) } catch (t) { a.styleSheet.cssText = e } t.getElementsByTagName("head")[0].appendChild(a) } function c() { return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math .random()) + ")" } var s = []; e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e .mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) { setTimeout(e, 1e3 / 60) }, i( ".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}" ), n(), r() }(window, document); </script> </body> </html>
2、鼠标点击弹出文字
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> (function () { var a_idx = 0; window.onclick = function (event) { var a = new Array("❤富强❤", "❤民主❤", "❤文明❤", "❤和谐❤", "❤自由❤", "❤平等❤", "❤公正❤", "❤法治❤", "❤爱国❤", "❤敬业❤", "❤诚信❤", "❤友善❤"); var heart = document.createElement("b"); //创建b元素 heart.onselectstart = new Function('event.returnValue=false'); //防止拖动 document.body.appendChild(heart).innerHTML = a[a_idx]; //将b元素添加到页面上 a_idx = (a_idx + 1) % a.length; heart.style.cssText = "position: fixed;left:-100%;"; //给p元素设置样式 var f = 16, // 字体大小 x = event.clientX - f / 2, // 横坐标 y = event.clientY - f, // 纵坐标 c = randomColor(), // 随机颜色 a = 1, // 透明度 s = 1.2; // 放大缩小 var timer = setInterval(function () { //添加定时器 if (a <= 0) { document.body.removeChild(heart); clearInterval(timer); } else { heart.style.cssText = "font-size:16px;cursor: default;position: fixed;color:" + c + ";left:" + x + "px;top:" + y + "px;opacity:" + a + ";transform:scale(" + s + ");"; y--; a -= 0.016; s += 0.002; } }, 15) } // 随机颜色 function randomColor() { return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math .random() * 255)) + ")"; } }()); </script> </body> </html>
3、鼠标点击弹出烟花波纹
代码
<html> <head> </head> <body> <script> function clickEffect() { let balls = []; let longPressed = false; let longPress; let multiplier = 0; let width, height; let origin; let normal; let ctx; const colours = ["#F73859", "#14FFEC", "#00E0FF", "#FF99FE", "#FAF15D"]; const canvas = document.createElement("canvas"); document.body.appendChild(canvas); canvas.setAttribute("style", "width: 100%; height: 100%; top: 0; left: 0; z-index: 99999; position: fixed; pointer-events: none;"); const pointer = document.createElement("span"); pointer.classList.add("pointer"); document.body.appendChild(pointer); if (canvas.getContext && window.addEventListener) { ctx = canvas.getContext("2d"); updateSize(); window.addEventListener('resize', updateSize, false); loop(); window.addEventListener("mousedown", function(e) { pushBalls(randBetween(10, 20), e.clientX, e.clientY); document.body.classList.add("is-pressed"); longPress = setTimeout(function(){ document.body.classList.add("is-longpress"); longPressed = true; }, 500); }, false); window.addEventListener("mouseup", function(e) { clearInterval(longPress); if (longPressed == true) { document.body.classList.remove("is-longpress"); pushBalls(randBetween(50 + Math.ceil(multiplier), 100 + Math.ceil(multiplier)), e.clientX, e.clientY); longPressed = false; } document.body.classList.remove("is-pressed"); }, false); window.addEventListener("mousemove", function(e) { let x = e.clientX; let y = e.clientY; pointer.style.top = y + "px"; pointer.style.left = x + "px"; }, false); } else { console.log("canvas or addEventListener is unsupported!"); } function updateSize() { canvas.width = window.innerWidth * 2; canvas.height = window.innerHeight * 2; canvas.style.width = window.innerWidth + 'px'; canvas.style.height = window.innerHeight + 'px'; ctx.scale(2, 2); width = (canvas.width = window.innerWidth); height = (canvas.height = window.innerHeight); origin = { x: width / 2, y: height / 2 }; normal = { x: width / 2, y: height / 2 }; } class Ball { constructor(x = origin.x, y = origin.y) { this.x = x; this.y = y; this.angle = Math.PI * 2 * Math.random(); if (longPressed == true) { this.multiplier = randBetween(14 + multiplier, 15 + multiplier); } else { this.multiplier = randBetween(6, 12); } this.vx = (this.multiplier + Math.random() * 0.5) * Math.cos(this.angle); this.vy = (this.multiplier + Math.random() * 0.5) * Math.sin(this.angle); this.r = randBetween(8, 12) + 3 * Math.random(); this.color = colours[Math.floor(Math.random() * colours.length)]; } update() { this.x += this.vx - normal.x; this.y += this.vy - normal.y; normal.x = -2 / window.innerWidth * Math.sin(this.angle); normal.y = -2 / window.innerHeight * Math.cos(this.angle); this.r -= 0.3; this.vx *= 0.9; this.vy *= 0.9; } } function pushBalls(count = 1, x = origin.x, y = origin.y) { for (let i = 0; i < count; i++) { balls.push(new Ball(x, y)); } } function randBetween(min, max) { return Math.floor(Math.random() * max) + min; } function loop() { ctx.fillStyle = "rgba(255, 255, 255, 0)"; ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < balls.length; i++) { let b = balls[i]; if (b.r < 0) continue; ctx.fillStyle = b.color; ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2, false); ctx.fill(); b.update(); } if (longPressed == true) { multiplier += 0.2; } else if (!longPressed && multiplier >= 0) { multiplier -= 0.4; } removeBall(); requestAnimationFrame(loop); } function removeBall() { for (let i = 0; i < balls.length; i++) { let b = balls[i]; if (b.x + b.r < 0 || b.x - b.r > width || b.y + b.r < 0 || b.y - b.r > height || b.r < 0) { balls.splice(i, 1); } } } } clickEffect();//调用特效函数 </script> </body> </html>
4、鼠标小星星拖尾跟随
代码
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <span class="js-cursor-container"></span> <script> (function fairyDustCursor() { var possibleColors = ["#D61C59", "#E7D84B", "#1B8798"] var width = window.innerWidth; var height = window.innerHeight; var cursor = { x: width / 2, y: width / 2 }; var particles = []; function init() { bindEvents(); loop(); } // Bind events that are needed function bindEvents() { document.addEventListener('mousemove', onMouseMove); window.addEventListener('resize', onWindowResize); } function onWindowResize(e) { width = window.innerWidth; height = window.innerHeight; } function onMouseMove(e) { cursor.x = e.clientX; cursor.y = e.clientY; addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]); } function addParticle(x, y, color) { var particle = new Particle(); particle.init(x, y, color); particles.push(particle); } function updateParticles() { // Updated for (var i = 0; i < particles.length; i++) { particles[i].update(); } // Remove dead particles for (var i = particles.length - 1; i >= 0; i--) { if (particles[i].lifeSpan < 0) { particles[i].die(); particles.splice(i, 1); } } } function loop() { requestAnimationFrame(loop); updateParticles(); } /** * Particles */ function Particle() { this.character = "*"; this.lifeSpan = 120; //ms this.initialStyles = { "position": "fixed", "display": "inline-block", "top": "0px", "left": "0px", "pointerEvents": "none", "touch-action": "none", "z-index": "10000000", "fontSize": "25px", "will-change": "transform" }; // Init, and set properties this.init = function (x, y, color) { this.velocity = { x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2), y: 1 }; this.position = { x: x + 10, y: y + 10 }; this.initialStyles.color = color; this.element = document.createElement('span'); this.element.innerHTML = this.character; applyProperties(this.element, this.initialStyles); this.update(); document.querySelector('.js-cursor-container').appendChild(this.element); }; this.update = function () { this.position.x += this.velocity.x; this.position.y += this.velocity.y; this.lifeSpan--; this.element.style.transform = "translate3d(" + this.position.x + "px," + this.position.y + "px, 0) scale(" + (this.lifeSpan / 120) + ")"; } this.die = function () { this.element.parentNode.removeChild(this.element); } } /** * Utils */ // Applies css `properties` to an element. function applyProperties(target, properties) { for (var key in properties) { target.style[key] = properties[key]; } } if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init(); })(); </script> </body> </html>
5、鼠标粒子随心拖尾跟随
代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>粒子随心动画</title> <script src="/uploads/allimg/230118/060T960G-5.jpg"></script> <style> body { overflow: hidden; margin: 0; } .twitter:hover a { transform: rotate(-45deg) scale(1.05); } .twitter:hover i { color: #21c2ff; } .twitter a { bottom: -40px; right: -75px; transform: rotate(-45deg); } .twitter i { bottom: 7px; right: 7px; color: #00aced; } .social-icon a { position: absolute; background: white; color: white; box-shadow: -1px -1px 20px 0px rgba(0, 0, 0, 0.3); display: inline-block; width: 150px; height: 80px; transform-origin: 50% 50%; transition: 0.15s ease-out; } .social-icon i { position: absolute; pointer-events: none; z-index: 1000; transition: 0.15s ease-out; } .youtube:hover a { transform: rotate(45deg) scale(1.05); } .youtube:hover i { color: #ec4c44; } .youtube a { bottom: -40px; left: -75px; transform: rotate(45deg); } .youtube i { bottom: 7px; left: 7px; color: #e62117; } </style> </head> <body> <canvas></canvas> <script> "use strict"; // Initial Setup var canvas = document.querySelector("canvas"); var c = canvas.getContext("2d"); canvas.width = innerWidth; canvas.height = innerHeight; // Variables var mouse = { x: innerWidth / 2, y: innerHeight / 2 - 80, }; var colors = ["#00bdff", "#4d39ce", "#088eff"]; // Event Listeners addEventListener("mousemove", function (event) { mouse.x = event.clientX; mouse.y = event.clientY; }); addEventListener("resize", function () { canvas.width = innerWidth; canvas.height = innerHeight; init(); }); // Utility Functions function randomIntFromRange(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } function randomColor(colors) { return colors[Math.floor(Math.random() * colors.length)]; } // Objects function Particle(x, y, radius, color) { var _this = this; var distance = randomIntFromRange(50, 120); this.x = x; this.y = y; this.radius = radius; this.color = color; this.radians = Math.random() * Math.PI * 2; this.velocity = 0.05; this.distanceFromCenter = { x: distance, y: distance, }; this.prevDistanceFromCenter = { x: distance, y: distance, }; this.lastMouse = { x: x, y: y }; this.update = function () { var lastPoint = { x: _this.x, y: _this.y }; // Move points over time _this.radians += _this.velocity; // Drag effect _this.lastMouse.x += (mouse.x - _this.lastMouse.x) * 0.05; _this.lastMouse.y += (mouse.y - _this.lastMouse.y) * 0.05; // Circular Motion _this.distanceFromCenter.x = _this.prevDistanceFromCenter.x + Math.sin(_this.radians) * 100; _this.distanceFromCenter.y = _this.prevDistanceFromCenter.x + Math.sin(_this.radians) * 100; _this.x = _this.lastMouse.x + Math.cos(_this.radians) * _this.distanceFromCenter.x; _this.y = _this.lastMouse.y + Math.sin(_this.radians) * _this.distanceFromCenter.y; _this.draw(lastPoint); }; this.draw = function (lastPoint) { c.beginPath(); c.strokeStyle = _this.color; c.lineWidth = _this.radius; c.moveTo(lastPoint.x, lastPoint.y); c.lineTo(_this.x, _this.y); c.stroke(); c.closePath(); }; } // Implementation var particles = undefined; function init() { particles = []; for (var i = 0; i < 50; i++) { var radius = Math.random() * 2 + 1; particles.push( new Particle( canvas.width / 2, canvas.height / 2, radius, randomColor(colors) ) ); } } // Animation Loop function animate() { requestAnimationFrame(animate); c.fillStyle = "rgba(255, 255, 255, 0.05)"; c.fillRect(0, 0, canvas.width, canvas.height); particles.forEach(function (particle) { particle.update(); }); } init(); animate(); </script> </body> </html>
6、鼠标笑脸跟随+仙女棒+泡泡+雪花+点击烟花效果(自由组合)
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!--光标特效仙女棒--> <script src="/uploads/allimg/230118/060T92D8-7.jpg"></script> <!--光标之泡泡--> <script src="/uploads/allimg/230118/060T95419-8.jpg"></script> <!--笑脸--> <script src="/uploads/allimg/230118/060T942N-9.jpg"></script> <!--雪花--> <script src="/uploads/allimg/230118/060T93555-10.jpg"></script> <!-- 点击后出现烟花效果 --> <script src="/uploads/allimg/230118/060T9A59-11.jpg"></script> </body> </html>
7、樱花特效
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3.org/1999/xhtml"> <head> <meta cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"> <link rel="stylesheet" href="cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css"> <script src="cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script> <script src="cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.js"></script> <script src="cdn.jsdelivr.net/npm/jquery.scrollto@2.1.2/jquery.scrollTo.min.js"></script> <script src="cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/autoload.js"></script>
10、烟花
代码
<!doctype html> <html> <head> <meta charset="utf-8"> <title>逼真的烟花</title> <script src="js/jquery.min.js"></script> <style> body { margin: 0; padding: 0; background: #000000; overflow: hidden; } </style> </head> <body> <canvas id="canvas"></canvas> <!-- best viewed in chrome --> <script> // fun options! const PARTICLES_PER_FIREWORK = 150; // 100 - 400 or try 1000 const FIREWORK_CHANCE = 0.02; // percentage, set to 0 and click instead const BASE_PARTICLE_SPEED = 0.6; // between 0-4, controls the size of the overall fireworks const FIREWORK_LIFESPAN = 600; // ms const PARTICLE_INITIAL_SPEED = 4.5; // 2-8 // not so fun options =\ const GRAVITY = 9.8; const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let particles = []; let disableAutoFireworks = false; let resetDisable = 0; let loop = () => { if (!disableAutoFireworks && Math.random() < FIREWORK_CHANCE) { createFirework(); } ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach((particle, i) => { particle.animate(); particle.render(); if (particle.y > canvas.height || particle.x < 0 || particle.x > canvas.width || particle.alpha <= 0 ) { particles.splice(i, 1); } }); requestAnimationFrame(loop); }; let createFirework = ( x = Math.random() * canvas.width, y = Math.random() * canvas.height ) => { let speed = (Math.random() * 2) + BASE_PARTICLE_SPEED; let maxSpeed = speed; let red = ~~(Math.random() * 255); let green = ~~(Math.random() * 255); let blue = ~~(Math.random() * 255); // use brighter colours red = (red < 150 ? red + 150 : red); green = (green < 150 ? green + 150 : green); blue = (blue < 150 ? blue + 150 : blue); // inner firework for (let i = 0; i < PARTICLES_PER_FIREWORK; i++) { let particle = new Particle(x, y, red, green, blue, speed); particles.push(particle); maxSpeed = (speed > maxSpeed ? speed : maxSpeed); } // outer edge particles to make the firework appear more full for (let i = 0; i < 40; i++) { let particle = new Particle(x, y, red, green, blue, maxSpeed, true); particles.push(particle); } }; class Particle { constructor( x = 0, y = 0, red = ~~(Math.random() * 255), green = ~~(Math.random() * 255), blue = ~~(Math.random() * 255), speed, isFixedSpeed ) { this.x = x; this.y = y; this.red = red; this.green = green; this.blue = blue; this.alpha = 0.05; this.radius = 1 + Math.random(); this.angle = Math.random() * 360; this.speed = (Math.random() * speed) + 0.1; this.velocityX = Math.cos(this.angle) * this.speed; this.velocityY = Math.sin(this.angle) * this.speed; this.startTime = (new Date()).getTime(); this.duration = Math.random() * 300 + FIREWORK_LIFESPAN; this.currentDiration = 0; this.dampening = 30; // slowing factor at the end this.colour = this.getColour(); if (isFixedSpeed) { this.speed = speed; this.velocityY = Math.sin(this.angle) * this.speed; this.velocityX = Math.cos(this.angle) * this.speed; } this.initialVelocityX = this.velocityX; this.initialVelocityY = this.velocityY; } animate() { this.currentDuration = (new Date()).getTime() - this.startTime; // initial speed kick if (this.currentDuration <= 200) { this.x += this.initialVelocityX * PARTICLE_INITIAL_SPEED; this.y += this.initialVelocityY * PARTICLE_INITIAL_SPEED; this.alpha += 0.01; this.colour = this.getColour(240, 240, 240, 0.9); } else { // normal expansion this.x += this.velocityX; this.y += this.velocityY; this.colour = this.getColour(this.red, this.green, this.blue, 0.4 + (Math.random() * 0.3)); } this.velocityY += GRAVITY / 1000; // slow down particles at the end if (this.currentDuration >= this.duration) { this.velocityX -= this.velocityX / this.dampening; this.velocityY -= this.velocityY / this.dampening; } if (this.currentDuration >= this.duration + this.duration / 1.1) { // fade out at the end this.alpha -= 0.02; this.colour = this.getColour(); } else { // fade in during expansion if (this.alpha < 1) { this.alpha += 0.03; } } } render() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); ctx.lineWidth = this.lineWidth; ctx.fillStyle = this.colour; ctx.shadowBlur = 8; ctx.shadowColor = this.getColour(this.red + 150, this.green + 150, this.blue + 150, 1); ctx.fill(); } getColour(red, green, blue, alpha) { return `rgba(${red || this.red}, ${green || this.green}, ${blue || this.blue}, ${alpha || this.alpha})`; } } let updateCanvasSize = () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }; // run it! updateCanvasSize(); $(window).resize(updateCanvasSize); $(canvas).on('click', (e) => { createFirework(e.clientX, e.clientY); // stop fireworks when clicked, re-enable after short time disableAutoFireworks = true; clearTimeout(resetDisable); resetDisable = setTimeout(() => { disableAutoFireworks = false; }, 5000); }); loop(); </script> </body> </html>
以上就是基于JS实现十种酷炫的网页特效的详细内容,更多关于JS网页特效的资料请关注自由互联其它相关文章!

