Skip to content

Instantly share code, notes, and snippets.

@2no
Created July 26, 2011 08:12
Show Gist options
  • Save 2no/1106250 to your computer and use it in GitHub Desktop.
Save 2no/1106250 to your computer and use it in GitHub Desktop.
フラグメント ID として(jQuery の)CSS セレクタでの指定を可能にする サンプル)http://www.wakuworks.com/jquery.cssFrag/
/**
* jquery.cssFrag: Using (jQuery)CSS Selectors as Fragment Identifiers
* Author: Kazunori Ninomiya
* Version: 1.0.0
* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
* Requires: jquery.js
* Requires: jquery.ba-hashchange.js
* http://benalman.com/code/projects/jquery-hashchange/docs/files/jquery-ba-hashchange-js.html
*/
(function($, window, document, undefined)
{
$(window).hashchange(function() {
var matches = decodeURIComponent(location.hash).match(/css\((.+)\)/);
if (matches) {
try {
var target = $("body").find(matches[1]); // avoiding XSS
if (target.length) {
setTimeout(function() { // for IE6
target = target.eq(0);
var offset = target.offset();
var x = offset.left - $(window).width() + target.width();
var y = offset.top;
$("html, body").scrollLeft(x).scrollTop(y);
target.focus();
}, 1);
}
}
catch (e) {}
}
});
}
)(jQuery, this, this.document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment