Connecting Tech Pros Worldwide Forums | Help | Site Map

Text Effects

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#1   Jun 1 '07
Heres some sample code I wrote up for you guys to use on making text effects

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //TEXT EFFECTS CODE WRITTEN BY iam_clint!
  3. window.onload = doEffects;
  4. var speed = 15; //lower the number the faster
  5. var color_speed = 50; //speed at which colors change
  6. var smoothing = 8; //the larger the number the less smooth but faster for sliding.
  7. var font_smoothing = 1; //lower the number the smoother the font changes.
  8. var start_top = 0;
  9. var start_left = 0;
  10. //setting global variables
  11. var curele = "";
  12. var slidedown = "";
  13. var start_topSD = "";
  14. var slideup = "";
  15. var start_topSU = "";
  16. var slideright = "";
  17. var start_leftSR = "";
  18. var slideleft = "";
  19. var start_leftSL = "";
  20. var zoomin = "";
  21. var zoomout = "";
  22. var start_zoomSize = "";
  23. var start_zoomSize0 = "";
  24. var colors = "";
  25. var R = "00";
  26. var G = "00";
  27. var B = "00";
  28. function doEffects() {
  29. var elements = document.getElementsByTagName("DIV");
  30. for (i=0; i<elements.length; i++) {
  31. var c = elements[i].getAttribute("effect").split(",");
  32. for (b=0; b<c.length; b++) {
  33. //elements[i].getAttribute("effect")
  34.     if (c[b] == "slidedown") { preset(elements[i]); start_topSD = elements[i].style.top; elements[i].style.top = 0; slidedown = window.setInterval("eSlideDown('"+curele+"')", speed); }
  35.     if (c[b] == "slideup") { preset(elements[i]); start_topSU = elements[i].style.top; elements[i].style.top = document.documentElement.offsetHeight; slideup = window.setInterval("eSlideUp('"+curele+"')", speed); }
  36.     if (c[b] == "slideright") { preset(elements[i]); start_leftSR = elements[i].style.left; elements[i].style.left = 0; slideright = window.setInterval("eSlideRight('"+curele+"')", speed); }
  37.     if (c[b] == "slideleft") { preset(elements[i]); start_leftSL = elements[i].style.left;  start_topSD = elements[i].style.top; elements[i].style.left = document.documentElement.offsetWidth; slideleft = window.setInterval("eSlideLeft('"+curele+"')", speed); }
  38.     if (c[b] == "zoomin") { preset(elements[i]); start_zoomSize = elements[i].style.fontSize; elements[i].style.fontSize = "0px"; zoomin = window.setInterval("eZoomIn('"+curele+"')", speed); }
  39.     if (c[b] == "zoomout") { preset(elements[i]); start_zoomSizeO = elements[i].style.fontSize; elements[i].style.fontSize = "100px"; zoomout = window.setInterval("eZoomOut('"+curele+"')", speed); }
  40.     if (c[b] == "colors") { preset(elements[i]); colors = window.setInterval("eColors('"+curele+"')", color_speed); }
  41. }
  42. }
  43. }
  44.  
  45. function preset(ele) {
  46.  curele = ele.getAttribute("id");
  47. }
  48. function calcHex(val) { hex = val; if (val==10) { hex = "A"; } if (val==11) { hex = "B"; } if (val==12) { hex = "C"; } if (val==13) { hex = "D"; } if (val==14) { hex = "E"; } if (val==15) { hex = "F"; } return hex; }
  49. function eSlideDown(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.top)<parseFloat(start_topSD)) { ele.style.top = Math.floor(parseFloat(ele.style.top)+smoothing)+"px"; } else { ele.style.top = start_topSD; window.clearInterval(slidedown); } }
  50. function eSlideUp(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.top)>parseFloat(start_topSU)) { ele.style.top = Math.floor(parseFloat(ele.style.top)-smoothing)+"px"; } else { ele.style.top = start_topSU; window.clearInterval(slideup); } }
  51. function eSlideLeft(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.left)>parseFloat(start_leftSL)) { ele.style.left = Math.floor(parseFloat(ele.style.left)-smoothing)+"px"; } else { ele.style.left = start_leftSL; window.clearInterval(slideleft); } }
  52. function eSlideRight(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.left)<parseFloat(start_leftSR)) { ele.style.left = Math.floor(parseFloat(ele.style.left)+smoothing)+"px"; } else { ele.style.left = start_leftSR; window.clearInterval(slideright); } }
  53. function eZoomIn(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.fontSize)<parseFloat(start_zoomSize)) { ele.style.fontSize = Math.floor(parseFloat(ele.style.fontSize)+font_smoothing)+"px"; } else { ele.style.fontSize = start_zoomSize; window.clearInterval(zoomin); } }
  54. function eZoomOut(eleid) { var ele = document.getElementById(eleid); if (parseFloat(ele.style.fontSize)>parseFloat(start_zoomSizeO)) { ele.style.fontSize = Math.floor(parseFloat(ele.style.fontSize)-font_smoothing)+"px"; } else { ele.style.fontSize = start_zoomSizeO; window.clearInterval(zoomout); } }
  55. function eColors(eleid) { var ele = document.getElementById(eleid); R = calcHex(Math.floor(Math.random()*15))+''+calcHex(Math.floor(Math.random()*15)); G = calcHex(Math.floor(Math.random()*15))+''+calcHex(Math.floor(Math.random()*15)); B = calcHex(Math.floor(Math.random()*15))+''+calcHex(Math.floor(Math.random()*15)); ele.style.color = "#"+R+G+B; }
  56. </script>
  57. <!--<div name="txtEffect" effect="slideleft" id="id4" style="position: absolute; top: 50px; left: 500px;"><font color="red">This text should slide in from the right</font></div>
  58. <div name="txtEffect" effect="slideleft" id="id5" style="position: absolute; top: 150px; left: 500px;"><font color="blue">This text should slide in from the right</font></div>
  59. <div name="txtEffect" effect="slideleft" id="id6" style="position: absolute; top: 250px; left: 500px;"><font color="green">This text should slide in from the right</font></div>
  60. <div name="txtEffect" effect="slideleft" id="id7" style="position: absolute; top: 350px; left: 500px;"><font color="orange">This text should slide in from the right</font></div>
  61. <div name="txtEffect" effect="slideright" id="id8" style="position: absolute; top: 100px; left: 500px;"><font color="red">This text should slide in from the left</font></div>
  62. <div name="txtEffect" effect="slideright" id="id9" style="position: absolute; top: 200px; left: 500px;"><font color="blue">This text should slide in from the left</font></div>
  63. <div name="txtEffect" effect="slideright" id="id10" style="position: absolute; top: 300px; left: 500px;"><font color="green">This text should slide in from the left</font></div>
  64. <div name="txtEffect" effect="slideright" id="id11" style="position: absolute; top: 400px; left: 500px;"><font color="orange">This text should slide in from the left</font></div>-->
  65. <div name="txtEffect" effect="slidedown,colors" id="id1" style="position: absolute; top: 300px; left: 0px;">This text should slide down from the top and change colors</div>
  66. <div name="txtEffect" effect="slideright" id="id3" style="position: absolute; top: 300px; left: 360px;"><font color="red">This text should slide in from the left</div>
  67. <div name="txtEffect" effect="zoomout,slideleft" id="id4" style="position: absolute; top: 300px; left: 600px; font-size: 20px;"><font color="green">This text should slide in from the right and zoom out</div>
  68. <div name="txtEffect" effect="zoomin,slideup,colors" id="id5" style="position: absolute; top: 500px; left: 300px; font-size: 40px;">This text should Zoom in to size (zoom in) and slide up and change colors</div>
  69.  



volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#2   Jul 1 '07

re: Text Effects


I like and don't like how it's given it's own attribute. I like the concept, but I don't like adding invalid markup. It does look like it works nicely though.

Since this is creating animation effects, I'd add a little extra optimization in the series of ifs by making them else ifs to avoid checking them all. Other than that, this script is solid. Very nice work. :D
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3   Jul 3 '07

re: Text Effects


To avoid invalid markup, you could use the class attribute instead.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#4   Jul 3 '07

re: Text Effects


hmmm ... the old 'philosophical' question ;) : custom attribute vs. class ...

i think it is really a question that is up to the 'taste' of the developer with advantages and disadvantages on both sides ... using a custom attribute makes the html invalid but describes what you really want to do ... to add an attribute that holds a value you need ... it's the straight way and from my point of view it is semantically correct until everybody who reads that will know what you want to do ... but: it makes the markup invalid! using the class-attribute avoids that problem, but you misuse it and you loose the semantic ... an attr-name like 'effect' shows what you want to have there ... class is always class ... so: there are the two possibilities and, i know all xhtml-evangelists will hate me for that ;), i would always prefer the custom-attribute way ...

kind regards
volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#5   Jul 4 '07

re: Text Effects


I'm writing a class that handle effects, and I chose to go with classes, requiring that the classes corresponding to events have a special class in their class attribute. However, when you put it into perspective, a custom attribute does seem like a good idea. I'll need to start a debate somewhere regarding this and get some opinions.
Reply


Similar JavaScript / Ajax / DHTML bytes