Best and easier way for rollover image links?

Discussion in 'CSS' started by Zaborg, Nov 17, 2010.

  1. #1
    Hi

    I've tried to create buttons with rollover effect, I've used Google, but I still can't find the best way to do this. They are all too complicated (long code) or bugged in some browsers.

    I want something which will work in all browsers and would be easy to write (code).

    Thanks!

    Zaborg
     
    Zaborg, Nov 17, 2010 IP
  2. radiant_luv

    radiant_luv Peon

    Messages:
    1,327
    Likes Received:
    34
    Best Answers:
    1
    Trophy Points:
    0
    #2
    As an exmaple you csn do this way,

    
    #button a {
    background-image:url("../images/button.png");
    background-repeat:no-repeat;
    background-position:center top;
    text-indent:-9999px;
    }
    #button a:hover{
    background-position:center bottom;
    }
    
    Code (markup):
    button.png will have the image of the button in normal state and hover state. on a:hover the position of the background will change.

    Take a look at this tutorial http://www.webvamp.co.uk/blog/coding/css-image-rollovers/
    In action "learn buttons": http://restoroofing.com
     
    radiant_luv, Nov 17, 2010 IP
  3. GWiz

    GWiz Peon

    Messages:
    359
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's the optimized version, most of it can be combined in 1 line:
    
    #button a {
    background:url("../images/button.png") center top no-repeat;
    text-indent:-9999px;
    }
    #button a:hover{
    background-position:center bottom;
    }
    
    HTML:
     
    GWiz, Nov 17, 2010 IP
  4. ronc0011

    ronc0011 Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you just want to do an image swap then a javascript roll over is probably the simplest most straight forward way to get there.

    Another option is to do it with CSS using the pseudo classes for the anchor tag.
     
    ronc0011, Nov 18, 2010 IP
  5. pagewil

    pagewil Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Make sure you use one image with a background position shift on hover. If you specify another image you will experience a slight load time on the hover...bad. This should be done with CSS not Javascript for the easiest and most efficient solution. There are plenty of tuts on this method.
     
    pagewil, Nov 22, 2010 IP
  6. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #6
    JS rollover means that you need 2 pictures (that means you have 2 http requests) to load for every link.
    If you do it with CSS you can use CSS sprites, you save one http request and no JS is involved.

    If someone has JS disabled (for any reason), the CSS menu will work ... JS rollover will not.

    There are several sites that handle CSS sprites, just Google for it
     
    CSM, Nov 23, 2010 IP
  7. GWiz

    GWiz Peon

    Messages:
    359
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I might be wrong, but I'm thinking you might be able to use JS with 1 image. You simply have to change the class (which would have a different background position for the same image), which would allow for advanced sprite effects to be utilized. And if you're creative enough, you could setup a <noscript> default CSS code to kick-in with the default :hover for changing the background position when JS is not available.
     
    GWiz, Nov 23, 2010 IP
  8. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #8
    Yeah, possible. But those stuff can be done with CSS without any line of JS or <noscript> tags, the effect is the same.

    Just use something like:

    
    a:hover, a:focus {...}
    
    Code (markup):
    and you are set.
     
    CSM, Nov 23, 2010 IP
  9. GWiz

    GWiz Peon

    Messages:
    359
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #9
    GWiz, Nov 23, 2010 IP
  10. CSM

    CSM Active Member

    Messages:
    1,047
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    55
    #10
    Well, it is pure CSS. Only the animation effect is done with jQuery.

    And if I take a closer look it is done in a bad way. It is possible with CSS sprites too.
     
    CSM, Nov 23, 2010 IP