あなたはそれを正しい方法で使用しませんでした.その関数は、最新のjqueryを使用して準備が整ったドキュメントで初期化する必要があります.
http://jsfiddle.net/n29up/1/
これはjQuery
コードです。jquery を次のように使用します。
ただし、必ず最新のjqueryプラグインを追加してください
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(function(){
$.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2 + "px");
this.css("left", ( $(window).width() - this.width() ) / 2 + "px");
return this;
}
$(".box").center(); // on page load div will be entered
$(window).resize(function(){ // whatever the screen size this
$(".box").center(); // this will make it center when
}); // window resize happens
});
</script>
あなたの小さなupdated css
:
.container {
height: 100%;
width: 100%;
background: #eee;
position: absolute;
}
.box {
height: auto;
width: 70%;
background: gray;
position: absolute;
margin: 0;
left:0;
top:0;
}