2017-08-23 51 views
3

我正在嘗試創建一個連續滾動而無需在循環之間中斷的水平文本的新聞滾動器。理想情況下,解決方案將是純粹的CSS/HTML,但我不知道這是可能的。這是我迄今爲止的基本嘗試:http://jsfiddle.net/lgants/ncgsrnza/。請注意,小提琴包含每個循環之間不需要的中斷。純CSS連續水平文本無間斷滾動

<p class="marquee"><span>This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text</span></p> 


.marquee { 
    margin: 0 auto; 
    white-space: nowrap; 
    overflow: hidden; 
} 

.marquee span { 
    display: inline-block; 
    padding-left: 100%; 
    animation: marquee 5s linear infinite; 
} 
+0

唔...是可以複製的文本?否則,我猜想有一點點的JS只需幾次克隆文本層 – lumio

+0

@lumio你的意思是重複的文本是什麼意思?我將使用React,因此重用組件很容易。 – lgants

+0

讓我試着解釋...... :)給我幾分鐘 – lumio

回答

2

你可以試試有兩個帳篷,並設置其中的一個具有2.5秒的延遲動畫這是全動畫(5秒)的一半的時間。

.marquee { 
 
    margin: 0 auto; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    position: absolute; 
 
} 
 

 
.marquee span { 
 
    display: inline-block; 
 
    padding-left: 100%; 
 
    animation: marquee 5s linear infinite; 
 
} 
 

 
.marquee2 span { 
 
    animation-delay: 2.5s; 
 
} 
 

 
@keyframes marquee { 
 
    0% { 
 
    transform: translate(0, 0); 
 
    } 
 
    100% { 
 
    transform: translate(-100%, 0); 
 
    } 
 
}
<p class="marquee"> 
 
    <span>This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text -&nbsp;</span> 
 
</p> 
 
<p class="marquee marquee2"> 
 
    <span>This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text - This is text -&nbsp;</span> 
 
</p>

+2

只是一個評論:沒有必要重複關鍵幀(marquee2)爲了這個概念的工作。 – Jonathan

+0

哎呀,好電話。刪除了第二個關鍵幀。 – rmurph46

+2

@ rmurph46清理了一下你的答案,以減少多餘的代碼,修復一個錯誤並將它帶到Stack Snippet。幹得好! –