Advertisement
diopralinato

jTypeWriter

Jun 23rd, 2011
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 4.31 KB | None | 0 0
  1. // jTypeWriter, JQuery plugin
  2. // v 1.1
  3. // Licensed under GPL licenses.
  4. // Copyright (C) 2008 Nikos "DuMmWiaM" Kontis, info@dummwiam.com
  5. // http://www.DuMmWiaM.com/jTypeWriter
  6. // ----------------------------------------------------------------------------
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. // ----------------------------------------------------------------------------
  25.  
  26. (function($) {
  27.     $.fn.jTypeWriter = function (b) {
  28.         var c, nIntervalCounter, nSequentialCounter, nSequentialCounterInternal, nInterval, nLoopInterval;
  29.         var d = $.extend({}, $.fn.jTypeWriter.defaults, b);
  30.         var e = d.duration * 1000;
  31.         var f = d.type.toLowerCase();
  32.         var g = d.sequential;
  33.         var h = d.onComplete;
  34.         var j = d.text;
  35.         var k = d.loop;
  36.         var l = d.loopDelay;
  37.         var m = (f == "word") ? " " : ".";
  38.         var n = new Array();
  39.         var o = 0;
  40.         for (i = 0; i < this.length; i++) {
  41.             if (j) {
  42.                 $(this[i]).text(j)
  43.             }
  44.             if (f == "letter") n.push({
  45.                 obj: $(this[i]),
  46.                 initialText: $(this[i]).text()
  47.             });
  48.             else n.push({
  49.                 obj: $(this[i]),
  50.                 initialText: $(this[i]).text().split(m)
  51.             });
  52.             if (!g) o = n[i].initialText.length > o ? n[i].initialText.length : o;
  53.             else o += n[i].initialText.length;
  54.             $(this[i]).text("")
  55.         }
  56.         init();
  57.  
  58.         function init() {
  59.             c = e / o;
  60.             nIntervalCounter = 0;
  61.             nSequentialCounter = nSequentialCounterInternal = 0;
  62.             nInterval = (!g) ? setInterval(typerSimultaneous, c) : setInterval(typerSequential, c)
  63.         };
  64.  
  65.         function typerSimultaneous() {
  66.             nIntervalCounter++;
  67.             for (i = 0; i < n.length; i++) {
  68.                 var a = n[i];
  69.                 if (a.initialText.length >= nIntervalCounter) {
  70.                     if (f == "letter") {
  71.                         a.obj.text(a.initialText.substr(0, nIntervalCounter))
  72.                     } else {
  73.                         a.obj.append(a.initialText[nIntervalCounter - 1]);
  74.                         if (nIntervalCounter < o) {
  75.                             a.obj.append(m)
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.             if (nIntervalCounter >= o) {
  81.                 circleEnd()
  82.             }
  83.         };
  84.  
  85.         function typerSequential() {
  86.             $obj = n[nSequentialCounter];
  87.             if (f == "letter") {
  88.                 $obj.obj.text($obj.initialText.substr(0, ++nSequentialCounterInternal))
  89.             } else {
  90.                 $obj.obj.append($obj.initialText[nSequentialCounterInternal++]);
  91.                 if (nSequentialCounterInternal < $obj.initialText.length) $obj.obj.append(m)
  92.             }
  93.             if (nSequentialCounterInternal >= $obj.initialText.length) {
  94.                 nSequentialCounter++;
  95.                 nSequentialCounterInternal = 0
  96.             }
  97.             nIntervalCounter++;
  98.             if (nIntervalCounter >= o) {
  99.                 circleEnd()
  100.             }
  101.         };
  102.  
  103.         function circleEnd() {
  104.             clearInterval(nInterval);
  105.             if (f != "letter") {}
  106.             if (k) {
  107.                 if (l) nLoopInterval = setInterval(loopInterval, l * 1000);
  108.                 else newLoop()
  109.             }
  110.             h()
  111.         };
  112.  
  113.         function newLoop() {
  114.             for (i = 0; i < n.length; i++) {
  115.                 n[i].obj.text("")
  116.             }
  117.             init()
  118.         };
  119.  
  120.         function loopInterval() {
  121.             newLoop();
  122.             clearInterval(nLoopInterval)
  123.         };
  124.  
  125.         function endEffect() {
  126.             clearInterval(nInterval);
  127.             for (i = 0; i < n.length; i++) {
  128.                 n[i].obj.text(n[i].initialText)
  129.             }
  130.         };
  131.         this.endEffect = endEffect;
  132.         return this
  133.     };
  134.     $.fn.jTypeWriter.defaults = {
  135.         duration: 2,
  136.         type: "letter",
  137.         sequential: true,
  138.         onComplete: function () {},
  139.         text: "",
  140.         loop: false,
  141.         loopDelay: 0
  142.     };
  143.     $.fn.jTypeWriter.variables = {
  144.         aObjects: new Array()
  145.     }
  146. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement