【发布时间】:2018-02-15 22:20:05
【问题描述】:
我试图在第一次单击时使 svg 旋转,然后在第二次单击时顺利旋转回原始位置。我有以下代码。
HTML:
<div class="quick-access">
<div class="quick-access__toggle">
<svg id="toggle-button" viewBox="0 0 100 100" >
<path d="M 50 30 L 50 70" stroke="white" stroke-width="5"/>
<path d="M 30 50 L 70 50" stroke="white" stroke-width="5"/>
</svg>
</div>
</div>
SCSS:
$colour-orange: #faab18;
.quick-access{
position: absolute;
bottom: 20px;
right: 20px;
&__toggle{
width: 50px;
height: 50px;
border-radius: 50%;
background: $colour-orange;
}
}
.open{
background: white;
svg{
transition-property: transform;
transition-duration: 0.7s;
path{
stroke: $colour-orange;
}
}
}
.overlay{
background: rgba(30,30,30,0.6);
}
JS:
$('.quick-access__toggle').on('click', function(){
$(this).toggleClass('open');
if($(this).hasClass('open')){
$('#toggle-button').css('transform', 'rotate(225deg)');
}else{
$('#toggle-button').css('transform', 'rotate(0deg)');
}
$('body').toggleClass('overlay')
})
我想要发生的是 svg 在单击时旋转到 225 度,然后在第二次单击时旋转到 0 度。目前它旋转到 225 度,但在第二次点击时它不会像第一次点击那样顺畅地旋转,它只是重置回原来的位置。我认为 transition-property 和 transition-duration 可以做到这一点,但它只适用于第一次点击。我尝试过做 js 动画,但它不支持转换,我宁愿不使用插件或 polyfill 来完成这项工作。
【问题讨论】:
标签: javascript jquery svg sass css-transitions