因为你已经旋转了这条线,所以你还需要改变它的位置,使它看起来不会下拉。
添加
top:2px;
到.remove .line
.
2px
匹配跨度的top:2px
$('button').click(function(){
$('.main').toggleClass('remove')
})
.line{
width:60px;
height:2px;
background:#000;
transform: rotate(30deg);
position:absolute;
top:20px;
z-index:1;
transition: all 0.3s ease-in-out;
}
.remove .line{
width:0;
top:2px;
}
.main{
position:relative;
}
span{
display:inline-block;
width:40px;
height:40px;
background:red;
border-radius:50%;
position:relative;
left:8px;
top:2px
}
button{
margin-top:60px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="line"></div>
<span></span>
</div>
<button>toggle</button>
更新:将 'top' 设置为 2px 会导致可见的跳跃 - 结果top
不受transition
. 受影响的是margin
.
由于有 30 度的旋转,您可以计算出所需的确切值,但稍作试验和错误会给出一个工作版本,使用:
margin-top:-16px;
margin-left:3px;
$('button').click(function(){
$('.main').toggleClass('remove')
})
.line{
width:60px;
height:2px;
background:#000;
transform: rotate(30deg);
position:absolute;
top:20px;
z-index:1;
transition: all 0.3s ease-in-out;
}
.remove .line{
width:0;
margin-top:-16px;
margin-left:3px;
}
.main{
position:relative;
}
span{
display:inline-block;
width:40px;
height:40px;
background:red;
border-radius:50%;
position:relative;
left:8px;
top:2px
}
button{
margin-top:60px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="line"></div>
<span></span>
</div>
<button>toggle</button>
额外:更改margin
也导致请求添加使其反向。
同样,通过一些试验和错误,可以根据 30 度角计算出精确值,给出:
margin-left:55px;
margin-top:15.5px;
$('button').click(function(){
$('.main').toggleClass('remove')
})
.line{
width:60px;
height:2px;
background:#000;
transform: rotate(30deg);
position:absolute;
top:20px;
z-index:1;
transition: all 0.3s ease-in-out;
}
.remove .line{
width:0;
margin-left:55px;
margin-top:15.5px;
}
.main{
position:relative;
}
span{
display:inline-block;
width:40px;
height:40px;
background:red;
border-radius:50%;
position:relative;
left:8px;
top:2px
}
button{
margin-top:60px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="line"></div>
<span></span>
</div>
<button>toggle</button>