Online Shopping : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 31 people (52%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How do I script a repeating ticker?
How do I script a ticker tape?

Apr 2nd, 2009 07:45
engatoo engatoo, Martin Honnen,


One way to scroll text on a page is described in
http://www.faqts.com/knowledge-base/view.phtml/aid/1536/fid/128/lang/en
This entry uses a slightly different technique to achieve the scroll 
effect, namely it uses only one layer/positioned element (so no 
scrollContainer) but simultanously moves the layer and changes the 
clipping to achieve the scrolling effect. To get a repeating ticker 
the 
content of the ticker is duplicated once.
<HTML>
<HEAD>
<STYLE>
.white { color: white; font-size: 16pt; }
</STYLE>
<SCRIPT>
function Ticker (left, top, width, height, content, spd, dx) {
  this.id = 'Ticker' + Ticker.cnt;
  this.cnt = Ticker.cnt;
  Ticker.elements[Ticker.cnt++] = this;
  this.width = width * 2;
  this.halfWidth = width;
  this.left = left - width;
  this.top = top;
  this.height = height;
  this.content = content;
  this.spd = spd || 20;
  this.dx = dx || 2;
  this.createLayer();
  this.initCoords();
  this.setCoords();
  this.makeVisible();
  this.startScrolling();
}
function Ticker_createLayer () {
  var html = '';
  html += '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"';
  html += '       WIDTH="' + this.width + '" HEIGHT="' + this.height 
+ '"';
  html += '>';
  html += '<TR>';
  var td = '';
  td += '<TD WIDTH="' + this.halfWidth + '" HEIGHT="' + this.height 
+ '"';
  td += '>';
  td += this.content;
  td += '<\/TD>';
  html += td + td;
  html += '<\/TR>';
  html += '<\/TABLE>';
  if (document.layers) {
    var l = this.layer = new Layer(this.width);
    l.document.open();
    l.document.write(html);
    l.document.close();
  }
  else if (document.all) {
    var lhtml = '';
    lhtml += '<DIV ID="' + this.id + '"';
    lhtml += '     STYLE="position: absolute; ';
    lhtml += '            visibility: hidden; overflow: hidden;"';
    lhtml += '>';
    lhtml += html;
    lhtml += '<\/DIV>';
    document.body.insertAdjacentHTML('beforeEnd', lhtml);
    this.layer = document.all[this.id];
  }
  else if (document.createElement) {
    var l = this.layer = document.createElement('DIV');
    l.style.position = 'absolute';
    l.style.visibility = 'hidden';
    l.style.overflow = 'hidden';
    document.body.appendChild(l);
    var range = document.createRange();
    range.setStartAfter(l);
    var docFrag = range.createContextualFragment(html);
    l.appendChild(docFrag);
  }
}
Ticker.prototype.createLayer = Ticker_createLayer;
function Ticker_initCoords () {
  if (this.dx > 0) {
    this.posLeft = this.left;
    this.clipTop = 0;
    this.clipBottom = this.height;
    this.clipLeft = this.halfWidth;
    this.clipRight = this.width;
  }
  else {
    this.posLeft = this.left + this.halfWidth;
    this.clipTop = 0;
    this.clipBottom = this.height;
    this.clipLeft = 0;
    this.clipRight = this.halfWidth;
  }
}
Ticker.prototype.initCoords = Ticker_initCoords;
function Ticker_setCoords () {
  var l = this.layer;
  if (document.layers) {
    l.left = this.posLeft;
    l.top = this.top;
    l.clip.left = this.clipLeft;
    l.clip.right = this.clipRight;
  }
  else if (document.all) {
    l.style.pixelLeft = this.posLeft;
    l.style.pixelTop = this.top;
    l.style.clip = 
      'rect(' + this.clipTop + ' ' + this.clipRight + ' ' +
                this.clipBottom + ' ' + this.clipLeft + ')';
  }
  else if (document.getElementById) {
    l.style.left = this.posLeft + 'px';
    l.style.top = this.top + 'px';
    l.style.clip = 
      'rect(' + this.clipTop + ' ' +
                (this.width - this.clipRight) + ' ' +
                '0 ' + this.clipLeft + ')';
  }
}
Ticker.prototype.setCoords = Ticker_setCoords;
function Ticker_makeVisible () {
  if (document.layers)
    this.layer.visibility = 'show';
  else if (document.all || document.getElementById)
    this.layer.style.visibility = 'visible';
}
Ticker.prototype.makeVisible = Ticker_makeVisible;
function Ticker_startScrolling () {
  this.stopScrolling();
  this.tid = setInterval ('Ticker.elements[' + this.cnt + '].scroll
()', 
this.spd);
}
Ticker.prototype.startScrolling = Ticker_startScrolling;
function Ticker_scroll () {
  this.changeCoords();
  this.setCoords();
}
Ticker.prototype.scroll = Ticker_scroll;
function Ticker_changeCoords () {
  this.posLeft += this.dx;
  this.clipLeft -= this.dx;
  this.clipRight -= this.dx;
  if ((this.dx > 0 && this.posLeft >= this.left + this.halfWidth) ||
      (this.dx < 0 && this.posLeft <= this.left))
    this.initCoords();
}
Ticker.prototype.changeCoords = Ticker_changeCoords;
function Ticker_changeDirection () {
  this.dx = -this.dx;
}
Ticker.prototype.changeDirection = Ticker_changeDirection;
function Ticker_stopScrolling () {
  clearInterval(this.tid);
}
Ticker.prototype.stopScrolling = Ticker_stopScrolling;
Ticker.cnt = 0;
Ticker.elements = new Array();
</SCRIPT>
<SCRIPT>
var ticker1, ticker2;
function init () {
  var w = window.innerWidth ? window.innerWidth : 
           document.body.clientWidth;
  var l1 = Math.floor((w - 150) / 2);
  var content = '<TABLE WIDTH="150" HEIGHT="50" BORDER="0"';
  content += ' CELLPADDING="0" CELLSPACING="0" BGCOLOR="lime">';
  content += '<TR><TD>';
  content += 'All for Kibology.<BR>Kibology for all.';
  content += '<\/TD><\/TR><\/TABLE>';
  ticker1 = new Ticker (l1, 100, 150, 50, content);
  var l2 = Math.floor((w - 400) / 2);
  content = '<TABLE WIDTH="400" HEIGHT="50" BORDER="0"';
  content += ' CELLPADDING="0" CELLSPACING="0">';
  content += '<TR>'
  content += '<TD WIDTH="200" HEIGHT="50" BGCOLOR="lime" ' 
+ 'CLASS="white">';
  content += 'All for Kibology.<BR>Kibology for all.';
  content += '<\/TD>';
  content += '<TD WIDTH="200" HEIGHT="50" BGCOLOR="orange" ' 
+ 'CLASS="white">';
  content += 'Scriptology for all.<BR>JavaScript.FAQTs.com';
  content += '<\/TD>';
  content += '<\/TR><\/TABLE>';
  ticker2 = new Ticker (l2, 300, 400, 50, content, 20, -2);
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="init()">
<A HREF="javascript: ticker1.changeDirection(); void 0">
ticker1.changeDirection();
</A>
|
<A HREF="javascript: ticker1.stopScrolling(); void 0">
ticker1.stopScrolling();
</A>
|
<A HREF="javascript: ticker1.startScrolling(); void 0">
ticker1.startScrolling();
</A>
<BR>
<A HREF="javascript: ticker2.changeDirection(); void 0">
ticker2.changeDirection();
</A>
|
<A HREF="javascript: ticker2.stopScrolling(); void 0">
ticker2.stopScrolling();
</A>
|
<A HREF="javascript: ticker2.startScrolling(); void 0">
ticker2.startScrolling();
</A>
</BODY>
</HTML>
The code is currently working with NN4 and IE4+. The code is not 
correctly working with as NN6 either has some clipping bug or some 
clipping technique I do not understand.
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38