我在循环上有一个简单的滚动文本行情自动收录器。但是我在循环在jQuery中循环时会略微跳一个问题。
我不确定如何设置“超时”,以便使其一个平稳的连续周期?我曾尝试更改window.setTimeout(callback, 1000 / 10);
,但它不断跳跃。我想知道这是否不是最好的方法?
// polyfill
window.requestAnimationFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 10);
};
})();
var speed = 5000;
(function ticker() {
var tickeritemWidth = $('.ticker-item:first-child').outerWidth();
$("#ticker").animate({
marginLeft: -tickeritemWidth
}, speed, 'linear', function() {
$(this).css({
marginLeft: 0
}).find("li:last").after($(this).find("li:first"));
});
requestAnimationFrame(ticker);
})();
#ticker-wrapper {
height: 10%;
background: red;
overflow: hidden;
width: 100%;
margin: 0;
position: relative;
padding: 30px 0;
}
#ticker {
overflow: hidden;
width: 1000%;
margin: 0;
background: red;
}
.ticker-item {
display: inline-block;
padding: 0 50px;
font-size: 4rem;
font-family: arial;
text-transform: uppercase;
color: pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="ticker-wrapper">
<ul id="ticker">
<li class="ticker-item">
#mjhonjhedolls
</li>
<li class="ticker-item">
@mjhonjhedolls
</li>
<li class="ticker-item">
#mjhonjhedolls
</li>
<li class="ticker-item">
@mjhonjhedolls
</li>
<li class="ticker-item">
#mjhonjhedolls
</li>
</ul>
</section>
答案 0 :(得分:0)
不使用setTimeout尝试以下片段和动画,
var speed = 5000;
(function ticker(){
$('.ticker-item').each(function(index){
var tickeritemWidth = $(this).outerWidth();
$("#ticker").animate({marginLeft:-tickeritemWidth},speed, 'linear', function(){
$(this).css({marginLeft:0}).find("li:last").after($(this).find("li:first"));
});
});
ticker();
})();
#ticker-wrapper {
height: 10%;
background: red;
overflow: hidden;
width: 100%;
margin: 0;
position: relative;
padding: 30px 0;
}
#ticker {
overflow: hidden;
width: 1000%;
margin: 0;
background: red;
}
.ticker-item {
display: inline-block;
padding: 0 50px;
font-size: 4rem;
font-family: arial;
text-transform: uppercase;
color: pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="ticker-wrapper">
<ul id="ticker">
<li class="ticker-item">
#mjhonjhedolls
</li>
<li class="ticker-item">
@mjhonjhedolls
</li>
<li class="ticker-item">
#mjhonjhedolls
</li>
<li class="ticker-item">
@mjhonjhedolls
</li>
<li class="ticker-item">
#mjhonjhedolls
</li>
</ul>
</section>
答案 1 :(得分:0)
这是CSS动画解决方案:
#ticker-wrapper {
height: 5rem;
padding: 1.2rem 0 1rem;
background-color: red;
margin: 0;
position: relative;
overflow: hidden;
font-size: 4rem;
font-family: arial;
text-transform: uppercase;
}
#ticker {
width: 48em;
margin: 0;
animation: marquee 8s linear infinite;
position: absolute;
}
#ticker li {
display: inline-block;
margin: 0;
padding: 0;
text-align: center;
color: pink;
width: 12em;
}
@keyframes marquee {
0% {
left: 0;
}
100% {
left: -24em;
}
}
<section id="ticker-wrapper">
<ul id="ticker">
<li>
#mjhonjhedolls
</li><li>
@mjhonjhedolls
</li><li>
#mjhonjhedolls
</li>
</ul>
</section>