0

I want to appy a hover effect to div boxes, may be up to 60 boxes on a page. It should be quite equal to the css hover effect but I want to apply a fade effect to the hover color. E.g. I have light green as background color and dark green as hover background color, then it should fade from the one to the other side.

I played a bit around with jQuery but could get the result that I wanted:

    $(".box").hover(function() {
        $(this).animate({ backgroundColor: "#68BFEF" }, 1000);
    },function() {
        $(this).animate({ backgroundColor: "#68BFEF" }, 500);
    });
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

6 Answers6

2

You need to use a decent color plug-in. See jQuery animate backgroundColor for more information.

Also, your original code is not really going to do anything, as you want it to animate to another colour afterwards.

$(".box").each( function () {
  $(this).data('baseColor',$(this).css('color'));
  $(this).hover(function() {
    $(this).animate({ backgroundColor: "#68BFEF" }, 1000);
  },function() {
    $(this).animate({ backgroundColor: $(this).data('baseColor') }, 500);
  });
});

EDIT: see http://jsfiddle.net/eHXNq/2/ for example.

Community
  • 1
  • 1
Fred
  • 4,195
  • 2
  • 29
  • 42
0

I don't have much experience with jQuery, but it looks like just a copy-and-paste mistake, since you have the same color in both .animate()s

Tyler
  • 21,762
  • 11
  • 61
  • 90
0

I believe you are not using the hover function like you should. the second function is used when the user leave the box so your should return to the original color.

White for example:

  $(".box").hover(function() {
        $(this).animate({ backgroundColor: "#68BFEF" }, 1000);
    },function() {
        $(this).animate({ backgroundColor: "#FFFFFF" }, 500);
    });
RageZ
  • 26,800
  • 12
  • 67
  • 76
0
$(".box").hover(
  function () {
 // this is mouseover
  }, 
  function () {
//  this is mouse out
  }
);

see example here

http://jsfiddle.net/krRse/

kobe
  • 15,671
  • 15
  • 64
  • 91
0

review this code, I think this might help you!!!

 <!DOCTYPE html>
<html>
<head>
  <style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
  <ul>
    <li>Milk</li>
    <li>Bread</li>
    <li class='fade'>Chips</li>

    <li class='fade'>Socks</li>
  </ul>
<script>
$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  }, 
  function () {
    $(this).find("span:last").remove();
  }
);



//li with fade class
$("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

</script>

</body>
</html>

take a look at this link too, http://api.jquery.com/hover/

kobe
  • 15,671
  • 15
  • 64
  • 91
harishtps
  • 1,439
  • 7
  • 20
  • 35
-1

60 boxes? Please use event delegation, or live.

Mark
  • 32,293
  • 33
  • 107
  • 137
  • 2
    thinking this should be a comment, as it doesn't really answer the question. i do agree though. .delegate() is a beautiful thing. – nathan gonzalez Dec 16 '10 at 07:23
  • @RageZ, its not about catching them all, but the proliferation of events in the dom. you don't want 120 events hanging around that all do the same thing when 2 delegated events could accomplish the same thing. – nathan gonzalez Dec 16 '10 at 07:24