| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| DesignersTalk > Changing background image of a container using multiple links |
|
LinkBack | Thread Tools | Search this Thread |
|
|
#1 (permalink) |
|
Baskin'
Join Date: Feb 2005
Posts: 5,619
|
Changing background image of a container using multiple links
Can this be done with pure CSS? Snippet: <div id="moo"> <ul> <li><a href="#">image-01</a></li> <li><a href="#">image-02</a></li> <li><a href="#">image-03</a></li> </ul> </div> So when I click the link the background image of ID moo changes image... Ta, Limbo |
|
|
|
|
|
#6 (permalink) |
|
Senior Member
Join Date: Jan 2005
Posts: 12,340
|
Perhaps you could do something similar to: http://www.alistapart.com/articles/imagegallery/ Sorry, I'm a JS retard. |
|
|
|
#7 (permalink) |
|
Baskin'
Join Date: Feb 2005
Posts: 5,619
|
Nope - that wont do it have loads of scripts like that This one HAS to change the background property - the div contains text so dont want to have to play with additional floats & divs as well - just one lot of padding. It's an odd one - not much out there about it. |
|
|
|
#8 (permalink) |
|
Baskin'
Join Date: Feb 2005
Posts: 5,619
|
So I've got this far using sliding doors - seems to work in IE & FF: <html> <head> <style type='text/css'> .div {width:300px;height:300px;background:#eee;} .div a:link, .div a:visited, .div a:active { background:url(images/test/test-rollover-2.gif); width:300px;height:300px; display:block; } .div a:hover { background:url(images/test/test-rollover-1.gif); width:300px;height:300px; display:block; } </style> </head> <body> <div class="div"><a href="#">rollover</a></div> </body> </html> But I want to trigger the rollover when I hover the link - not the div... Any suggestions would be hugely appreiciated - need to get this sown up tonight. Cheers, Limbo |
|
|
|
#9 (permalink) |
|
vague™
Join Date: Mar 2004
Location: Glasgow
Posts: 5,512
|
aye, you're changing the bg on the anchor though. your original block of markup (anchors *inside* a div element changing the background of the div) can only be done with JS as you can't write the CSS rule to catch the hover (as the background element contains the anchors). far too late in the day to be asking this kinda thing to be honest. sorry chief, pub is calling |
|
|
|
#10 (permalink) |
|
Everything is fine.
|
Here you go. A fully working XHTML Strict background image swapper thingy...this will degrade gracefully if the user doesn't have JavaScript enabled, and also tests for browser object/method compatibility. I should also point out that if you change the ID of the div in the html page, then you'll also need to change the targetDiv name in the JavaScript on the line that reads: var targetDiv = document.getElementById("moo"); Hope that helps !! - Mike |
|
|
|
#12 (permalink) | |
|
Everything is fine.
|
Quote:
By the way, in your original post you wanted the background image to change when the link is clicked, then in a later post you said you want it triggered when the link is hovered over. I have set it to work on the click of the link, if you want it to work on the hover instead then let me know. - Mike |
|
|
|
|
#13 (permalink) | |
|
Baskin'
Join Date: Feb 2005
Posts: 5,619
|
Quote:
Cheers Mike Works perfect in a browser with mozilla core (netscape, firefox, flock...) But falls over in IE and there's a 1 click delay in opera (background displays white) Typical of IE to make things more difficult than neccessary. |
|
|
|
|
#15 (permalink) | |
|
Everything is fine.
|
Quote:
I'm not too sure about the Opera thing though, I'll have a look in to that. - Mike |
|
|
|
|
#17 (permalink) | |
|
Everything is fine.
|
Quote:
http://www.phdcc.com/xpsp2.htm With regards to the white background issue, I believe this could be due to loading the image when it's been clicked on. There will be a slight delay while the image is loaded frome the site in to the browser which is triggering the temporary white background issue. After the first time it's been loaded it should show from the cache and change immediately. One option to combat this would be to have the JavaScript pre-load the images in to the browser. - Mike |
|
|
|
|
#19 (permalink) |
|
Baskin'
Join Date: Feb 2005
Posts: 5,619
|
Hi Mike - Last quesiton. There is more than one div that I'd like to have this action work on. The page looks a little like this: Code:
The alternative is to have the page work like this: Code:
|
|
|
|
#20 (permalink) |
|
Everything is fine.
|
Ok, I've rewritten the code using an array to allow for an unlimited amount of divs that you would like to change the background image with. You'll need to manually add (and change) the div names in the JavaScript code so that it knows which to pick out and prepare: var contentDivs = Array("moo","baa"); The way it's been written is to keep the <a href> tags inside the div as in your original example. The JavaScript will traverse the node tree to target the necessary div for the image swapping: <div id="moo"> <ul> <li><a href="redBg.gif">Red</a></li> <li><a href="yellowBg.gif">Yellow</a></li> <li><a href="blueBg.gif">Blue</a></li> </ul> </div> I've tested it out in FF & IE and all seems to work ok. Let me know if you have any issues with it. - Mike |
|
![]() |