如何使用css动画从中心增加边界线
how to increase border line from the center using css animation
我在玩CSS动画,我想知道是否有一种方法可以在加载页面时自动增加一条垂直线(具有一定高度)的长度。理想情况下,我希望垂直线从中间增长,从顶部和底部增长到特定的高度。到目前为止,我只能让它从上到下增加长度。这是我的:
.vertical-line {
margin-left: 100px;
background: red;
width: 8px;
border-radius: 4px;
animation: grow 4s forwards;
}
@keyframes grow {
0% {
height: 10px;
}
100% {
height: 100px;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Creating Vertical borders using animation/javascript</title>
</head>
<body>
<div class="vertical-line"></div>
</body>
</html>
代码的错误之处在于您只是增加了高度。为了在增加高度的同时在两侧生长,必须将该元素移向另一侧
示例:如果你的高度增加了100px,那么你必须反向移动50px
CSS:
#cool
{
height:10px;
width:10px;
border-radius:4px;
margin-left:10%;
background-color:#431;
margin-top:20%;
animation:grow 3s forwards;
position:relative;
}
@keyframes grow
{
0% {
height: 0px;
top:0;
}
100%{
height: 200px;
top:-100px;
}
}
</style>
HTML:
<div id=cool>
</div>
</body>
对于高度100px,将元素移动到顶部-50px。拿了一半,因为显示了两边的增长。如果顶部为-100px,那么它将从底部生长。
我希望这能帮助
实现这一点的一种方法是将线的初始位置设置在最中心,然后使其向视口的顶部和底部延伸。
.myLine {
position: absolute;
left: 50vw;
top: 50vh;
width: 1px;
height: 1px;
}
然后,您可以通过JavaScript添加一个类extended
,该类可以更改位置和高度,从而使其看起来从中心垂直延伸。
.extended {
top: 0vh;
bottom: 100vh;
height: 100%;
transition: all 3s ease;
}
使用JavaScript,正如我在这里所做的那样,您可以设置一个短暂的超时,并在超时结束后添加类。
var el = document.querySelector('.myLine');
setTimeout(function() {
el.classList.add('extended');
}, 300);
请参阅我的示例代码笔。
试试这个,
<div class="vertical-line"></div>
<style>
.vertical-line {
position: absolute;
top: 0;
border: 5px solid red;
width: 10px;
border-radius: 4px;
animation: grow 3s infinite alternate;
}
@keyframes grow {
0% {
width: 0px;
height: 0px;
}
100% {
width: 20px;
height: 100%;
}
}
</style>
相关文章:
- JQuery使计数器每次更改时都会增加
- 如何增加同时按键总数
- 如何使用css动画从中心增加边界线
- JavaScript:单击时相对于父级增加变量值
- PaperJS-增加矢量幅度
- 如何每10.6秒从时间戳增加+1
- 如何通过ajax增加/减少PHP变量值并重新加载函数
- 使用Jquery增加选项卡
- 通过JSON中的值增加
- 根据元素高度和宽度的百分比变化增加或减少字体大小
- 如何制作侧边栏以增加其完整长度
- 在SoundCloud中,使用JavaScript SDK流式传输曲目是否会增加该曲目的playback_count
- 如何增加引导程序进度条
- JavaScript到PHP的时间戳失败,增加了大约一个半月的时间
- 引导时间选择器在增加或减少小时和分钟的同时获取 NaN
- 函数比javascript中的步进器增加/减少更多
- 对象数组-与其他列数据相比,增加一列
- 使用$(el).addClass('example');但是不要't增加负载
- 如何使用jquery滑块增加日期
- Extjs:增加JsonStore的大小