首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >几秒钟后,我自动生成的“点”会粘在屏幕的底部。

几秒钟后,我自动生成的“点”会粘在屏幕的底部。
EN

Stack Overflow用户
提问于 2017-02-02 05:59:57
回答 2查看 57关注 0票数 1

所以我有了这个网站(http://mc.wordquest.nl/green/dots.html),我想让圆点从页面底部漂浮到空中。在最初的几秒钟内,它工作得很好。但在一秒或五秒后,点就会留在它们的位置上。Click here for the picture (of the problem)

这些点应该漂浮到页面底部以上的100px。我通过使用CSS动画(可能不是最好的方式,但我可以使用它…)来做到这一点。

我在页面中使用的代码如下:

代码语言:javascript
运行
复制
		window.setInterval(function() {
			randomSquare();
		}, 100);

		var i = 0;

		function randomSquare() {
		
			//Set window height
			var w = window.innerWidth;
			var h = window.innerHeight;
		
			
			//Set dot x-position
			var x = Math.floor((Math.random() * w) + 1);
			var y = Math.floor((Math.random() * h) + 1);

			// Add a dot to follow the cursor
			dot = document.createElement('div');
			dot.className = "dot";
			dot.style.left = x + "px";
			dot.style.bottom = 10 + "px";

			i++;

			//Random color
			var COLOURS = ['#69D2E7', '#A7DBD8', '#E0E4CC', '#F38630',     '#FA6900', '#FF4E50', '#F9D423'];
    // 			var color = "blue";
			var color = COLOURS[Math.floor(Math.random() * COLOURS.length)];

			dot.style.backgroundColor = color;

			if (y < h * 0.97 && x < w * 0.97) {
				document.body.appendChild(dot);
			}
		};
代码语言:javascript
运行
复制
		body {
			height: 80%;
			width: 90%;
			font-family: Arial;
			font-weight: Bold;
		}
		
		.dot {
			width: 20px;
			height: 20px;
			background-color: green;
			position: absolute;
			color: white;
			z-index: 1px;
			border-radius: 50%;
			animation: dots 2s linear;
		}
		
		@keyframes dots {
			0% {
				width: 20px;
				height: 20px;
				opacity: 1;
				bottom: 0px;
			}
			100% {
    /* 				opacity: 0; */
				width: 0px;
				height: 0px;
				bottom: 100px;
			}
		}
代码语言:javascript
运行
复制
<!DOCTYPE html>
    <html>

    <head>
	<title>Test</title>
	<style>
	</style>
    </head>

    <body>

    </body>

    </html>

EN

Stack Overflow用户

发布于 2017-02-02 06:23:18

要使元素在动画结束时保持其样式,请添加:

代码语言:javascript
运行
复制
.dot {
    animation-fill-mode: forwards;
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41990593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档