Created
July 26, 2011 08:12
-
-
Save 2no/1106250 to your computer and use it in GitHub Desktop.
フラグメント ID として(jQuery の)CSS セレクタでの指定を可能にする サンプル)http://www.wakuworks.com/jquery.cssFrag/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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