Fun with Javascript Hovers and CSS Hovers

AlisonRobin's picture
I was playing with hovers in javascript and have found that they are superior to CSS hovers in a kind of fun way so I thought I'd share.

Say you have a div that changes when you hover over it, like this one:


Here's the code for that that goes where you want the div to appear:
<div id="test1"></div>

And here's the code for that which you will need to have in your style tag:
#test1{width:100px;height:100px;border-style:solid;border-width:1px;background:transparent;} #test1:hover{background:#ff0000;}

You see that there are two ways that it can appear. There's how it is when you hover, and how it is when you don't hover.

This next div uses Javascript to change:


So with Javascript, there are three ways. Before the hover, during the hover, and after the hover, that it can appear. Click this to reset that div and play with it again.

And here's the code:
<div id="test2" style="width:100px;height:100px;border-style:solid;border-width:1px;background:transparent" onmouseover="document.getElementById('test2').style.background='#ff0000';" onmouseout="document.getElementById('test2').style.background='#0000ff';"></div>

And the code that I used to reset it:
<a href="javascript:;" onmousedown="document.getElementById('test2').style.background='transparent';">Reset</a>

And I'll do my best to explain how it works. You can see that there's this sequence: onmouseover="document.getElementById('test2').style.background='#ff0000';" in the code and I'll use it as an example.

onmouseover is an event.
document is the entire webpage.
getElementById is a method.
test2 is the id of the element (in this case, the div) that I want to get.
style is an attribute of the div test2 element.
background is a property of the style attribute.
##ff0000 is a value of the background property

So in the end it reads something like, "when this is moused over, search the document for the element with the Id of "test2" and then find its style attribute and look in there to change the background to #ff0000."

So, you can take this code and change a lot of it out however you want to change different things. You could change things like the size of the div when it's moused over so it grows and shrinks, you can change the border size and pattern, you could make things disappear (but I don't know how you would make it reappear), you could change the font of text when you mouse over it. But a lot of that stuff you can also do with CSS, so I dunno. Be creative you wonderful people! I'm too much of a turd to come up with anything good but if you have an idea you want I'll do my best to help you make it happen.

So far I've mostly been experimenting with using hovering and clicking to change things inside style attributes because a lot of Javascript is about math and functions that I think would be impossible to execute on this site. I'm a little disappointed that the more I look into it the more I'm finding stuff that is useless here, and what is useful here is mostly stuff that could be accomplished with CSS as well. But I'm still trying new things for fun and you should too.

My teacher: W3Schools

Unplugged's picture

Javascript is tefc's future.

Javascript is tefc's future. I haven't taken a closer look at this yet but this is great. Thanks AlisonRobin.
AlisonRobin's picture

No problem. I'm not too

No problem. I'm not too convinced it's the future though, because so far most of what I've looked at are things that can already be done with CSS and usually with less trouble. A lot of what Javascript has going for it that CSS doesn't are things like math (I can't think of a way to use JS on this site where I'd even need to define a variable or use a for loop). I dunno. I'm still skeptical that it'll really start taking over. Though I have figured out a way to put a tic tac toe game in the forum.

That said, it's a lot of fun to play with. I used to know a language called Processing which has a lot of the same rules for syntax that JS does so at least I'm not entirely a fish out of water.
Unplugged's picture

I'm not sure, but the JS tabs

I'm not sure, but the JS tabs for example are way superior to the regular ones (which use really quite a messy method), and stuff like those changing images could allow for a lot of creativity with bios. It all depends on how you utilize these things. I could imagine some cool stuff coming out of it once everyone gets more acquainted with it. Though I don't know how I would use this code, for example.
AlisonRobin's picture

Me either. But I'd like if

Me either. But I'd like if people know that there's a method to do this even if they don't know anything about JS so they can say "well what if we try..." and then someone else can punch in the numbers later on.

I was going to do a huge grid of the letter O like 10 by 10 where each one would have a different JS function to change the letter color onmouseover so people could go through each of them and change their colors.

Actually, if I had a picture of a bubble wrap bubbles, I could make a big thing for people to pop them with their mouse. But I think that's already been done before and I'm just too sleepy to remember where I saw it.
littlesinner's picture

I love this. Track.

I love this. Track.

hardcore trackingg

hardcore trackingg

Thank you, Tuo and Kohva!

Track for future

Track for future

This is so epic! I am

This is so epic!
I am definitely going to stalk this. ^^
Image and video hosting by TinyPic