Highlighting Named Anchors with jQuery
I’ve always disliked the way named anchors (<a href=”#name”>...</a>) behave in browsers. It jumps your viewport to a different part of the page, but it’s rarely obvious which section you have landed on. If the page is long enough, the referenced section will start at the top of your browser, but where does it end? If the content is towards the bottom of the page, it may not be at the top of the page, but somewhere in the middle. And what if your content is in two columns? Or a table? What you’ve linked to becomes totally ambiguous.
Sphinx uses named anchors quite a bit, so I wanted something that would improve their usability for Django Best Practices. That’s when I dug up this clever little jQuery snippet from our code vault.
// highlight and fade background on named anchors // requires jquery.color.js http://plugins.jquery.com/project/color function highlight(elemId){ var elem = $(elemId); elem.css("backgroundColor", "#ffffff"); // hack for Safari elem.animate({ backgroundColor: '#ffffaa' }, 1500); setTimeout(function(){$(elemId).animate({ backgroundColor: "#ffffff" }, 3000)},1000); } if (document.location.hash) { highlight(document.location.hash); } $('a[href*=#]').click(function(){ var elemId = '#' + $(this).attr('href').split('#')[1]; highlight(elemId); });
This will briefly highlight and fade the DOM element referred to by the named anchor (technically, we’re using the id attribute, not the name attribute). You can see it in action by clicking here.
The link above should highlight this paragraph. In testing, Safari required the element to have a defined background color to start, but IE and FF were happy to fade back to whatever color that element inherited.
You can grab a copy of the snippet from GitHub. Did you find this helpful or have a better way of doing it? Let us know.
Comments
Got something to say?
Our Products
Categories
- accessiblity
- code
- company news
- django
- gondola
- open source
- portfolio
- presentation
- pro tip
- review
- screencast
- seo
- software
- subversion
- trailmapping
- wordpress
Archives
- July, 2010
- June, 2010
- May, 2010
- April, 2010
- February, 2010
- December, 2009
- November, 2009
- October, 2009
Elsewhere
What we’ve been up to online
-
Just launched a Flask/App Engine mini-site we've been tinkering on http://emailed-me.appspot.com/
Pete, 14 hours, 43 minutes ago -
created repository Emailed-Me-
Pete, 14 hours, 51 minutes ago -
Our first iPhone development project hit the App Store last week and is already over 1k users! Check them out @takemyspot #iphone #geodjango
Pete, 3 weeks ago -
Love the new sites! RT @welikesmall: We just launched two new sites. http://post.ly/mGoq
Pete, 3 weeks, 1 day ago -
Pro tip: Using pip safely for automated deployment (no more pesky prompts) http://bit.ly/b5zsPa
Pete, 4 weeks, 1 day ago -
commented on justquick/django-mailfriend
Pete, 1 month ago -
RT @unbracketed: Excited to have @mitsuhiko joining us for some work this summer :)
Pete, 1 month ago -
New blog post: managing supervisord with upstart http://bit.ly/db3p5N
Pete, 1 month ago -
Troubleshooting OpenID is just like user/password. Except you have 5 of them and and you don't know which one is failing, and 3 login pages
Pete, 1 month, 1 week ago -
This gets very interesting around 42 min. Using javascript to snoop inside firewalled networks http://bit.ly/aNVPc5
Pete, 1 month, 2 weeks ago -
The final tally is in. 8 Lincoln Loopers attending DjangoCon. 3 US, 4 EU, and 1 NZ. Looking forward to it!
Pete, 1 month, 2 weeks ago -
Twitter / Dustin Curtis: I'm flying to Madrid tomor ...
Dustin Curtis travels to Berlin, Bangkok & Madrid in exchange for design services as the result of a late night tweet.
Pete, 1 month, 2 weeks ago -
created branch ubuntu-8.04 at lincolnloop/fab-pave
Pete, 1 month, 3 weeks ago -
created repository fab-pave
Pete, 1 month, 3 weeks ago -
pushed to master at lincolnloop/django-mailfriend
Pete, 1 month, 3 weeks ago


Sliding the page, instead of letting the browser just jump there, would also help give a better sense of location. Ideally sliding to just above where the anchor is and then highlighting the proper area.
Is there any way to do the same if the destination is on another page?
Thank you, looks nice. The CSS3 pseudo class :target can help to achieve similar result.
But then again, your solutions works cross-browser…