我的跨度绝对位于div中。跨度比div宽,隐藏溢出,所以我想将跨度设置为right: 0;
的动画,类似于选框效果。
如何让它从起始位置滑动到right: 0;
的位置?
.auto-option__name-container {
display: inline-block;
margin-left: 20px;
margin-top: 20px;
@include font-paragraph;
overflow-x: hidden;
position: relative;
height: 1.618em;
width: 11em;
background-color: red;
}
.animateLongName{
-webkit-animation: marquee 2s ease-in-out infinite;
position: absolute;
background-color: rgba(255,255,255,0.4);
white-space: nowrap;
}
@-webkit-keyframes marquee {
0% { right: auto; }
100% { right: 0px; }
}
<body>
<div class="auto-option__name-container">
<span class="animateLongName">
This is a really long name that won't fit in the div
</span>
</div>
</body>
答案 0 :(得分:1)
据我所知,您无法使用auto
值制作动画。一个选项可以是使用transform
属性。检查一下:
.auto-option__name-container {
display: inline-block;
margin-left: 20px;
margin-top: 20px;
@include font-paragraph;
overflow-x: hidden;
position: relative;
height: 1.618em;
width: 11em;
background-color: red;
}
.animateLongName {
-webkit-animation: marquee 4s ease-in-out infinite;
position: absolute;
background-color: rgba(255, 255, 255, 0.4);
white-space: nowrap;
}
@-webkit-keyframes marquee {
0% {
transform: translateX(100%)
}
100% {
transform: translateX(-100%)
}
}
<div class="auto-option__name-container">
<span class="animateLongName">
This is a really long name that won't fit in the div
</span>
</div>
答案 1 :(得分:0)
使用jQuery animate:
var longName = $(".animateLongName");
var parent = longName.parent();
var difference = parent.width() - longName.width();
var margin = 10;
function animateSpan() {
longName.animate({ left: difference-margin}, { duration: 5000, complete: function(){animateSpanBack()} });
}
function animateSpanBack() {
longName.animate({ left: margin}, { duration: 5000, complete: function(){animateSpan()} });
}
if (difference < 0) animateSpan();
答案 2 :(得分:0)
我让动画工作了。
注意:我使用了:after
选择器来隐藏溢出的文本;参考css
风格。