我想创建一个首先开始的字幕,但是每10秒,字幕将停止5秒,然后字幕再次开始。
我该怎么做呢?
我只创建了一个在5秒后停止字幕的计时器:
<script>
function startLoop() {
setInterval( "doSomething()", 5000 ); }
function doSomething() {
document.getElementById('myMarquee').stop(); }
</script>HTML
<body onload="startLoop();">
<marquee direction="right" id="myMarquee">This is a marquee.</marquee>
</body>发布于 2012-11-20 15:46:52
几天前,我需要一些类似于你的问题的东西。我很快就发现marquee不是一个标准元素,所以你不能在跨浏览器的解决方案中使用它。
我已经提取了动画部分,基于jQuery,我在我的解决方案中使用,你可以在这个jsFiddle中看到效果
HTML
<div id="container">
<div id="mytext">
this is a simple text to test custom marquee
</div>
</div>CSS
#container
{
display: inline-block;
background-color: #cccccc;
width: 100px;
height: 100px;
overflow: hidden;
}
#mytext
{
display: inline-block;
position: relative;
white-space: nowrap;
}
JavaScript
$(function() {
var txt = $("#mytext");
txt.bind('scroll', function () {
var el = $(this);
// Scroll state machine
var scrollState = el.data("scrollState") || 0;
el.data("scrollState", (scrollState + 1) % 4);
switch (scrollState) {
case 0: // initial wait
el.css({ left: 0 });
el.show();
window.setTimeout(function () {
el.trigger("scroll");
}, 5000);
break;
case 1: // start scroll
var delta = el.parent().width() - el.width();
if (delta < 0) {
el.animate({ left: delta }, 2000, "linear", function () {
el.trigger("scroll");
});
}
break;
case 2: // delay before fade out
window.setTimeout(function () {
el.trigger("scroll");
}, 2000);
break;
case 3: // fade out
el.fadeOut("slow", function () {
el.trigger("scroll");
});
break;
}
}).trigger("scroll");
});它并不完全符合您的需求,但如果您阅读代码并对状态机进行一些更改,您将使其正常工作:)
发布于 2012-11-20 15:57:43
如果你想继续使用字幕,这应该可以(在支持字幕的浏览器中):
<script>
function stopRunning() {
document.getElementById('myMarquee').stop();
setInterval("startRunning()", 5000);
}
function startRunning() {
document.getElementById('myMarquee').start();
setInterval("stopRunning()", 10000);
}
//You don't really need a function to start the loop, you can just call startRunning();
startRunning();
</script>这将使选取框开始运行,10秒后停止,5秒后再次启动,然后重复。
发布于 2012-11-20 15:59:48
试试这个:
var myMarquee = document.getElementById('myMarquee');
run();
function run() {
setTimeout(function() {
myMarquee.stop();
setTimeout(function(){
myMarquee.start();
run();
},5000);
},10000);
} 在http://jsfiddle.net/gjwYh/上查看它的运行
https://stackoverflow.com/questions/13468441
复制相似问题