July 18, 201115 yr Hi Guys, I've got two images I've give Div ID's to and layered them on top of each other. I've managed to use a code to hide the top DIV and show it on hover, here's the code <script> function show(id) { document.getElementById(id).style.visibility = "visible"; } function hide(id) { document.getElementById(id).style.visibility = "hidden"; } </script> <div onMouseOver="show('lit')" onMouseOut="hide('lit')"> Here's the stylesheet I use also #unlit { position:absolute; left:0px; top:0px; z-index:-1 } #lit {visibility: hidden; position:absolute; left:0px; top:0px; z-index:-1} And the HTML <div id ="unlit"> <img src = "Bulbunlit.gif"> </div> <div id='lit'> <img src = "Bulblit.gif"> </div> Now this works fine, but I need to make it so it runs of an external JavaScript file but this code doesnt seem to run like that. Can anyone help me make it JavaScript compatible so I can put it on my CMS, Thanks!
July 19, 201115 yr Hi Guys, I've got two images I've give Div ID's to and layered them on top of each other. I've managed to use a code to hide the top DIV and show it on hover, here's the code <script> function show(id) { document.getElementById(id).style.visibility = "visible"; } function hide(id) { document.getElementById(id).style.visibility = "hidden"; } </script> <div onMouseOver="show('lit')" onMouseOut="hide('lit')"> Here's the stylesheet I use also #unlit { position:absolute; left:0px; top:0px; z-index:-1 } #lit {visibility: hidden; position:absolute; left:0px; top:0px; z-index:-1} And the HTML <div id ="unlit"> <img src = "Bulbunlit.gif"> </div> <div id='lit'> <img src = "Bulblit.gif"> </div> Now this works fine, but I need to make it so it runs of an external JavaScript file but this code doesnt seem to run like that. Can anyone help me make it JavaScript compatible so I can put it on my CMS, Thanks! Have you thought about using jQuery? You could achieve what you're trying to do there in fewer lines of code. From the source code for your images, I'm assuming you have a picture of a lightbulb that lights up when hovered over? It's a much better syntax too. For the purpose of what you're doing here, you could use .hide and .show Or, failing that, you could also achieve this by using CSS sprites and no JavaScript. Here's a good tutorial for that: CSS Sprites
July 19, 201115 yr Author Thanks for the reply. Yeah I basically have two pictures of bulbs on top of each other and the lit one is hidden until I hover. I'll look into using JQuery if it makes things simpler
July 19, 201115 yr even simpler and no need for a jQuery library http://www.dynamicdrive.com/dynamicindex15/domroll.htm also note that things like iPads don't do Hover.
July 19, 201115 yr Instead of using js to swap the image, why don't you take the image out of your html and use CSS to apply it as a background instead? Then you can just use the :hover attribute to swap them over. Even better, if you create a single image with both states shown, you can just use CSS to move the position of the image to create the same effect. #box {styles for unlit state} #box:hover {styles for lit state}
July 19, 201115 yr Author I wasn't using CSS and couldn't use my first method because the CMS I was using wouldn't run it for some odd reason. I've used JQuery and managed to get it working perfectly. Thanks for all your help guys!
July 19, 201115 yr I wasn't using CSS and couldn't use my first method because the CMS I was using wouldn't run it for some odd reason. I've used JQuery and managed to get it working perfectly. Thanks for all your help guys! *Taps +1 button*
Create an account or sign in to comment