Literally explode text apart to random places with

2019-04-09 21:16发布

问题:

There are some jQuery examples, how to implode a text, like this one:

http://jsfiddle.net/doktormolle/dNXVx/

How can I make the reverse?

I want to explode the letters from a span element to random places.

回答1:

function fx(o) {
  var $o = $(o);

  $o.html($o.text().replace(/([\S])/g, "<span>$1</span>"));
  $o.css("position", "relative");
  $("span", $o).each(function(i) {
    var newTop = Math.floor(Math.random()*500)*((i%2)?1:-1);
    var newLeft = Math.floor(Math.random()*500)*((i%2)?1:-1);

    $(this).css({position: "relative",
      opacity: 1,
      fontSize: 12,
      top: 0,
      left: 0
    }).animate({
      opacity: 0,
      fontSize: 84,
      top: newTop,
      left:newLeft
    },1000);
  });
}​

You can see the code in action at http://jsfiddle.net/dNXVx/37/