[英]How can I make this circle go to the bottom of the screen only css?
So I have this code. 所以我有这个代码。
@keyframes anim{
0% {background-color: red; top: 0px; }
100% {background-color: yellow; top: 800px;}
}
#circle {
height: 100px;
width: 100px;
border-radius: 50px;
background-color: red;
animation-name: anim;
animation-duration: 3s;
position: relative;
}
The circle is a div element. 圆是div元素。 This animation makes it to go down by 800px.
这个动画使它下降800px。 But I want to make to go down only as much as is the size of the browser and then to stop.
但我想让它只下降到浏览器的大小然后停止。 So that no scroll appears.
这样就不会出现滚动。 I need to do this with CSS ONLY.
我只需要用CSS做这件事。 Is there any way to do so?
有没有办法这样做?
You can use: 您可以使用:
top: 100vh
where 100vh means 100% of the vertical viewport height. 其中100vh表示垂直视口高度的100%。 Also, if you don't want a scroll on the page, you can either set "overflow: hidden" on the body or make the circle stop just in time:
此外,如果您不想在页面上滚动,可以在主体上设置“overflow:hidden”或者使圆圈及时停止:
top: calc(100vh - 100px)
Example: https://jsfiddle.net/4sjf2t9y/ 示例: https : //jsfiddle.net/4sjf2t9y/
You can animate as a percentage rather than as pixels. 您可以以百分比形式设置动画而不是像素。 The problem is, you can't say 100% minus the height of the element with just
top
or bottom
( edit my bad, turns out you can! see other answer). 问题是,你不能说100%减去元素的高度只有
top
或bottom
( 编辑我的坏,结果你可以!看到其他答案)。
But you can animate it to top:100%
which would push it off the end of the page, then use translateY
(different to top
) to bring it back. 但你可以将它设置为
top:100%
将它推离页面末尾,然后使用translateY
(不同于top
)将其恢复。 See below: 见下文:
html, body{
height:100%;
}
@keyframes anim{
0% {background-color: red; top: 0%; transform:translateY(0px); }
100% {background-color: yellow; top: 100%; transform:translateY(-100px);}
}
#circle {
height: 100px;
width: 100px;
border-radius: 50px;
background-color: red;
animation-name: anim;
animation-duration: 3s;
position: relative;
}
See demo here: http://codepen.io/EightArmsHQ/pen/avKwRr 请参阅此处的演示: http : //codepen.io/EightArmsHQ/pen/avKwRr
You can viewport percentages: 您可以使用视口百分比:
@keyframes anim{
0% {background-color: red; top: 0px; }
100% {background-color: yellow; top:100vh;}
}
#circle {
height: 100px;
width: 100px;
border-radius: 50px;
background-color: red;
animation-name: anim;
animation-duration: 3s;
position: relative;
}
but since your ball will go outside the frame when you put it at 100vh. 但是因为当你把球放到100vh时你的球会越过框架。 Use like 80.
使用像80。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.