当前位置: 代码网 > it编程>编程语言>Javascript > HTML很火的浪漫爱心表白代码

HTML很火的浪漫爱心表白代码

2024年05月15日 Javascript 我要评论
程序员的你是不是也想送个特别的礼物。今天给大家分享一个html+css+jquery实现的情侣浪漫爱心表白js特效,视觉效果还是相当不错!得此表白神器,程序猿也可以很浪漫!快去追你的女神吧,把这个告白

 程序员的你是不是也想送个特别的礼物。今天给大家分享一个html+css+jquery实现的情侣浪漫爱心表白js特效,视觉效果还是相当不错!得此表白神器,程序猿也可以很浪漫!快去追你的女神吧,把这个告白爱心动画发给你心爱的她!

html代码如下,亲测可用。

<!doctype html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="x-ua-compatible" content="ie=edge">
		<title>diy跳动爱心</title>
		<style>
			* {
				padding: 0;
				margin: 0;
			}

			body {
				height: 600px;
				padding: 0;
				margin: 0;
				background: #000;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.container {
				width: 500px;
				height: 500px;
				position: relative;
			}

			canvas {
				z-index: 99;
				position: absolute;
				width: 500px;
				height: 500px;
			}

			.text_box {
				text-align: center;
				position: absolute;
				font-size: 1.125rem;
				top: 36%;
				left: 22%;
				color: #ff437b;
				z-index: 100;
			}

			input {
				font-size: 1.375rem;
				color: #ff437b;
				text-align: center;
				background: none;
			}

			button {
				font-size: 1.375rem;
				border: none;
				border-radius: 4px;
			}

			input::input-placeholder {
				color: #dc4b61;
			}

			input::-webkit-input-placeholder {
				color: #dc4b61;
			}


			.heart {
				animation: heart 1s infinite ease-in-out;
			}

			@keyframes heart {

				0%,
				100% {
					transform: rotate(-2deg) scale(1);
				}

				50% {
					transform: rotate(2deg) scale(1.12);
				}
			}
		</style>
	</head>
	<body>
		<div id="jsi-cherry-container" class="container ">
			<!-- 爱心 -->
			<canvas id="pinkboard" class="container heart"> </canvas>
			<!-- 输入你需要的文字 -->
			<div class="text_box">
				<input type="text" id="text" placeholder="送给你的那个[ta]️">
				<button id="btn" onclick="fn()">❤️</button>
			</div>

		</div>
	</body>
	<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
	<script>
		function fn() {
			var a1 = document.queryselector('#text');
			var btn = document.queryselector('#btn');
			a1.style.border = 'none';
			btn.parentnode.removechild(btn);
			console.log("点关注不迷路!");
		}
	</script>
	<script>
		/*
		 * settings
		 */
		var settings = {
			particles: {
				length: 500, // maximum amount of particles
				duration: 2, // particle duration in sec
				velocity: 100, // particle velocity in pixels/sec
				effect: -0.75, // play with this for a nice effect
				size: 30, // particle size in pixels
			},
		};

		(function() {
			var b = 0;
			var c = ["ms", "moz", "webkit", "o"];
			for (var a = 0; a < c.length && !window.requestanimationframe; ++a) {
				window.requestanimationframe = window[c[a] + "requestanimationframe"];
				window.cancelanimationframe =
					window[c[a] + "cancelanimationframe"] ||
					window[c[a] + "cancelrequestanimationframe"];
			}
			if (!window.requestanimationframe) {
				window.requestanimationframe = function(h, e) {
					var d = new date().gettime();
					var f = math.max(0, 16 - (d - b));
					var g = window.settimeout(function() {
						h(d + f);
					}, f);
					b = d + f;
					return g;
				};
			}
			if (!window.cancelanimationframe) {
				window.cancelanimationframe = function(d) {
					cleartimeout(d);
				};
			}
		})();

		/*
		 * point class
		 */
		var point = (function() {
			function point(x, y) {
				this.x = typeof x !== "undefined" ? x : 0;
				this.y = typeof y !== "undefined" ? y : 0;
			}
			point.prototype.clone = function() {
				return new point(this.x, this.y);
			};
			point.prototype.length = function(length) {
				if (typeof length == "undefined")
					return math.sqrt(this.x * this.x + this.y * this.y);
				this.normalize();
				this.x *= length;
				this.y *= length;
				return this;
			};
			point.prototype.normalize = function() {
				var length = this.length();
				this.x /= length;
				this.y /= length;
				return this;
			};
			return point;
		})();

		/*
		 * particle class
		 */
		var particle = (function() {
			function particle() {
				this.position = new point();
				this.velocity = new point();
				this.acceleration = new point();
				this.age = 0;
			}
			particle.prototype.initialize = function(x, y, dx, dy) {
				this.position.x = x;
				this.position.y = y;
				this.velocity.x = dx;
				this.velocity.y = dy;
				this.acceleration.x = dx * settings.particles.effect;
				this.acceleration.y = dy * settings.particles.effect;
				this.age = 0;
			};
			particle.prototype.update = function(deltatime) {
				this.position.x += this.velocity.x * deltatime;
				this.position.y += this.velocity.y * deltatime;
				this.velocity.x += this.acceleration.x * deltatime;
				this.velocity.y += this.acceleration.y * deltatime;
				this.age += deltatime;
			};
			particle.prototype.draw = function(context, image) {
				function ease(t) {
					return --t * t * t + 1;
				}
				var size = image.width * ease(this.age / settings.particles.duration);
				context.globalalpha = 1 - this.age / settings.particles.duration;
				context.drawimage(
					image,
					this.position.x - size / 2,
					this.position.y - size / 2,
					size,
					size
				);
			};
			return particle;
		})();

		/*
		 * particlepool class
		 */
		var particlepool = (function() {
			var particles,
				firstactive = 0,
				firstfree = 0,
				duration = settings.particles.duration;

			function particlepool(length) {
				// create and populate particle pool
				particles = new array(length);
				for (var i = 0; i < particles.length; i++)
					particles[i] = new particle();
			}
			particlepool.prototype.add = function(x, y, dx, dy) {
				particles[firstfree].initialize(x, y, dx, dy);

				// handle circular queue
				firstfree++;
				if (firstfree == particles.length) firstfree = 0;
				if (firstactive == firstfree) firstactive++;
				if (firstactive == particles.length) firstactive = 0;
			};
			particlepool.prototype.update = function(deltatime) {
				var i;

				// update active particles
				if (firstactive < firstfree) {
					for (i = firstactive; i < firstfree; i++)
						particles[i].update(deltatime);
				}
				if (firstfree < firstactive) {
					for (i = firstactive; i < particles.length; i++)
						particles[i].update(deltatime);
					for (i = 0; i < firstfree; i++) particles[i].update(deltatime);
				}

				// remove inactive particles
				while (
					particles[firstactive].age >= duration &&
					firstactive != firstfree
				) {
					firstactive++;
					if (firstactive == particles.length) firstactive = 0;
				}
			};
			particlepool.prototype.draw = function(context, image) {
				// draw active particles
				if (firstactive < firstfree) {
					for (i = firstactive; i < firstfree; i++)
						particles[i].draw(context, image);
				}
				if (firstfree < firstactive) {
					for (i = firstactive; i < particles.length; i++)
						particles[i].draw(context, image);
					for (i = 0; i < firstfree; i++) particles[i].draw(context, image);
				}
			};
			return particlepool;
		})();

		/*
		 * putting it all together
		 */
		(function(canvas) {
			var context = canvas.getcontext("2d"),
				particles = new particlepool(settings.particles.length),
				particlerate =
				settings.particles.length / settings.particles.duration, // particles/sec
				time;

			// get point on heart with -pi <= t <= pi
			function pointonheart(t) {
				return new point(
					160 * math.pow(math.sin(t), 3),
					130 * math.cos(t) -
					50 * math.cos(2 * t) -
					20 * math.cos(3 * t) -
					10 * math.cos(4 * t) +
					25
				);
			}

			// creating the particle image using a dummy canvas
			var image = (function() {
				var canvas = document.createelement("canvas"),
					context = canvas.getcontext("2d");
				canvas.width = settings.particles.size;
				canvas.height = settings.particles.size;
				// helper function to create the path
				function to(t) {
					var point = pointonheart(t);
					point.x =
						settings.particles.size / 2 +
						(point.x * settings.particles.size) / 350;
					point.y =
						settings.particles.size / 2 -
						(point.y * settings.particles.size) / 350;
					return point;
				}
				// create the path
				context.beginpath();
				var t = -math.pi;
				var point = to(t);
				context.moveto(point.x, point.y);
				while (t < math.pi) {
					t += 0.01; // baby steps!
					point = to(t);
					context.lineto(point.x, point.y);
				}
				context.closepath();
				// create the fill
				context.fillstyle = "#dc4b61";
				context.fill();
				// create the image
				var image = new image();
				image.src = canvas.todataurl();
				return image;
			})();

			// render that thing!
			function render() {
				// next animation frame
				requestanimationframe(render);

				// update time
				var newtime = new date().gettime() / 1000,
					deltatime = newtime - (time || newtime);
				time = newtime;

				// clear canvas
				context.clearrect(0, 0, canvas.width, canvas.height);

				// create new particles
				var amount = particlerate * deltatime;
				for (var i = 0; i < amount; i++) {
					var pos = pointonheart(math.pi - 2 * math.pi * math.random());
					var dir = pos.clone().length(settings.particles.velocity);
					particles.add(
						canvas.width / 2 + pos.x,
						canvas.height / 2 - pos.y,
						dir.x,
						-dir.y
					);
				}

				// update and draw particles
				particles.update(deltatime);
				particles.draw(context, image);
			}

			// handle (re-)sizing of the canvas
			function onresize() {
				canvas.width = canvas.clientwidth;
				canvas.height = canvas.clientheight;
			}
			window.onresize = onresize;

			// delay rendering bootstrap
			settimeout(function() {
				onresize();
				render();
			}, 10);
		})(document.getelementbyid("pinkboard"));
	</script>
</html>

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com