Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create Button MouseOver effect
#1
Welcome and Enjoy!

Intro:
Well this is my 7th day on this forum, so I've decided to create this little and simple tutorial wich
it's 7th tutorial I've posted on here, that's 1 tutorial per day. Yeye

I will show you two way of create mouseover effects for your buttons, 1st is CSS wich I prefer most,
and the 2nd is with JavaScript wich needs images with text on them.

CSS:
This is very simple to do and it doesn't need to be explained alot.

Code:
#button {
    width:108px;
    height:25px;
    background:url('normal.png');
    color:white;
    text-align:center;
    vertical-align:middle;
    cursor:pointer; cursor:hand;
}

#button:hover {
    background:url('hover.png');
}

#button is some standard styling for our button, #button:hover are style commands that are used when mouse pointer goes over the element with
the id "button".

The html code looks like this:
Code:
<div id="button" onclick="document.location='test.htm';">Home</div>

Simple, I've told you!

JavaScript
Alright this one is also a simple one, but I just don't like using it, and it doesn't work if your user has javascript disabled.

Code:
<script language="javascript" type="text/javascript">
            if(document.images) {
                var button1 = new Image();
                button1.src = "normal.png";
                
                var button2 = new Image();
                button2.src = "hover.png";
            }
        </script>

The abouve code sould be put between "<head></head>" tags.
We first check if our page has images with "if(document.images)", if it finds an "<img>" tag it will return 1/TRUE,
and the code between "{" & "}" will be executed.
We need to preload images first so the user won't need to wait for the hover image to load.
We do that by createing a new Image object
Code:
var button1 = new Image();
And then we set ".src" as a path to our images.

The above code is used this way:
Code:
<div onmouseover="document.button.src=button2.src;" onmouseout="document.button.src=button1.src;">
    <a href="test.htm"><img style="border:0px;" name="button" src="normal.png" width="108" height="25" alt="" /></a>
</div>

With "onmouseover" and "onmouseout" we control the appearance of our button, "document.button" means that we control the element named button.
(name="button")
We define "document.button.src" as variables that we have created previously with javascript, "button1" and "button2".

And again like I said it's easy.


Button Images (Click to View)



Thank you for reading!
Reply


Messages In This Thread
How to create Button MouseOver effect - by Gaijin - 10-10-2009, 11:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What File Would I Modify For New Button On My Main Page schneweis 2 873 12-23-2010, 10:20 PM
Last Post: Bourd
  Submit Button ElephantShoe 3 891 11-10-2010, 02:53 AM
Last Post: Malmoc
  Who wants to create a website? Emotion 8 1,820 08-02-2010, 11:52 PM
Last Post: Cybr
  Auto Click Button Scorpion 1 1,973 11-06-2009, 09:00 PM
Last Post: Gaijin

Forum Jump:


Users browsing this thread: 2 Guest(s)