iam_clint 1,208
Recognized Expert Top Contributor -
/*###############################*/
-
/* Link fade script by iam_clint, bytes.com */
-
/* www.bytes.com, If you use this script on your site */
-
/* Please keep this comment at the top of the javascript */
-
/*###############################*/
-
-
window.onload=function() { setupLinks(); }
-
-
var red=0; /*base 16*/
-
var green=15; /*15 because I want the faded in color to be super green*/
-
var blue=0; /*no blue*/
-
var fade_in_speed=20;
-
var fade_out_speed=200;
-
var start_color="#000000";
-
var divider;
-
-
function cHex(dec) {
-
var hex=dec.toString(16); /*Converts decimal to hax*/
-
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*/
-
return hex;
-
}
-
-
function rgb(r, g, b) {
-
var str="#"; /*Hex Colors Start With #*/
-
str+=cHex(r); /*Red*/
-
str+=cHex(g); /*Green*/
-
str+=cHex(b); /*Blue*/
-
return str;
-
}
-
-
function hoverObj(obj, fspeedin, fspeedout) {
-
/*Set instance specific variables*/
-
this.id = hoverObj.Instances.length; /*Gets the array length*/
-
hoverObj.Instances[this.id] = this; /*sets this object to this instance so it can be referenced to like an array*/
-
this.cObj=obj; /*This sets this instance's object to what was passed.. this could be used for other tags other than links*/
-
this.current=0; /*This just starts the current color off at 0*/
-
this.speedIn=fspeedin; /*This is the speed at which the color fades in at*/
-
this.speedOut=fspeedout; /*This is the speed at which the color fades iout at*/
-
this.interval; /*This is a variable being used to store the interval id*/
-
this.direction; /*This tells the changeColor function to add or subtract to current*/
-
this.hoverLink=hoverLink; /*Sets a reference to the function hoverLink from within this instance*/
-
this.changeColor=changeColor;
-
this.isOver=false;
-
}
-
hoverObj.Instances = new Array(); /*Sets Instances in hoverObj to an array*/
-
-
function changeColor() {
-
if (this.direction==1) { /*I used 1 for increasing current*/
-
if (this.current<15) { this.current+=1;} else {
-
if (!this.isOver) {
-
window.clearInterval(this.interval); /*clears the interval*/
-
this.direction=0;
-
this.interval=window.setInterval("hoverObj.Instances[" + this.id + "].changeColor()", this.speedOut);
-
return false;
-
}
-
}
-
if (this.current>15) { this.current=15; } /*if its above 15 set it to 15 so the hex value with be FF*/
-
} else {
-
if (this.current>0) { this.current-=1; } else { window.clearInterval(this.interval); this.cObj.style.color=start_color; return false; } /*fade out*/
-
}
-
var calc_red=Math.round(this.current*(red/15));
-
var calc_green=Math.round(this.current*(green/15));
-
var calc_blue=Math.round(this.current*(blue/15));
-
this.cObj.style.color=rgb(calc_red, calc_green, calc_blue); /*actually set the color*/
-
}
-
-
function hoverLink(direction) {
-
if (direction==1) { this.isOver=true; this.direction=1 } else { this.isOver=false; }
-
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*/ }
-
}
-
-
function setupLinks() {
-
var links=document.getElementsByTagName("A"); /*Get all the elements that are <A />*/
-
for (var i=0; i<links.length; i++) {
-
links[i].setAttribute("i", i);
-
links[i].onmouseover=function() {
-
if (this.getAttribute("hoverID")=="" || this.getAttribute("hoverID")==null) {
-
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*/
-
this.setAttribute("hoverID", lnk.id);
-
}
-
var link=hoverObj.Instances[this.getAttribute("hoverID")];
-
link.hoverLink(1);
-
}
-
links[i].onmouseout=function() {
-
var link=hoverObj.Instances[this.getAttribute("hoverID")]; /*refers to the instance given in onmouseover*/
-
link.hoverLink(0);
-
}
-
links[i].style.color=start_color;
-
}
-
}
-
In this example you can see i am using the word new. -
var lnk=new hoverObj(this, fade_in_speed, fade_out_speed);
-
This is creating an instance of the hoverObj -
function hoverObj(obj, fspeedin, fspeedout) {
-
/*Set instance specific variables*/
-
this.id = hoverObj.Instances.length; /*Gets the array length*/
-
hoverObj.Instances[this.id] = this; /*sets this object to this instance so it can be referenced to like an array*/
-
this.cObj=obj; /*This sets this instance's object to what was passed.. this could be used for other tags other than links*/
-
this.current=0; /*This just starts the current color off at 0*/
-
this.speedIn=fspeedin; /*This is the speed at which the color fades in at*/
-
this.speedOut=fspeedout; /*This is the speed at which the color fades iout at*/
-
this.interval; /*This is a variable being used to store the interval id*/
-
this.direction; /*This tells the changeColor function to add or subtract to current*/
-
this.hoverLink=hoverLink; /*Sets a reference to the function hoverLink from within this instance*/
-
this.changeColor=changeColor;
-
this.isOver=false;
-
}
-
hoverObj.Instances = new Array(); /*Sets Instances in hoverObj to an array*/
-
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.
2 3142 Dormilich 8,658
Recognized Expert Moderator Expert
I'd change line 7 to - window.onload = setupLinks;
if you haven't already declared onload events. otherwise add setupLinks() to your load chain.
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:
but since we don't pass any param in this case we could even use:
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: -
var o = new Object;
-
// vs.
-
var o = {};
-
// or
-
var a = new Array;
-
// vs.
-
var a = [];
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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...
|
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 =)
...
|
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
| |
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.
|
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)
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |