what's best for hover effect?

Discussion in 'HTML & Website Design' started by zuly, Oct 28, 2008.

  1. #1
    hello i'm wondering what do you experienced members think what is the best for creating hover effect, mainly to change a picture on mouse hover but to have a link on the image. can it be done with CSS or Javascript or something else?
     
    zuly, Oct 28, 2008 IP
  2. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    All depends, how do you want to "change" the picture? Do you want to manipulate the picture directly? E.g move it, fade it, etc. If this is the case, JavaScript is your man.
     
    garrettheel, Oct 28, 2008 IP
  3. Sean@WMS

    Sean@WMS Peon

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can do it with either CSS or JS, but CSS would be better.

    Here are two examples in CSS:
    
    <style type="text/css">
    /* Method #1 */
    .imageBox {
     width: Xpx;
     height: Ypx
    }
    a .imageBox {
     background-image: url(/images/image01.jpg)
    }
    a:hover .imageBox {
     background-image: url(/images/image02.jpg)
    }
    /* Method #2 */
    .imageBox img {
     border: 0
    }
    .imageBox {
     background-image: url(/images/image02.jpg)
    }
    .imageBox a {
     display: block;
     width: Xpx;
     height: Ypx
    }
    .imageBox a:hover img {
     display: none
    }
    </style>
    
    Code (markup):
    
    <!-- Method #1 -->
    <a href="#"><div class="imageBox"></div></a>
    <!-- Method #2 -->
    <div class="imageBox">
     <a href="#"><img alt="alt text" src="/images/image01.jpg" width="X" height="Y" /></a>
    </div>
    
    Code (markup):
     
    Sean@WMS, Oct 28, 2008 IP
  4. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #4
    CSS all day everyday unless you want some fancy transitions
     
    wd_2k6, Oct 28, 2008 IP
  5. arizona39

    arizona39 Well-Known Member

    Messages:
    622
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    108
    #5
    I like menu images when you hover over it, there's a new color or underlined button
     
    arizona39, Oct 28, 2008 IP
  6. zuly

    zuly Well-Known Member

    Messages:
    317
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    thanks for this, i was doing something similar but i gave up on that when i found out that IE 6 and earlier doesn't support these CSS effects, and a lot of people still uses IE 6.:confused::confused::confused:
     
    zuly, Oct 29, 2008 IP
  7. outspan

    outspan Active Member

    Messages:
    420
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #7
    outspan, Oct 29, 2008 IP
  8. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #8
    I opened a thread on this a few days ago and it seems that css wins every time. less code + works every time.
     
    fadetoblack22, Oct 29, 2008 IP
  9. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    or just
    <img src="image.gif" onmouseover="this.src='image_hover.gif'" onmouseout="this.src='image.gif'" />
    HTML:
    :)
     
    elias_sorensen, Oct 29, 2008 IP
  10. zuly

    zuly Well-Known Member

    Messages:
    317
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #10
    yes i've been using that, and @ Sean@WMS i tried your suggestions but the first in IE 6 changes the image and it stays like that, you have to refresh the page to see it properly again second didn't work that well at all, and yes i see that javascript is the safest bet for doing this, that IE should be banned or something.
     
    zuly, Oct 29, 2008 IP
  11. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #11
    fadetoblack22, Oct 29, 2008 IP
  12. Sean@WMS

    Sean@WMS Peon

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Confounded IE 6, LOL!

    Here's another:
    
    <style type="text/css">
    .imageBox {
     width: Xpx;
     height: Ypx;
     display: block;
     background-image: url(/images/image01.jpg)
    }
    .imageBox:hover {
     background-image: url(/images/image02.jpg)
    }
    </style>
    
    Code (markup):
    
    <a href="#" class="imageBox"></a>
    
    Code (markup):
    This works in IE 6 & 7, FF 2 & 3, Google Chrome, and Safari 3
     
    Sean@WMS, Oct 29, 2008 IP
  13. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    This method above is foolproof and will work in any browser. Use it ^_^
     
    garrettheel, Oct 29, 2008 IP
  14. websushil

    websushil Active Member

    Messages:
    137
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #14
    .bidimage
    {
      background-image:url(images/bid.gif);
      background-repeat:no-repeat;
      height:28px;
      width:62px;
      cursor:pointer;
    }       
    
    .loginimage
    {
      background-image:url(images/login.gif);
      background-repeat:no-repeat;
      height:28px;
      width:62px;
      cursor:pointer;
    }
    Code (markup):
    Since image is used as the background in div, the height and width should be defined and declared one pixel more than the background image.

    Now, keep in the following division in the “index.html” inside the body tag.

    <div class="bidimage"> </div>
    Code (markup):
    Now, define the script for the hover effect using jQuery inside the the “index.html” file.

    <script>
    $(document).ready(function()
    {
      $("div.bidimage").mouseover(function ()
      {
        $(this).addClass("loginimage");
      });
    
      $("div.bidimage").mouseout(function ()
      {
        $(this).removeClass("loginimage");
      });
    });
    </script>
    Code (markup):
     
    websushil, Oct 30, 2008 IP
  15. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    ^ why on earth would you use javascript for a task that CSS is more than capable of with :hover...
     
    garrettheel, Oct 30, 2008 IP
  16. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #16
    Because you have to set the image as a background on an empty div, and therefore you have to manually set the width and height of that div.

    1. Some browsers may not be parsing that width/height parametre.
    2. What if the image should be clickable?
    3. It's strange to show an image "without showing it"
     
    elias_sorensen, Oct 30, 2008 IP
  17. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #17
    The image can still be clickable without javascript.. and the rest of that doesn't make a whole lot of sense.

    Also, what about the people who have javascript turned off? I always try to use javascript where it will enhance things for those who have it on but make no difference to the site for those who have it off. Setting background images with javascript is a bad idea.
     
    garrettheel, Oct 30, 2008 IP
  18. Sean@WMS

    Sean@WMS Peon

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Serously . . . but if you're going to go JS, why so complicated about it? See:
    Much more elegant JS solution.
     
    Sean@WMS, Oct 30, 2008 IP
  19. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #19
    How would you make it clickable without javascript using a W3 strict solution? Load a blank gif as 100%x100% inside the div?

    And some browsers don't parse the width and height attribute, and scales the div by the content.

    However, I don't see why you wouldn't use the javascript solution. It's more elegant and works better.
    IF someone has disabled javascript in their browser, they shouldn't visit websites from the 21st century.
    Come on.. I think that about 99% of ALL websites has some JS code.
    Google apps, Facebook, Microsoft's website, Apple's website. EVERYONE.
    If you haven't enabled it, you should understand that you don't get that much out of your browser.

    Oh... And in my image code, I forgot to add an alt attribute
    <img src="image.gif" onmouseover="this.src='image_hover.gif'" onmouseout="this.src='image.gif'" alt="" />
    HTML:
     
    elias_sorensen, Oct 30, 2008 IP
  20. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Correction - they have non-destructive javascript on their sites. This is so that all their site functions can still be used without Javascript, they just look nicer with it. As was mentioned previously, a lot of schools and workplaces all have Javascript turned off and a lot of people turn it off for security reasons. Making a site inaccessible to people without Javascript turned on is pure ignorance and extremely bad practice in the web development world.
     
    garrettheel, Oct 30, 2008 IP