logo DevAnswer - Developers Answer

Random colorful balls in background

7th February

Preview


Source Code
                            <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from www.devanswer.com -->


<style>
.ball {
    position: absolute;
    border-radius: 100%;
    opacity: 0.7;
    z-index: 0;
}
.center{
    display: flex;
    justify-content: center;
    align-items: center;
   margin: 250px 100px;
}
.text{
    font-size: 30px;
    padding: 40px;
    border-radius: 3px;
    background: #ffbfbf;
    box-shadow: 1px 1px 21px rgba(0 ,0 ,0 ,0.25);
}
.box {
    position: relative;
    z-index: 100;
}
</style>

</head>
<body>
<div class="box">
  <div class="text center">
    welcome to my random ball site
  </div>
</div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Developers Answer</a></div>
<script>
 // Some random colors
const colors = ["#3CC157", "#2AA7FF", "#1B1B1B", "#FCBC0F", "#F85F36"];
const numBalls = 50;
const balls = [];
for (let i = 0; i < numBalls; i++) {
        let ball = document.createElement("div");
        ball.classList.add("ball");
        ball.style.background = colors[Math.floor(Math.random() * colors.length)];
        ball.style.left = `${Math.floor(Math.random() * 100)}vw`;
        ball.style.top = `${Math.floor(Math.random() * 100)}vh`;
        ball.style.transform = `scale(${Math.random()})`;
        ball.style.width = `${Math.random()}em`;
        ball.style.height = ball.style.width;
        balls.push(ball);
        document.body.append(ball);
}
    // Keyframes
    balls.forEach((el, i, ra) => {
        let to = {
            x: Math.random() * (i % 2 === 0 ? -11 : 11),
            y: Math.random() * 12
        };
        let anim = el.animate(
            [
                { transform: "translate(0, 0)" },
                { transform: `translate(${to.x}rem, ${to.y}rem)` }
            ],
            {
                duration: (Math.random() + 1) * 2000, // random duration
                direction: "alternate",
                fill: "both",
                iterations: Infinity,
                easing: "ease-in-out"
            }
        );
    });
</script>

</body>
</html>                        



Other Codes