Changing class on hover and keeping it
09-23-09, 06:22 AM
Newbie Coder
Join Date: Feb 2009
Posts: 78
Thanks: 2
Thanked 0 Times in 0 Posts
Changing class on hover and keeping it
Hi guy not the greatest with javascript i use it now and then.
so far i have this code working correctly. Currently on mouse-over its changes the img background of imgholder.
this is the HTML
This is the script
currenly if you hover over any of the <a> a css class is applied
css code
what i need to do is keep this class applied over the last <a> that is hovered.
so what i would like to do is change the class of the last <a> hover so it stays active until another one is hovered.
does this make sense? any help or guidance would be appreciated
09-23-09, 06:29 AM
-
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
It looks like you're preloading the images on mouseover, and that a:hover is updating the display.
To change the image on mouseover, I'd use something like:
Not tested.
The Following User Says Thank You to wirehopper For This Useful Post:
09-23-09, 06:35 AM
Newbie Coder
Join Date: Feb 2009
Posts: 78
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you for the help so far.
But that really not what im after.
I should of posted this link before to help explain
SureCloud ? Vulnerability Management and IT GRC Solutions
if you hover over the buttons at the top they turn from red to blue on hover, and also changes the big image.
when you move of the button they go back to red, I would like to be able to keep the last button that the user was on to be blue? possible
09-23-09, 12:08 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Get rid of the hover parts of your CSS and let's use Javascript to change the CSS instead of trying to figure out how to set it up using CSS classes.
I have defined two arrays that contain all your images.
The first array, image_url(), contains the images you want displayed in your anchor elements.
The second array, animated_image(), contains the images you want to display when you hover over an anchor image.
Change these image names as needed.
In the function changeImg(num,imgName), the variable gif_image contains the background formatting such as 'no-repeat scroll -148px -59px'.
Change these as necessary.
The style section, I added, just to give the anchor elements some dimensions.
You may remove it and use your own style.
HTML Code:
<html>
<head>
<script>
var URL_prefix = 'http://www.website.co.uk/surecloud';
// set image url
var image_url = new Array(
URL_prefix+"images/img1.jpg",
URL_prefix+"images/img2.jpg",
URL_prefix+"images/img3.jpg",
URL_prefix+"images/img4.jpg",
URL_prefix+"images/img5.jpg",
URL_prefix+"images/img6.jpg"
);
var animated_image = new Array(
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif"
);
var obj;
function pre_load_images()
{
obj = document.getElementById("anchor_container").document.getElementsByTagName("a");
for(var i=0;i<obj.length;i++){obj[i].style.background="transparent url('"+image_url[i]+"') no-repeat";}
}
function changeImg(num,imgName)
{
pre_load_images();
var gif_image;
if(num==0){gif_image = "transparent url('"+animated_image[0]+"') no-repeat scroll -148px -59px";}
if(num==1){gif_image = "transparent url('"+animated_image[1]+"') no-repeat scroll -148px -59px";}
if(num==2){gif_image = "transparent url('"+animated_image[2]+"') no-repeat scroll -148px -59px";}
if(num==3){gif_image = "transparent url('"+animated_image[3]+"') no-repeat scroll -148px -59px";}
if(num==4){gif_image = "transparent url('"+animated_image[4]+"') no-repeat scroll -148px -59px";}
if(num==5){gif_image = "transparent url('"+animated_image[5]+"') no-repeat scroll -148px -59px";}
obj[num].style.mozBackgroundClip="border";
obj[num].style.mozBackgroundInlinePolicy="continuous";
obj[num].style.mozBackgroundOrigin="padding";
obj[num].style.background=gif_image;
document.getElementById('imgholder').style.background="transparent url('"+imgName+"') no-repeat";
}
</script>
<style>
.vm{width:50px;height:100px;}
.grc{width:50px;height:100px;}
.pci{width:50px;height:100px;}
.gcs{width:50px;height:100px;}
.pt{width:50px;height:100px;}
.as{width:50px;height:100px;}
</style>
</head>
<body onload="pre_load_images();" >
<div id="imgholder" style="height:400px;" > </div>
<div id="myController2" >
<span class="jFlowControl2" > No 1 </span>
<span class="jFlowControl2" > No 2 </span>
</div>
<div id="mySlides2" >
<div id="anchor_container" >
<a onmouseover="changeImg(0,image_url[0]);" class="vm" href="vulnerabiliy.htm" title="Vulnerability Management" > </a>
<a onmouseover="changeImg(1,image_url[1]);" class="grc" href="it_grc.htm" title="IT Governance, Risk and Compliance" > </a>
<a onmouseover="changeImg(2,image_url[2]);" class="pci" href="pci.htm" title="Payment Card Industry Data Security Standard" > </a>
<a onmouseover="changeImg(3,image_url[3]);" class="gcs" href="gcs.htm" title="Government Connect Code of Connection" > </a>
<a onmouseover="changeImg(4,image_url[4]);" class="pt" href="penetration.htm" title="Penetration Testing" > </a>
<a onmouseover="changeImg(5,image_url[5]);" class="as" href="application.htm" title="Application Security" > </a>
<!--span class="jFlowNext2 NextFlash" > </span-->
</div>
<div>
<!--span class="jFlowPrev2 BackFlash" > </span-->
</div>
</div>
</body>
</html>
__________________
Jerry Broughton
The Following User Says Thank You to job0107 For This Useful Post:
09-24-09, 03:38 AM
Newbie Coder
Join Date: Feb 2009
Posts: 78
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you for alll the help so far...
I have tried this code and i think i have amended what is needed, but does not seem to work.
I have debugged with my limted skills.
var image_url seems to work
["http://www.cleverdevelopment.co.uk/surecloudimages/img1.jpg", "http://www.cleverdevelopment.co.uk/surecloudimages/img2.jpg", "http://www.cleverdevelopment.co.uk/surecloudimages/img3.jpg",
var animated_image seems to work
["../../images/animated_nav.gif", "../../images/animated_nav.gif", "../../images/animated_nav.gif", 3 more.
its never seems to get past
function pre_load_images()
code is up and running with debugger; code in if you would like to see
http://www.cleverdevelopment.co.uk/surecloud/
09-24-09, 06:51 AM
-
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
This is a different approach - instead of using the background image, it uses the image tag to display the image. If you want to preload the images, you could just use a hidden div.
09-25-09, 06:47 AM
Newbie Coder
Join Date: Feb 2009
Posts: 78
Thanks: 2
Thanked 0 Times in 0 Posts
09-25-09, 09:39 AM
-
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
09-25-09, 09:46 AM
Newbie Coder
Join Date: Feb 2009
Posts: 78
Thanks: 2
Thanked 0 Times in 0 Posts
HI Thanks again for all this, im going to try and get this sorted this weekend..
the javascript
Code:
var URL_prefix = 'http://www.cleverdevelopment.co.uk/surecloud';
// set image url
var image_url = new Array(
URL_prefix+"images/img1.jpg",
URL_prefix+"images/img2.jpg",
URL_prefix+"images/img3.jpg",
URL_prefix+"images/img4.jpg",
URL_prefix+"images/img5.jpg",
URL_prefix+"images/img6.jpg"
);
var animated_image = new Array(
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif"
);
var obj;
function pre_load_images()
{ debugger;
obj = document.getElementById("anchor_container").document.getElementsByTagName("a");
for(var i=0;i<obj.length;i++)
{obj[i].style.background="transparent url('"+image_url[i]+"') no-repeat";}
}
function changeImg(num,imgName)
{ debugger;
pre_load_images();
var gif_image;
if(num==0){gif_image = "transparent url('"+animated_image[0]+"') no-repeat scroll -148px -59px";}
if(num==1){gif_image = "transparent url('"+animated_image[1]+"') no-repeat scroll -148px -59px";}
if(num==2){gif_image = "transparent url('"+animated_image[2]+"') no-repeat scroll -148px -59px";}
if(num==3){gif_image = "transparent url('"+animated_image[3]+"') no-repeat scroll -148px -59px";}
if(num==4){gif_image = "transparent url('"+animated_image[4]+"') no-repeat scroll -148px -59px";}
if(num==5){gif_image = "transparent url('"+animated_image[5]+"') no-repeat scroll -148px -59px";}
obj[num].style.mozBackgroundClip="border";
obj[num].style.mozBackgroundInlinePolicy="continuous";
obj[num].style.mozBackgroundOrigin="padding";
obj[num].style.background=gif_image;
document.getElementById('imgholder').style.background="transparent url('"+imgName+"') no-repeat";
}
The HTML
09-26-09, 10:58 AM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by
RichyRich38
HI Thanks again for all this, im going to try and get this sorted this weekend..
the javascript
Code:
var URL_prefix = 'http://www.cleverdevelopment.co.uk/surecloud';
// set image url
var image_url = new Array(
URL_prefix+"images/img1.jpg",
URL_prefix+"images/img2.jpg",
URL_prefix+"images/img3.jpg",
URL_prefix+"images/img4.jpg",
URL_prefix+"images/img5.jpg",
URL_prefix+"images/img6.jpg"
);
var animated_image = new Array(
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif",
"../../images/animated_nav.gif"
);
var obj;
function pre_load_images()
{ debugger;
obj = document.getElementById("anchor_container").document.getElementsByTagName("a");
for(var i=0;i<obj.length;i++)
{obj[i].style.background="transparent url('"+image_url[i]+"') no-repeat";}
}
function changeImg(num,imgName)
{ debugger;
pre_load_images();
var gif_image;
if(num==0){gif_image = "transparent url('"+animated_image[0]+"') no-repeat scroll -148px -59px";}
if(num==1){gif_image = "transparent url('"+animated_image[1]+"') no-repeat scroll -148px -59px";}
if(num==2){gif_image = "transparent url('"+animated_image[2]+"') no-repeat scroll -148px -59px";}
if(num==3){gif_image = "transparent url('"+animated_image[3]+"') no-repeat scroll -148px -59px";}
if(num==4){gif_image = "transparent url('"+animated_image[4]+"') no-repeat scroll -148px -59px";}
if(num==5){gif_image = "transparent url('"+animated_image[5]+"') no-repeat scroll -148px -59px";}
obj[num].style.mozBackgroundClip="border";
obj[num].style.mozBackgroundInlinePolicy="continuous";
obj[num].style.mozBackgroundOrigin="padding";
obj[num].style.background=gif_image;
document.getElementById('imgholder').style.background="transparent url('"+imgName+"') no-repeat";
}
The HTML
This Javascript function ( function pre_load_images() ) needs to go into the body elements onload event listener.
Or it needs to be run as the last command in the html.
__________________
Jerry Broughton
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off