473,800 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Orientation [Links Fader Example]

iam_clint
1,208 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. /*###############################*/
  2. /*     Link fade script by iam_clint, bytes.com                  */
  3. /*     www.bytes.com, If you use this script on your site       */
  4. /*    Please keep this comment at the top of the javascript  */
  5. /*###############################*/
  6.  
  7. window.onload=function() { setupLinks(); }
  8.  
  9. var red=0; /*base 16*/
  10. var green=15; /*15 because I want the faded in color to be super green*/
  11. var blue=0; /*no blue*/
  12. var fade_in_speed=20;
  13. var fade_out_speed=200;
  14. var start_color="#000000";
  15. var divider;
  16.  
  17. function cHex(dec) { 
  18.     var hex=dec.toString(16); /*Converts decimal to hax*/
  19.     if (hex.length==1) { hex+=""+hex; } /*This just makes sure that the return  is atleast 2 digits, so if the hax is actually F it is going to return FF*/
  20.     return hex;
  21. }
  22.  
  23. function rgb(r, g, b) {
  24.     var str="#";  /*Hex Colors Start With #*/
  25.     str+=cHex(r); /*Red*/
  26.     str+=cHex(g); /*Green*/
  27.     str+=cHex(b); /*Blue*/
  28.     return str;
  29. }
  30.  
  31. function hoverObj(obj, fspeedin, fspeedout) {
  32.     /*Set instance specific variables*/
  33.     this.id = hoverObj.Instances.length; /*Gets the array length*/
  34.     hoverObj.Instances[this.id] = this; /*sets this object to  this instance so it can be referenced to like an array*/
  35.     this.cObj=obj; /*This sets this instance's object to what was passed.. this could be used for other tags other than links*/
  36.     this.current=0; /*This just starts the current color off at 0*/
  37.     this.speedIn=fspeedin; /*This is the speed at which the color fades in at*/
  38.     this.speedOut=fspeedout; /*This is the speed at which the color fades iout at*/
  39.     this.interval; /*This is a variable being used to store the interval id*/
  40.     this.direction; /*This tells the changeColor function to add or subtract to current*/
  41.     this.hoverLink=hoverLink; /*Sets a reference to the function hoverLink from within this instance*/
  42.     this.changeColor=changeColor;
  43.     this.isOver=false;
  44. }
  45. hoverObj.Instances = new Array(); /*Sets Instances in hoverObj to an array*/
  46.  
  47. function changeColor() {
  48.     if (this.direction==1) {    /*I used 1 for increasing current*/
  49.         if (this.current<15) { this.current+=1;} else {
  50.             if (!this.isOver) {
  51.                 window.clearInterval(this.interval); /*clears the interval*/    
  52.                 this.direction=0;
  53.                 this.interval=window.setInterval("hoverObj.Instances[" + this.id + "].changeColor()", this.speedOut); 
  54.                 return false; 
  55.             }
  56.         }
  57.         if (this.current>15) { this.current=15; } /*if its above 15 set it to 15 so the hex value with be FF*/
  58.     } else { 
  59.         if (this.current>0) { this.current-=1; } else { window.clearInterval(this.interval); this.cObj.style.color=start_color; return false; } /*fade out*/
  60.     }
  61.     var calc_red=Math.round(this.current*(red/15));
  62.     var calc_green=Math.round(this.current*(green/15));
  63.     var calc_blue=Math.round(this.current*(blue/15));
  64.     this.cObj.style.color=rgb(calc_red, calc_green, calc_blue); /*actually set the color*/
  65. }
  66.  
  67. function hoverLink(direction) {
  68.     if (direction==1) { this.isOver=true; this.direction=1 } else { this.isOver=false; }
  69.     if (this.direction==1 && this.isOver) { this.interval=window.setInterval("hoverObj.Instances[" + this.id + "].changeColor()", this.speedIn); /*sets this instance of the interval( to start fading in*/    }
  70. }
  71.  
  72. function setupLinks() {
  73.     var links=document.getElementsByTagName("A"); /*Get all the elements that are <A />*/
  74.     for (var i=0; i<links.length; i++) {
  75.         links[i].setAttribute("i", i);
  76.         links[i].onmouseover=function() { 
  77.             if (this.getAttribute("hoverID")=="" || this.getAttribute("hoverID")==null) {
  78.                 var lnk=new hoverObj(this, fade_in_speed, fade_out_speed); /*this important because for each link this loop finds it gives it its own instance of hoverObj*/
  79.                 this.setAttribute("hoverID", lnk.id);
  80.             }
  81.             var link=hoverObj.Instances[this.getAttribute("hoverID")];
  82.             link.hoverLink(1); 
  83.         }
  84.         links[i].onmouseout=function() { 
  85.                 var link=hoverObj.Instances[this.getAttribute("hoverID")]; /*refers to the instance given in onmouseover*/
  86.                 link.hoverLink(0); 
  87.         }
  88.         links[i].style.color=start_color;
  89.     }
  90. }
  91.  
In this example you can see i am using the word new.
Expand|Select|Wrap|Line Numbers
  1. var lnk=new hoverObj(this, fade_in_speed, fade_out_speed); 
  2.  
This is creating an instance of the hoverObj
Expand|Select|Wrap|Line Numbers
  1. function hoverObj(obj, fspeedin, fspeedout) {
  2.     /*Set instance specific variables*/
  3.     this.id = hoverObj.Instances.length; /*Gets the array length*/
  4.     hoverObj.Instances[this.id] = this; /*sets this object to  this instance so it can be referenced to like an array*/
  5.     this.cObj=obj; /*This sets this instance's object to what was passed.. this could be used for other tags other than links*/
  6.     this.current=0; /*This just starts the current color off at 0*/
  7.     this.speedIn=fspeedin; /*This is the speed at which the color fades in at*/
  8.     this.speedOut=fspeedout; /*This is the speed at which the color fades iout at*/
  9.     this.interval; /*This is a variable being used to store the interval id*/
  10.     this.direction; /*This tells the changeColor function to add or subtract to current*/
  11.     this.hoverLink=hoverLink; /*Sets a reference to the function hoverLink from within this instance*/
  12.     this.changeColor=changeColor;
  13.     this.isOver=false;
  14. }
  15. hoverObj.Instances = new Array(); /*Sets Instances in hoverObj to an array*/
  16.  
You may ask yourself, why do I need more than 1 instance of this hoverObj.
The reason is so each link can act independently. So while one link is fading in another link can be fading out this keeps things running smoothly, Each link gets its own "timer" rather than sharing 1.
Nov 19 '08 #1
2 3142
Dormilich
8,658 Recognized Expert Moderator Expert
I'd change line 7 to
Expand|Select|Wrap|Line Numbers
  1. window.onload = setupLinks;
if you haven't already declared onload events. otherwise add setupLinks() to your load chain.
Dec 16 '08 #2
gits
5,390 Recognized Expert Moderator Expert
just a note for small 'mini-improvements': when we want to create an instance of any object we typically use:

Expand|Select|Wrap|Line Numbers
  1. var o = new OBJ();
  2.  
but since we don't pass any param in this case we could even use:

Expand|Select|Wrap|Line Numbers
  1. var o = new OBJ;
  2.  
so JavaScript don't need to eval an empty args-list this leads to better performance. in case we use built in constructors then we have a much better performance when using the literals:

Expand|Select|Wrap|Line Numbers
  1. var o = new Object;
  2. // vs.
  3. var o = {};
  4. // or
  5. var a = new Array;
  6. // vs.
  7. var a = [];
  8.  
the literals are less characters to be parsed and so that could be evaled faster. so from the point of performance it is to prefer to use the literals and leave out the brackets when possible when creating instances of objects.

kind regards
gits
Dec 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2611
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
2
1920
by: Nick | last post by:
Does a background fader work by... A) Changing the color after a long 'loop' statement??? B) Changing the color, then waiting a bit, then changing the color again??? To me it seems B would be less cpu/browser intensive. Does anyone know
9
4814
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
4
1855
by: anatushi | last post by:
Hi, im looking for help on how to add a text fader to my website, usually i'm working with css but in this case i need to add java script to make it work, that wasn't the problem but somehow it seems like they are not compatible with each other so when i add the text fader code in the <body> tag,the rest of my site is gone :S if someone please can have a look at my code and tell what am i doing wrong. thx allot for whom of you who can =) ...
11
2300
by: Vishal Naidu | last post by:
i m a college student in my second year..... my queston is.. is it really possible to write object oriented code in C ? and if yes how do we achieve abstration, polymorhism , hierarchy etc. in C
0
1476
by: Mamatha | last post by:
Hi I am developing a small video capture application in C#.net. I want to develop this application like "winamp",means i want to display VUlevel meters and FADER. I want controls in .net for VUlevel meters and FADER. If you have any idea about source code for this userdefined control or any related URLs ,please let me know. Please give me solution,thanks in advance.
14
2020
by: James | last post by:
I am trying to a add a method to a helper class library I built that will fade out the current form. My code is this: public void fade(object currentForm) { int z = 0; for(double i=1.0; i> 0; i-=.1) { currentForm.Opacity = i; while(z<10000)
2
31778
by: tridirk | last post by:
Hi; I am getting a Objceted Expected Error on my forum site. I can't find what is wrong? Line: Char: Error: Object expected Code:0 the site is My SMF Forum
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.