A while ago a user asked me how the widget hover effects were done in my Checkmate theme. Well I’m going to tell you. It is a little black magic, a little jquery, and passing through the outer realms of probability. But I think anyone can handle it.
First things last
First you’ll need jQuery and the color animations plugin. Add these in your footer so they load last(that’s just good practice).
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="/js/jquery.color.js"></script>
The basic markup
The basic HTML for this example is something like the following.
<div class="widget">
<h3>Widget Title</h3>
<ul>
<li>Item One</li>
<li>Item Two</li>
<li>Item Three</li>
</ul>
</div>
Hover Magic
Now for the code that makes the hover actually work. Don’t blink you might miss it.
var originalBG = $(".widget ul li").css("background-color");
var fadeColor = "#ddd";
$(".widget ul li").hover( function () {
$(this).animate( { backgroundColor:fadeColor}, 450 )
.animate( {backgroundColor:"#fff"}, 950 )
},
function () {}
);
That’s it! Ok I’ll explain a bit. The first two lines are setting up some variables, this makes the code a bit cleaner but you could include the variables directly if you wanted. The first variable is the original background color set in the CSS file. The second is the background color which will fade in.
If you’re familiar with jQuery you’ll know the third line includes the selector(“.widget ul li”). Next is the actual animation. First the line items background color is faded to the whatever the fadeColor is set to. Next the background color is faded out to the original color before the mouse leaves. This is the effect I wanted, but if you want the color to fade out after the mouse pointer leaves then add the
.animate( {backgroundColor:"#fff"}, 950)
line to the second empty function like so:
var originalBG = $(".widget ul li").css("background-color");
var fadeColor = "#ddd";
$(".widget ul li").hover( function () {
$(this).animate( { backgroundColor:fadeColor}, 450 )
},
function () {
$(this).animate( {backgroundColor:"#fff"}, 950 )
}
);
The End
That is just a quicky, there are a different effects you can easily get from adding more animation, colors, and timing. But I’ll let you figure all that out.
8 Comments
Leave a Commentthank you for this, works perfectly.
5th Nov 2008
Sorry, It doesn’t work. It’s for the left side on my site. I have also a menu with ul – li attributes. Can anyone help me?
10th Jul 2009
Make sure both the jQuery library and the color plugin are loading. Also check that the selectors are right in your code.
10th Jul 2009
Sometimes with me other plugins can overrule other plugins :)
4th Sep 2009
Thanks , this is perfect ! .. i love jquery
20th Jan 2010
Wow, wonderful effect, i am using in image border.
Thank you very much!
16th Feb 2010
i tried many way… but now work for me.. example please
3rd Mar 2010
Trackbacks / Pingbacks
show/hide trackbacks[...] jquery, and passing through the outer realms of probability. But I think anyone can handle it. Web Site AKPC_IDS += "1098,";Popularity: 1% [?] Share and [...]