Unscramble effect in jQuery while maintaining the format
But it seems only to work with text that has no format. I want that the text formatting remains unchanged.
The unscramble plugin:
- (function($) {
- $.fn.unscramble = function() {
- this.each(function() {
- var $ele = $(this), str = $ele.text(), replace = /[^\s]/,
- state = [], choose = [], reveal = 25, random = randomAlphaNum;
-
- for (var i = 0; i < str.length; i++) {
- if (str[i].match(replace)) {
- state.push(random());
- choose.push(i);
- } else {
- state.push(str[i]);
- }
- }
-
- shuffle(choose);
- $ele.text(state.join(''));
-
- var timer = setInterval(function() {
- var i, r = reveal;
- while (r-- && choose.length) {
- i = choose.pop();
- state[i] = str[i];
- }
- for (i = 0; i < choose.length; i++) state[choose[i]] = random();
- $ele.text(state.join(''));
- if (choose.length == 0) clearInterval(timer);
- }, 100);
- });
- return this;
- };
- })(jQuery);
As you can see when pressed 'Unscramble me' the text format and the <br> tags are removed.
Due to my lack of skills in making jQuery plug-ins, I don't know what the problem is. I think it goes wrong when replacing /[^\s]/ but I'm not sure.
Somebody has an answer?
Thanks in advance.