要素を画面真ん中に配置する

Ads

Result

jQuery

jQuery.fn.center = function () {
    //position:absolute;を与えて、ウィンドウのサイズを取得し、topとleftの値を調整
    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;
};
$("div").center();

css

div{
    width:100px;
    height:100px;
    background:#eee;
}

via

CENTER DIV WITH JQUERY

タイトルとURLをコピーしました