Skip to content

Instantly share code, notes, and snippets.

@HungYuHei
Created August 15, 2011 10:11
Show Gist options
  • Save HungYuHei/1145997 to your computer and use it in GitHub Desktop.
Save HungYuHei/1145997 to your computer and use it in GitHub Desktop.
把一个元素放在屏幕的中心位置
jQuery.fn.center = function () {
this.css('position','absolute');
this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop() + 'px');
this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px');
return this;
}
//这样来使用上面的函数:
$(element).center();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment