gpt4 book ai didi

javascript - css 和 javascript 如何协同工作?

转载 作者:行者123 更新时间:2023-11-28 02:50:25 25 4
gpt4 key购买 nike

我在 javascript 的帮助下通过动态添加 css 样式使一些气泡随机移动。
在这里,我使用了 addElement 方法来添加多个 div block 。现在,当我在 body 中创建一个新元素并尝试在 <style> 下使用没有 javaScript 的 css 设置样式时元素不起作用。

<!DOCTYPE html>
<html>

<head>
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
border-radius: 100px;
box-shadow:0 0 10px #0000e6;
-webkit-transition: 5s linear;
}
.box:hover {
width: 100px;
height: 100px;
border-radius: 100px;
background-color:red;

}
}
.next
{
position: absolute;
border-radius: 10px;
width: 40px;
height: 20px;
box-shadow:0 0 10px #0000e6;
left:500px;
top:400px;
color:white;
}
</style>

</head>

<body bgcolor="black" onload="bbbl()">

<h2 class="next">Next</h2>

<script>



function getRandom(max) {
return Math.random() * (max - 0) + 0;
}

function moveRandom(){
var box = document.getElementsByClassName('box')
for (var i = 0; i < box.length; i++) {
box[i].style.left = (getRandom(window.innerWidth) -100) + 'px';
box[i].style.top = (getRandom(window.innerHeight) -100) + 'px';
}
}

function bbbl() {

for (i = 0; i <= 51; i++) {
var el = document.createElement('div')
el.classList.add('box')
el.style.backgroundColor = "black";
el.style.opacity = "0.9";
document.body.insertBefore(el, document.body.childNodes[0])

moveRandom()
}
}

bbbl()
setInterval(moveRandom, 5000)

</script>

</body>

</html>

最佳答案

<!DOCTYPE html>
<html>

<head>
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
border-radius: 100px;
box-shadow:0 0 10px #0000e6;
-webkit-transition: 5s linear;
}
.box:hover {
width: 100px;
height: 100px;
border-radius: 100px;
background-color:red;

}
} <<<<<<<<<<<<< delete
.next
{
position: absolute;
border-radius: 10px;
width: 40px;
height: 20px;
box-shadow:0 0 10px #0000e6;
left:500px;
top:400px;
color:white;
}
</style>

</head>

<body bgcolor="black" onload="bbbl()">

<h2 class="next">Next</h2>

<script>



function getRandom(max) {
return Math.random() * (max - 0) + 0;
}

function moveRandom(){
var box = document.getElementsByClassName('box')
for (var i = 0; i < box.length; i++) {
box[i].style.left = (getRandom(window.innerWidth) -100) + 'px';
box[i].style.top = (getRandom(window.innerHeight) -100) + 'px';
}
}

function bbbl() {

for (i = 0; i <= 51; i++) {
var el = document.createElement('div')
el.classList.add('box')
el.style.backgroundColor = "black";
el.style.opacity = "0.9";
document.body.insertBefore(el, document.body.childNodes[0])

moveRandom()
}
}

bbbl()
setInterval(moveRandom, 5000)

</script>

</body>

</html>

关于javascript - css 和 javascript 如何协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39554443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com