`

Center an element on the screen

阅读更多
$(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;  
  }  
  $("#id").center();  
}); 


引自:25 个 jQuery 使用技巧(jQuery tips, tricks & solutions)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics