HTML、CSS和JavaScript编写的吹泡泡的网页代码和注释:

以下是使用HTML、CSS和JavaScript编写的吹泡泡的网页代码和注释:

<!DOCTYPE html>
<html>
<head>
	<title>Bubble Blower</title>
	<style>
		body {
			background-color: #f2f2f2;
		}
		#bubble {
			position: absolute;
			background-color: #ffffff;
			border-radius: 50%;
			box-shadow: 0px 0px 20px #ffffff;
			animation: bubble 3s ease-out;
			animation-fill-mode: forwards;
		}
		@keyframes bubble {
			0% {
				width: 0px;
				height: 0px;
				opacity: 1;
			}
			50% {
				width: 100px;
				height: 100px;
				opacity: 0.5;
			}
			100% {
				width: 200px;
				height: 200px;
				opacity: 0;
			}
		}
	</style>
</head>
<body>
	<h1>Bubble Blower</h1>
	<button onclick="blowBubble()">Blow Bubble</button>
	<div id="bubble"></div>
	<script>
		function blowBubble() {
			// 创建一个div元素作为泡泡
			var bubble = document.createElement("div");
			// 给泡泡设置id
			bubble.setAttribute("id", "bubble");
			// 将泡泡添加到body中
			document.body.appendChild(bubble);
			// 设置泡泡的位置为鼠标点击的位置
			bubble.style.left = event.clientX - 100 + "px";
			bubble.style.top = event.clientY - 100 + "px";
		}
	</script>
</body>
</html>

注释:

  1. <!DOCTYPE html>:声明文档类型为HTML5。
  2. <html>:HTML文档的根元素。
  3. <head>:文档头部,用于包含文档的元数据。
  4. <title>:文档的标题,显示在浏览器的标签页上。
  5. <style>:用于定义文档的样式。
  6. body:文档的主体部分。
  7. background-color:设置背景颜色为灰色。
  8. #bubble:选择id为bubble的元素。
  9. position:设置元素的定位方式为绝对定位。
  10. background-color:设置元素的背景颜色为白色。
  11. border-radius:设置元素的圆角半径为50%。
  12. box-shadow:设置元素的阴影效果。
  13. animation:设置元素的动画效果,使用名为bubble的动画,持续时间为3秒,缓动方式为ease-out。
  14. animation-fill-mode:设置动画结束后元素的状态,保持在最后一帧的状态。
  15. @keyframes bubble:定义名为bubble的动画。
  16. 0%:动画开始时元素的状态。
  17. 50%:动画进行到50%时元素的状态。
  18. 100%:动画结束时元素的状态。
  19. width:设置元素的宽度。
  20. height:设置元素的高度。
  21. opacity:设置元素的透明度。
  22. <h1>:定义文档的一级标题。
  23. <button>:定义一个按钮。
  24. onclick:设置按钮的点击事件。
  25. blowBubble():定义一个名为blowBubble的函数,用于创建泡泡。
  26. document.createElement("div"):创建一个div元素。
  27. setAttribute("id", "bubble"):给元素设置id属性为bubble。
  28. appendChild(bubble):将元素添加到文档中。
  29. event.clientX:获取鼠标点击的x坐标。
  30. event.clientY:获取鼠标点击的y坐标。
  31. style.left:设置元素的左边距。
  32. style.top:设置元素的上边距。

进阶优化版本

<!DOCTYPE html>
<html>

<head>
    <title>Bubble Blower</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            width: 100vw;
            height: 100vh;
            overflow: hidden;
            background-color: #3a3434;
        }

        #bubble {
            position: absolute;
            /* background-color: #ffffff; */
            border-radius: 50%;
            box-shadow: 0px 0px 20px #ffffff;
            animation: bubble 3s ease-out;
            animation-fill-mode: forwards;
        }

        @keyframes bubble {
            0% {
                width: 0px;
                height: 0px;
                opacity: 1;
                filter: hue-rotate(0);
            }

            50% {
                width: 100px;
                height: 100px;
                opacity: 0.5;
            }

            100% {
                width: 200px;
                height: 200px;
                opacity: 0;
                filter: hue-rotate(360deg);
            }
        }
    </style>
</head>

<body>
    <h1>Bubble Blower</h1>
    <!-- <button onclick="blowBubble()">Blow Bubble</button> -->
    <!-- <div id="bubble"></div> -->
    <script>
        let bodyEle = document.body
        bodyEle.addEventListener("click", blowBubble)
        function blowBubble(event) {
            // 创建一个div元素作为泡泡
            var bubble = document.createElement("div");
            // 给泡泡设置id
            bubble.setAttribute("id", "bubble");
            // 将泡泡添加到body中
            document.body.appendChild(bubble);
            // 设置泡泡的位置为鼠标点击的位置
            bubble.style.left = event.clientX + "px";
            bubble.style.top = event.clientY + "px";
            bubble.style.backgroundColor=`rgb(${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)},${Math.floor(Math.random()*255)})`
           
            setTimeout(() => {
                bubble.remove()
            }, 3000)
        }
        let lastExecutedTime = 0;
        function blowBubbleJieLiu(event){
            
            let currentTime = Date.now();
            let timeSinceLastExecution = currentTime - lastExecutedTime;
            console.log(currentTime , lastExecutedTime);
            if (timeSinceLastExecution >= 50) {

                blowBubble(event)

                lastExecutedTime=currentTime
            }else{
                console.log("不行");
                return
            }


        }

        bodyEle.addEventListener("mousedown", blowBubble)
        bodyEle.addEventListener("mousemove", blowBubbleJieLiu)
        
        // 随机
        setInterval(() => {
            // 创建一个div元素作为泡泡
            let bubble = document.createElement("div");
            // 给泡泡设置id
            bubble.setAttribute("id", "bubble");
            // 将泡泡添加到body中
            document.body.appendChild(bubble);
            // 设置泡泡的位置为鼠标点击的位置
            w = Math.floor(Math.random()*window.innerWidth)
            h = Math.floor(Math.random()*window.innerHeight)
            bubble.style.left = w + "px";
            bubble.style.top = h + "px";
            setTimeout(() => {
                bubble.remove()
            }, 3000)
        }, 1500)













    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值