开发者

Image Swap using jQuery

I hope somebody could help me out with this. I'm working on a portfolio website that currently looks like this:

http://cargocollective.com/shap

As you can see, I have a png image over 15 animated gif thumbnails.

Basically, I want to have some short gif animations popping into some of the screens from a time to time, so what i'm trying to do is to have a script that replaces some of the gifs with others, either randomly OR after a certain amount of time.

I hope that someone can understand what i mean and help me.

__________ *UPDATE*:

Thanks a lot, I really appreciate the help. I'm sorry for the lack of knowladge I have, making it hard for me to use what you just wrote there, but this is what I added to my source code:

<script type="text/javascript">
$(document).ready(function() { changer(); });

function changer() {
  var imgnum = Math.floor(15*Math.random());
  var time = Math开发者_C百科.floor(5000*Math.random());
  var $img = $("img").eq(imgnum);

  if ($img.attr("src")=="http://payload.cargocollective.com/1/1/39798/479556/prt_166x132_1314132436.gif")
    $img.attr("src","http://payload.cargocollective.com/1/1/39798/479849/prt_166x132_1314132538.gif");
  else
    $img.attr("src","http://payload.cargocollective.com/1/1/39798/479556/prt_166x132_1314132436.gif");
  setTimeout(changer,time);
}
</script>

I just copied the URL of 2 of my gif thumbnails and added them to the script, is that absolutely wrong? Maybe the fact that I'm using the cargocollective system makes things complicated. Here is what it looks like right now, as you can see, something IS happening, but I can't really control it:

http://cargocollective.com/shap

(after a minute the png overlay is gone for whatever reason)

best, s


Tube televisions! How wonderfully quaint. ;-)

Well, here's something I tossed together. Obviously you should change the actual image SRCs into something that exists on your server.

http://jsfiddle.net/9KrsV/1/

$(function() { changer(); });

function changer() {
  var imgnum = Math.floor(15*Math.random());
  var time = Math.floor(5000*Math.random());
  var $img = $("img").eq(imgnum);

  if ($img.attr("src")=="images/chalk-dotted.png")
    $img.attr("src","images/chalk-box.png");
  else
    $img.attr("src","images/chalk-dotted.png");
  setTimeout(changer,time);
}

UPDATE: Obviously, this is pretty narrow. Here's another script that will store the old SRC and eventually flip back to it:

function changer() {
    var imgnum = Math.floor(15 * Math.random());
    var time = Math.floor(5000 * Math.random());
    var $img = $("img").eq(imgnum);
    var newsrc = "noisy_static.gif";

    if ($img.attr("src") == newsrc) {
        $img.attr("src", $img.data("oldsrc"));
    } else {
        $img.data("oldsrc",$img.attr("src"));
        $img.attr("src", newsrc);
    }
    setTimeout(changer, time);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜