2

I am working on a PHP file. I'm working on the menu bar, the menu bar contains all the image buttons, if someone hovers on one of the buttons I want them to change image(color). Could someone help me out with this?

$globalsettings = array(
    'src' => $sImageURL.'global1.png',
    'alt' => $clang->gT("Global participant settings"),
    'title' => $clang->gT("Global participant settings"),
    'style' => 'margin-left:5px',
    'style' => 'margin-right:1px'
);
7
  • You should take a look at CSS sprites Commented May 3, 2013 at 8:08
  • PHP doesn't handle this... CSS or Javascript would be more suited for this job. You can have a CSS :hover so it changes background color. Commented May 3, 2013 at 8:08
  • How could i change a background color, when it's an image? Please read before answer.. Commented May 3, 2013 at 8:10
  • @JohanVanBaak So you have a background-image and you want to change it to background-color on hover ? Commented May 3, 2013 at 8:12
  • No i have an menu at the homepage, the home/about-us/contact/etc.. are all images. I want that when someone hovers the home image, it switches to the same image but in another color. and the code for example global settings are above, each image has his own code. but how can i put an hover effect on the code above. Commented May 3, 2013 at 8:19

4 Answers 4

2

You can create hover effects using CSS (cascading stylesheets). Your CSS must be in an external stylesheet or embedded style element.

I'm using BUTTON that will style all <button> elements, but you can replace it with whatever element you want to style, such as an <img> with IMG (lowercase or uppercase).

BUTTON { 
    background: url(my_bg.png); 
}

BUTTON:hover { 
    background: url(my_hover_bg.png); 
}

If you don't know how to use stylesheets, just insert embedded styling into the <head> of your HTML document.

<style type="text/css">
    /* Place CSS here */
</style>

If you want you can take it a step further and use CSS sprites (like old videos games used to do it). CSS sprites are a collection of images in one single image, and you simply change the position of the location of the background, and it creates the effect. You can achieve this like this:

#myelement { 
    background: url(my_bg.png) -0 -0; 
}

#myelement:hover { 
    background: url(my_bg.png) -0 -100px; 
}

There are also old school ways of hover effects but they're like Frontpage-era, so I don't recommend using them. CSS hover effects is the standard of today.

Sign up to request clarification or add additional context in comments.

3 Comments

where could i define BUTTON then? i'm sorry but my english is not verry well my gramar could be bad.
Updated again to explain what elements you can style with CSS.
Updated again explaining CSS sprites.
0

You're trying to solve 2 problems in one step. You need to get the images to display and then swap between them on hover.

You can't dynamically edit a button in JS (ok, you could with canvases and html 5 but it's non-trivial). So, you need to use CSS (or possibly JS) to to swap between 2 images.

Where those images come from is up to you - you can either pre-generate them which is a little work up front but easy to implement and no PHP required. This would be the preferred option if there's only one or two variations in colour.

Alternatively, you can have a PHP script which generates the images on-the-fly (and ideally caches them to save recomputing them later). This allows for infinite variation but requires more overhead on the server. This approach is commonly used to generate thumbnails as the source image isn't known in advance

Note that PHP has no control over when each image is displayed - it simply provides images to your CSS/JS in exactly the same way as a webserver would serve a static image.

If you want to edit an image in PHP, you need to look at the GD+ library

Comments

0

You can use css to do this quite easily by using the content: selector.

for example, your markup might look like this:

<div class="link" id="link1">
    <a href="yourhref"><img /></a>
</div>

and the css would be something like:

#link1 a img{
    content:url("http://www.maxxpotential.com/stephen2/wp-content/uploads/2013/03/Images-from-Deep-in-the-Woods-by-Astrid-Yskout-4.jpg");
}

#link1:hover a img{
content:url("http://blogs.mathworks.com/pick/files/zebrainpastelfield.png");
}

by using the selectors you assign in your script, you should find it pretty easy to amend this to suit your needs.

here is a working fiddle demonstrating this http://jsfiddle.net/pWYtu/1

1 Comment

I understand but i can't really make DIV's the $home, $globalsettings are already assigned.
0

You can use sprite image and onhover change position. also you will get benefit of performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.