Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Using Opacity to Show Focus with jQuery

40 Responses »
jQuery Opacity

A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery.

The jQuery JavaScript

$(document).ready(function() {
	//area 1
	$('.fade-area-1').children().hover(function() {
		$(this).siblings().stop().fadeTo(500,0.5);
	}, function() {
		$(this).siblings().stop().fadeTo(500,1);
	});
	//area 2
	$('.fade-area-2').children().hover(function() {
		$(this).siblings().stop().fadeTo(500,0.5);
	}, function() {
		$(this).siblings().stop().fadeTo(500,1);
	});
});

There you have it. Opacity is a very simple but effective tool for building an attractive interface.

Discussion

  1. August 27, 2009 @ 8:43 am

    I should add that the syntax to accomplish this goal is somewhat ugly, which is why I prefer MooTools. Case in point:

    $(this).siblings().stop().fadeTo(500,0.5);

    First, “siblings.stop()” doesn’t make sense because you don’t stop elements, you stop events. Second, the “fadeTo” method’s first argument is duration and not fade level. Why isn’t the method’s first argument the primary attribute it’s going to modify?

  2. August 27, 2009 @ 9:14 am

    Very nice, I like that. Thanks for posting!

  3. August 27, 2009 @ 10:09 am

    Nice effect. The list objects in the second demo seem to have a weird issue where the number 1 in the first li sort of disappears in Firefox 3.5.

  4. August 27, 2009 @ 10:27 am

    @LeRoy Lee: It must be a FF3.5 issue because there’s nothing code-wise that would cause that.

  5. August 27, 2009 @ 12:34 pm

    Just noticing the lack of Christina Ricci in your posts lately… Have you moved on?

  6. August 27, 2009 @ 12:47 pm

    Awesome effect. Works fine in Firefox 3.5 for me…

  7. August 27, 2009 @ 1:24 pm

    oooeee!
    jQuery is nice, I like that. Thanks!

  8. August 27, 2009 @ 3:32 pm

    nice… it so cute.

  9. pockata
    August 27, 2009 @ 4:31 pm

    Very nice. Just what i was looking for :)

  10. August 27, 2009 @ 5:42 pm

    @Aaron Newton: Bite your tongue — Ms. Ricci is the only one for me.

  11. August 28, 2009 @ 12:21 am

    @David: I experience the same bug as on the mootools version…very weird…it doesn’t happen with chrome though…so it must be a firefox issue.

  12. August 29, 2009 @ 4:29 pm

    Great tip, found you via Reddit. Upvote!

  13. eric tf bat
    August 29, 2009 @ 5:58 pm

    Not bad. Will it take much extra effort to get it working with keyboard focus as well? Not everyone relies on mice for navigation…

  14. August 30, 2009 @ 10:57 am

    Looks interesting. How do I use it?
    I mean, can you upload a demo file with the complete tutorial?

  15. graham
    September 1, 2009 @ 10:54 am

    Looks very nice. I can see using that as a nice visual aid that has no impact on accessibility. One note: don’t confuse this with showing keyboard focus (e.g. changing the background of an editable control when the caret is in it). I would say it’s more like showing hot-spots.

  16. September 3, 2009 @ 8:24 am

    jQuery <3

    Nice effect David! Thanks for sharing :)

  17. ndo
    September 9, 2009 @ 8:08 pm

    Hey, this works great I figured it out for my website gallery, I have a thought though. Is it possible to get it to fade like that when you click a scroll (or the navigator plugin) button. So that while the jquery scrollable is scrolling from one div etc. to the next it fades out, and then fades back in when the scrollable is finished scrolling?

  18. david
    September 11, 2009 @ 1:59 am

    Love this. Any chance of a Mootools version?

  19. david
    September 11, 2009 @ 2:03 am

    Got ahead of myself there didn’t I, lol. Dave you’re a star, thanks:)

  20. September 18, 2009 @ 4:08 pm

    Nice effect …great in active links!! ;(

  21. little doe
    September 24, 2009 @ 2:49 pm

    I have never done any of this kind of thing before, and it’s working awesome for me!!!
    I’m in love with it; before I’d have to put all the images in flash to make such an effect. WELL DONE!

  22. marjan
    October 26, 2009 @ 12:55 pm

    Hello,

    well done.

    Someone else having problems in IE7 on WINXP? Text gets blury, distorted.

    Someone know what could it be?

  23. October 29, 2009 @ 5:45 pm

    I have the same problem in IE7, the text change, but I don’t find a solution…

    Help ;-)

  24. November 3, 2009 @ 3:18 am

    Hmmm.. this reminds me of a very similar effect on jQuery Style, I think your solution is a bit better though. Cheers

  25. November 11, 2009 @ 10:42 am

    Hi, works a treat but for one little issue which I cannot solve. The top tabs on my new site http://www.trial.dcprint.co.uk have a tendency to jump around in IE depending on which one is clicked or where the pointer is placed. Only happens on first load and then seems to settle down until clicked again. Firefox no issues, but Opera 10.0 doesn’t run the effect at all.

  26. thebat
    November 27, 2009 @ 9:32 am

    It’s an awesome effect. Works fine in Firefox… but not in IE !

    I’m using it on PNG images (with transparency) and unfortunately, when the mouse leave the image, an ugly “border” appears.

    If anyone have a solution…

  27. samuel larsson
    December 18, 2009 @ 9:36 am

    Hi! I´m using your script at http://www.emmaeriksson.se/sopor-i-neapel, and it works great.

    I´m just curious, how would you select JUST the thumbnails for the effect? You know, make it work not on all siblings but just images w a certain class? I´m kinda new at jQuery and I´ve worked tried hard to modify your script without any success…

    Thank you for a great page!

  28. rodrigo
    December 29, 2009 @ 3:13 pm

    How can I to downlad the code?

  29. barneyy
    January 19, 2010 @ 2:35 am

    Hi David,

    really awesome, this is really really really nice for me – i’m a jquery-beginner and got it to work for my galleries.

    thx a lot for this snippet of code….

  30. derleth
    January 29, 2010 @ 10:16 pm

    gracias amigoo, es simple y muy bueno.. me encanto :D

  31. marc
    January 31, 2010 @ 9:04 am

    Thanks a lot for this. Was all working great for me until I looked at it in IE8 – it works as you have it but if you add links to the images then the effect stops working – any way to fix this?

  32. March 23, 2010 @ 10:27 am

    I’m using this for the first time on a client site. Everything works great except when I make the images linked. The fade doesn’t work in safari. Is there a work around?
    Seems Marc had the same problem in IE8. I got it to work in IE7 though.

    Thank you very much, btw.

  33. joey william s.
    April 30, 2010 @ 5:28 pm

    Very cool! Nie effect… Thanks!

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!