javascript - 动画创建一个 div 来移动我的页面内容?

标签 javascript html css

我有以下站点,我尝试在该站点上实现非常漂亮的动画。我不知道为什么,但它一直在我的页面周围创建空白,如果我还添加一个按钮(就像我在 fiddle 中所做的那样),它就会变得疯狂。有什么解决办法?

fiddle :https://jsfiddle.net/7suL84my/

动画代码:

// 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"
}
);
});

最佳答案

从您在 css 文件上的链接更改此内容:

#main{
background-color:black;
color:white;
width:98%;
height:98%;
align-content: center;

}

为此:

#main{
background-color:black;
color:white;
width:100%;
height:100%;
align-content: center;

}

关于javascript - 动画创建一个 div 来移动我的页面内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272784/

相关文章:

javascript - 在 JavaScript 中转义单引号

javascript - 使用另一个元素的 x 和 y 偏移定位一个 div 元素

javascript - 无法解析 strip Angular 依赖注入(inject)

javascript - 将对象传递给 jQuery 方法

javascript url 值到多个字段

html - 为什么 Microsoft Edge 在我的网站上检测到错误的语言?

html - 不同尺寸的李

css - 如果不支持@supports,@supports 'not' 关键字是否有效?

forms - label 元素的 for 属性必须引用一个表单控件

javascript - Moment.js,获取下一个 [工作日]