【发布时间】:2020-12-18 23:33:26
【问题描述】:
我正在研究 CSS 效果, 在代码中,我将用于显示球的 div 附加到具有随机大小和颜色的文档正文中。但是代码会生成额外的滚动空间。 我被球在水平和垂直方向上产生的额外滚动所打动。
HTML 页面不会超过两行。
到目前为止,我创建的 HTML 页面的代码 Code Pen
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://kit.fontawesome.com/cae1531884.js" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css2?family=Montserrat&family=Rubik&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
font-family: 'Montserrat', sans-serif;
}
.ball {
position: absolute;
border-radius: 100%;
opacity: 0.7;
}
.headings {
font-family: 'Rubik', sans-serif;
}
.container {
height: 100vh;
padding-left: 2rem;
position: relative;
}
.vertical-center {
margin: 0;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.social-links {
padding: 5px;
color: #121212;
}
</style>
</head>
<body>
<div class="container">
<div class="vertical-center">
<div class="header headings">
<h1>Hello World</h1>
</div>
<div class="content">
<p>Lorem Ipsum</p>
</div>
</div>
</div>
<script>
const colors = ["#3CC157", "#2AA7FF", "#d3d3d3", "#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>
【问题讨论】:
-
But there is extra scroll space being generated with the code.... 容器控制滚动行为。试试overflow-x: auto -
适用于全屏视图但不适用于移动视图
-
在移动端视图中,用户将能够对隐藏的内容进行触摸拖动
-
不需要那个touch拖拽页面不包含超过2行html内容
-
由于容器不是问题,代码的某些其他方面会导致不良结果。可能你有一个额外的结束标签或一个没有结束的开始标签。
标签: javascript html css layout responsive-design