我正在尝试将一行文本中的字符分别设置为动画。我想实现的是让它们飞起来,一次一个地显示在屏幕上。我知道我可以使用转换和转换来做到这一点: translateX。但我有几个问题。首先,文本的外部div没有包装所有字符,其次,对其中一个跨度应用transform: translateX不起作用。我已经创建了一个小演示,供您检查。谢谢。
#burger_container {
background-color: #404041;
display: block;
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 60px;
z-index: 101;
}
svg {
margin: 20px auto 0 auto;
display: block;
}
#logo_text {
transform: rotate(-90deg);
margin-top: 350px;
font-size: 40px;
color: #ffffff;
font-weight: 700;
}
#logo_text span {
font-weight: 400;
}
#logo_text span:nth-child(1){
transform: translatex(-50px);
}<div id="burger_container">
<div>
<svg id="button" style="height: 26px; width: 26px;">
<g style="" fill="#f04d43">
<rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
<rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
<rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
</g>
</svg>
<div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span> <span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>
发布于 2017-07-27 19:18:17
你已经使用了span标签,默认情况下是inline元素,现在如果我改变它,然后它breaks它如何对齐的形式,你可以改为position到relative,然后使用CSS animation,你可以使用breaks属性逐个获取每个文本,如下所示,
选择每个span标签,然后如上所述使用CSS动画。
#logo_text span:nth-of-type(8) {
animation: mv 1s ease forwards;
}
#logo_text span:nth-of-type(9) {
animation: mv 1s ease forwards 1s;
}
#logo_text span:nth-of-type(10) {
animation: mv 1s ease forwards 2s;
}
#logo_text span:nth-of-type(11) {
animation: mv 1s ease forwards 3s;
}
#logo_text span:nth-of-type(12) {
animation: mv 1s ease forwards 4s;
}
#logo_text span:nth-of-type(13) {
animation: mv 1s ease forwards 5s;
}
#logo_text span:nth-of-type(14) {
animation: mv 1s ease forwards 6s;
}
#logo_text span:nth-of-type(15) {
animation: mv 1s ease forwards 7s;
}
#logo_text span:nth-of-type(16) {
animation: mv 1s ease forwards 8s;
}
@keyframes mv {
from {
top: 0;
}
to {
top: -50px;
}
}
#burger_container {
background-color: #404041;
display: block;
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 60px;
z-index: 101;
}
svg {
margin: 20px auto 0 auto;
display: block;
}
.line1,
.line2,
.line3 {
transition: all 0.3s ease;
}
.open1 {
transform-origin: top left;
transform: translatex(3px) translatey(-1px) rotate(45deg);
width: 33px;
}
.open2 {
opacity: 0;
}
.open3 {
transform-origin: bottom left;
transform: translatex(3px) translatey(1px) rotate(-45deg);
width: 33px;
}
#trans_overlay {
display: none;
}
#logo_two {
display: none;
}
#logo_text {
transform: rotate(-90deg);
margin-top: 300px; /*Change this back to 350px, just to see output I have change it to 300px*/
font-size: 40px;
color: #ffffff;
font-weight: 700;
}
#logo_text span {
font-weight: 400;
position: relative;
}
#logo_text span:nth-of-type(8) {
animation: mv 1s ease forwards;
}
#logo_text span:nth-of-type(9) {
animation: mv 1s ease forwards 1s;
}
#logo_text span:nth-of-type(10) {
animation: mv 1s ease forwards 2s;
}
#logo_text span:nth-of-type(11) {
animation: mv 1s ease forwards 3s;
}
#logo_text span:nth-of-type(12) {
animation: mv 1s ease forwards 4s;
}
#logo_text span:nth-of-type(13) {
animation: mv 1s ease forwards 5s;
}
#logo_text span:nth-of-type(14) {
animation: mv 1s ease forwards 6s;
}
#logo_text span:nth-of-type(15) {
animation: mv 1s ease forwards 7s;
}
#logo_text span:nth-of-type(16) {
animation: mv 1s ease forwards 8s;
}
@keyframes mv {
from {
top: 0;
}
to {
top: -50px;
}
}<div id="burger_container">
<div>
<svg id="button" style="height: 26px; width: 26px;">
<g style="" fill="#f04d43">
<rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
<rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
<rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
</g>
</svg>
</div>
<div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span> <span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>
发布于 2017-07-27 18:56:41
把#logo_text做成flexbox,让span有display:inline-block就行了。不过,您必须使用Y轴。
#burger_container {
background-color: #404041;
display: block;
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 60px;
z-index: 101;
}
svg {
margin: 20px auto 0 auto;
display: block;
}
#logo_text {
transform: rotate(-90deg);
margin-top: 350px;
font-size: 40px;
color: #ffffff;
font-weight: 700;
display: flex;
}
#logo_text span {
font-weight: 400;
display: inline-block;
}
#logo_text span:nth-child(1){
transform: translatex(-50px);
}<div id="burger_container">
<div>
<svg id="button" style="height: 26px; width: 26px;">
<g style="" fill="#f04d43">
<rect class="line1" x="0" y="1" rx="2" ry="2" width="26px" height="4px" />
<rect class="line2" x="0" y="11" rx="2" ry="2" width="26px" height="4px" />
<rect class="line3" x="0" y="21" rx="2" ry="2" width="26px" height="4px" />
</g>
</svg>
<div id="logo_text"><span>C</span><span>o</span><span>m</span><span>p</span><span>a</span><span>n</span><span>y</span> <span>W</span><span>o</span><span>r</span><span>k</span><span>f</span><span>o</span><span>r</span><span>c</span><span>e</span></div>
</div>
发布于 2017-07-27 19:16:20
如果JavaScript是你的选择,你可以使用它。仅在此部分更改css:
#logo_text span{
position:relative;
}然后在最后将这部分包含在你的身体里。
<script>
var span_texts=document.getElementById("logo_text").getElementsByTagName("span");
for(var i=0;i<span_texts.length;i++){
(function(j){
var elem=span_texts[j];
setTimeout(function(){
elem.setAttribute("style","top:-70px");
},300*j+300);
})(i);
}
</script>您可以看到文本离开屏幕(向左),一个time.The动画开始于300ms,随后的每个字母在每300ms后离开屏幕。
https://stackoverflow.com/questions/45347855
复制相似问题