Skip to content

Instantly share code, notes, and snippets.

@okuden-labo
Created March 19, 2015 03:35
Show Gist options
  • Save okuden-labo/f0e11d2100599ca83a23 to your computer and use it in GitHub Desktop.
Save okuden-labo/f0e11d2100599ca83a23 to your computer and use it in GitHub Desktop.
jQueryで画面の中心
/* 画面の中心に要素を配置 */
$(document).ready(function() {
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$("#my-id").center();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment