Scrolling to an internal hash link

Another little JS trick I wanted to bookmark for my own benefit.

The problem: You have a page filled with internal hash links, but don’t like the way the page jumps to the new location when you click one. You’d like it to scroll smoothly instead.

The solution: Use JavaScript/jQuery to cancel the default click event and scroll to the desired location instead.

$('a[href^=#]').on('click', function(e) { // apply to all hash links
  var el = $(this).attr('href');          // get the href
  e.preventDefault();                     // cancel default action
  $('body').animate({
      scrollTop: $(el).position().top     // scroll to the ID
    }, function() {
      window.location.hash = el;          //change bookmarkable URL
  });
});

jsBin demo

Diagnostic Imaging Specialists of Chicago

Diagnostic Imaging Specialists of Chicago
Diagnostic Imaging Specialists of Chicago

A minor redesign of this site gave our department’s new Search Engine Optimization expert an excuse to try out everything she’s been reading about. Directories, images, and PDFs were renamed to conform to Google’s recommendations, ALT texts were widely used, and all the source code was rebuilt using HTML5 tags. Microdata is implemented in certain areas for further SEO benefits.

Continue reading Diagnostic Imaging Specialists of Chicago

The mathematics of elections

Moving slightly off topic this time, I’ve been fascinated by the importance good math has played in the 2012 US Presidential election. A few links: