使用javascript“闪烁”随机文本

时间:2011-08-14 19:57:08

标签: javascript

我想知道你们中是否有人知道一个可以让文字进出的脚本,比如当新的推文进来时Twitter如何自我更新,或者更具体地说是Apple的主页如何将这些文字闪烁在“Hot News Headlines”旁边”

我有自己的设置文本,所以它不像我从twitter或rss更新。

例如我有  “text1” 中的  “文本2”  “text3” 中

但是我只希望“text1”显示约...说5秒,然后“text2”出现并通过淡化或任何替换它

2 个答案:

答案 0 :(得分:1)

是的,它可以使用JavaScript setInterval函数。

//Global Scope variables, usable in all functions...
var randomStuff = ["Foo", "Bar", "Even multiple Words"];
var $target;
var loadContentIndex = 0;

$(function() {
    $target = $('#target'); //Set the target div...
    loadContent(); //Initiate it once on page load...
    window.setInterval(loadContent, 2000); //And set it to work every 2000ms (or 2s).
});

function loadContent() {
    $target.fadeOut(function() { //Once fade out is complete...
        $target.text(randomStuff[loadContentIndex]); //Change the text
        $target.fadeIn(); //Fade back in.
    });

    loadContentIndex++; //Increase the array counter.
    if (randomStuff.length == loadContentIndex) { //If reached the end of the array...
        loadContentIndex = 0; //Reset the counter :)
    }
}

注意:我正在使用jQuery库来处理动画,只是因为它更容易。

Example Here

答案 1 :(得分:0)

使用jQuery等库可以让您的生活更轻松。

要淡出元素,这是一个演示:http://jsfiddle.net/dFQCR/1/

  $('#update').show().fadeOut(1000, function() {
    // Animation complete.
  });

此处有更多效果:http://api.jquery.com/category/effects/

相关问题