Advertisement
Guest User

[Marquee] addMarquee

a guest
Jan 9th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.     var addEvent = comYahooAnswersPuckettTim.base.addEvent;
  4.     var beforeLoad = true;
  5.  
  6.     var addMarquee = function ()
  7.     {
  8.         var support = document.getElementById("support");
  9.         var supportChild = support && support.firstChild;
  10.         var head = document.getElementsByTagName("head")[0];
  11.  
  12.         if (!supportChild || !head)
  13.         {
  14.             (beforeLoad)
  15.                 && !(beforeLoad = false)
  16.                 && addEvent(addMarquee);
  17.             return;
  18.         }
  19.  
  20.         var marqueeRel = document.createAttribute("rel");
  21.         marqueeRel.value = "stylesheet";
  22.         var marqueeHref = document.createAttribute("href");
  23.         marqueeHref.value = "marquee-style.txt";
  24.         var marqueeStyle = document.createElement("link");
  25.         marqueeStyle.setAttributeNode(marqueeRel);
  26.         marqueeStyle.setAttributeNode(marqueeHref);
  27.         head.insertBefore(marqueeStyle, head.lastChild);
  28.  
  29.         var marquee = document.createElement("div");
  30.  
  31.         var position = 0;
  32.         var width = 0;
  33.         var parentWidth = 0;
  34.  
  35.         var mouseover = false;
  36.         var mousecap = document.createElement("div");
  37.         mousecap.appendChild(marquee);
  38.  
  39.         var focus = function (e)
  40.         {
  41.             if (!isStop && !mouseover) return;
  42.  
  43.             var max = parentWidth - width;
  44.             if (width > parentWidth) return;
  45.             else if (position < 0)
  46.                 position = (position > -1) ? 0
  47.                     : (position * 2/3);
  48.             else if (position > max)
  49.                 position = (position - max < 1) ? (position = max)
  50.                     : (position*2 + max)/3;
  51.             else return;
  52.             marquee.style.left = position + "px";
  53.             setTimeout(function (e) { focus(e) }, 1);
  54.             return true;
  55.         }
  56.  
  57.         var isStop = true;
  58.         var step = -4;
  59.  
  60.         var scroll = function (e)
  61.         {
  62.             if (isStop || mouseover) return;
  63.             position += step;
  64.             if (position < -width) position = parentWidth;
  65.             if (position > parentWidth) position = -width;
  66.             marquee.style.left = position + "px";
  67.             setTimeout(function (e) { scroll(e) }, 20);
  68.         }
  69.  
  70.         mousecap.onmouseover = function (e) {
  71.             mouseover = true;
  72.             focus(e);
  73.             return true;
  74.         }
  75.  
  76.         mousecap.onmouseout = function (e) {
  77.             mouseover = false;
  78.             scroll(e);
  79.             return true;
  80.         }
  81.  
  82.         while (supportChild) {
  83.             if (!parentWidth && 1===supportChild.nodeType) parentWidth = supportChild.offsetWidth;
  84.             var marqueeChild = supportChild;
  85.             supportChild = supportChild.nextSibling;
  86.             marquee.appendChild(marqueeChild);
  87.         }
  88.  
  89.         var start = function (e)
  90.         {
  91.             this.onclick = stop;
  92.             this.firstChild.nodeValue = "Stop the marquee";
  93.             isStop = false;
  94.             if (marquee.offsetWidth) width = marquee.offsetWidth;
  95.             position = parentWidth - step;
  96.             scroll(e);
  97.             return true;
  98.         }
  99.  
  100.         var stop = function (e)
  101.         {
  102.             isStop = true;
  103.             focus(e);
  104.             this.onclick = start;
  105.             this.firstChild.nodeValue = "Start the marquee";
  106.             return true;
  107.         }
  108.  
  109.         var startButton = document.createElement("button");
  110.         startButton.onclick = start;
  111.         startButton.appendChild(document.createTextNode("Start the marquee"));
  112.  
  113.         support.appendChild(startButton);
  114.         support.insertBefore(mousecap, startButton);
  115.         return true;
  116.     }
  117.  
  118.     if (comYahooAnswersPuckettTim.base.W3CDOM) addMarquee();
  119. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement