Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Show Div on Hover

Featured Replies

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!

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

  • 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 :good:

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}

  • 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!

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* :friends:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.