O'Reilly

iPhone Click Effect Using MooTools or jQuery

By on  

One thing I love about love about Safari on the iPhone is that Safari provides a darkened background effect when you click a link. It's the most subtle of details but just enforces than an action is taking place. So why not implement that on any page? We can easily do so using MooTools or jQuery.

The CSS

.clicked { padding:1px 2px; -moz-border-radius:5px; background:#aaa; }

Style as you wish!

The MooTools JavaScript

window.addEvent('domready',function() {
	var lynx = $$('a');
	lynx.addEvent('click',function(e) {
		lynx.removeClass('clicked'); //remove from others
		this.addClass('clicked');
	});
});

The syntax between the two frameworks is very similar.

The jQuery JavaScript

$(document).ready(function() {
	var lynx = $('a');
	lynx.click(function(e) {
		lynx.removeClass('clicked');
		$(this).addClass('clicked');
	});
});

My example shows the gray background but what's great is that since the snippet uses a CSS class, you can make the background any color you'd like. You could add a spinner next to the link or italicize the link text. Of course when a link is clicked there's only a moment where you can see the added effect but I think it's worth it!

Track.js Error Reporting

Upcoming Events

Recent Features

  • Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • 9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

  • Link Nudging Using Dojo

    In the past we've tinkered with link nudging with MooTools and link nudging with jQuery. In an effort to familiarize myself with other JavaScript frameworks, we're going to try to duplicate that effect with another awesome framework: Dojo. The JavaScript: Attempt...

  • MooTools, Mario, and Portal

    I'm a big fan of video games. I don't get much time to play them but I'll put down the MacBook Pro long enough to get a few games in. One of my favorites is Portal. For those who don't know, what's...

Discussion

  1. You’re right, this effect very subtle, but incredibly effective.

    Curious how a linked transparent gif/png would be affected? I’m assuming you would see the ‘clicked’ background on those as well?

  2. Nice stuff, but shouldn’t it be e.preventDefault(); on the mootools version too?
    I would like to know how you did this textmate snippet integration too, your awesome!

  3. My bad — I forgot to remove the “stop” and “preventDefault()” ‘s from when they were demos. We don’t want to stop or prevent anything.

  4. Mr.X.

    I love it! I’ve made a hover effect a while back and this is a great add to it since a lot of my site uses ajax!

    Here is my hover-effect script:

    $$('a[not!=e]').each(function(el){
    	
    		el.addEvent('mouseover', function(event){
    			var morph = new Fx.Morph(el, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
    			var bgcolor = el.getStyle('background-color');
    			morph.start({
    			'padding-top': [0 , 5],
    			'padding-bottom': [0 , 5],
    			'background-Color':'#cfffff'
    			});
    	
    		});
    		el.addEvent('mouseout', function(event){
    			morph_delay = function(){
    				var morph = new Fx.Morph(el, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
    				morph.start({
    				'padding-top': [5 , 0],
    				'padding-bottom': [5 , 0],
    				'background-Color':this.bgcolor
    				});
    			}
    			morph_delay.delay(50)
    		});
    })
    
  5. Thomas

    Excuse me, but why just not use the :active CSS-pseudo class?

  6. Thomas

    @Thomas: CSS-pseudo selector* even

  7. Couldn’t you mostly do this with CSS? the a:active pseudo-class will give you something when they click on it.

    It’ll go away when they release the mouse button (so I wouldn’t do it if you wanted a spinner or something fancy) but if you just want to visually register that they clicked a link, :active might be enough.

  8. “:active” is a great option but prefer the javascript method because it gives me more options.

  9. Mr.X.

    Is it my imagination or lynx is named by new Ubuntu 10.04: Lucid Lynx ?

  10. Thomas

    @David Walsh: I don’t see what additional options you’re referring to in the above example that can’t be solved with the :active pseudo-selector.

    I found a nice article that pretty much sums up my thoughts about the use of (redundant) Javascript: http://css-tricks.com/you-know-you-should-use-javascript-when/
    “JavaScript: If it can be done in another language, it should be done in another language.”

    Regards

  11. Don’t waste resources and just remove the class from the active link ;)

    http://paste.davidwalsh.name/99

  12. Good call Kassens!

  13. Thomas: I realize that. One thing I wanted to toy with was leaving the class on anything clicked. Maybe a different color for those that weren’t the most recent clicked. In that case I’d definitely JS. I do, however, see where you’re coming from.

  14. I hardly see anything that resembles the iPhone UI, but thanks for the post and the mootools & jQuery comparison code. Also I love your code preview, it is FTW.

  15. I don’t mean to repeat what others have already stated, but I have to agree that CSS is the right tool for this task.

    I believe that :focus is what you’re after, not :active. The link remains “focused” even after the mouse button is released.

  16. Agree, the :active selector is there for a reason, and you don’t need any javascript at all for it!

  17. Andrew

    Personally, I don’t think he was trying to show a better than css way, simply showing up to do. Also, if we’re doing this for Safari, should we not be using the -webkit-border-radius? I was under the impression Safari doesn’t use the -moz. Cheers.

  18. Al

    Would like to know how to apply it to a specific div so that it’s not applied to all clickable links including pics, thx.

  19. I’d humbly suggest a mousedown for the addClass and mouseup and mouseout for removeClass to better resemble iPhone functionality.

  20. Hamid

    i think it is better if you use this :

    a {
    outline: none;
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • OâReilly Velocity Conference â New York

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • Free Download: Font Bundle Featuring 17 Incredible Typefaces

    The only thing we love more than a good font, is a good free font. So we’ve combed the Web for some of our favorite free fonts, and gathered them here in a single download. You’ll find a variety of useful typefaces, from highly geometric designs...

  • OâReilly Velocity Conference â Amsterdam

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • CanIUse Command Line

    Every front-end developer should be well acquainted with CanIUse, the website that lets you view browser support for browser features.  When people criticize my blog posts for not detailing browser support for features within the post, I tell them to check CanIUse:  always up to date, unlike...

  • Generating Alternative Stylesheets for Browsers Without @media

    If your CSS code is built with a mobile-first approach, it probably contains all the rules that make up the "desktop" view inside @media statements. That's great, but browsers that don't support media queries (IE 8 and below) will simply ignore them, ending up getting the...