2013-04-04 61 views
0

我有这段代码在jsfiddle上运行完美,但是当我在Dreamweaver中编写代码并在Chrome(和其他浏览器)中预览时,中央div(包含测试的3个图像)不是垂直集中加载时。只有当我缩放浏览器窗口,它正确地居中,因为我预期..加载时jQuery元素不能被垂直居中

任何人都可以帮忙吗?谢谢!

http://jsfiddle.net/nampham/v6HLT/

HTML

<!--menu--> 
<a href="#" id="logo"><b>bitchism<br>.</b></a> 
<a href="#" id="projects"><b>projects<br>.</b></a> 
<a href="#" id="writings"><b>writings<br>.</b></a> 
<a href="#" id="contact"><b>.<br>contact</b></a> 
<!--menu--> 

<div id="first"> 
<img src="image/circle.gif" style="width:30%;display:inline-block"/> 
<img src="image/cloud3.gif" style="width:30%;display:inline-block"/> 
<img src="image/circle2.gif" style="width:30%;display:inline-block"/> 
<br />Testing 
</div> 

SCRIPT

$(document).ready(function(){ 

$(window).resize(function(){ 


    $('#projects').css({ 
     position:'fixed', 
     top: ($(window).height() - $('#projects').outerHeight())/2 
    }); 

    $('#writings').css({ 
     position:'fixed', 
     top: ($(window).height() - $('#writings').outerHeight())/2 
    }); 

    $('#logo').css({ 
     position:'fixed', 
     left: ($(window).width() - $('#logo').outerWidth())/2 
    }); 

    $('#contact').css({ 
     position:'fixed', 
     left: ($(window).width() - $('#contact').outerWidth())/2 
    }); 

    $('#first').css({ 
     position:'fixed', 
     top: ($(window).height() - $('#first').outerHeight())/2, 
     left: ($(window).width() - $('#first').outerWidth())/2 
    }); 

}); 

$(window).resize(); 

}); 


body 
{ 
background-color:#FFF; 
background-image:url(image/dot.png); 
background-repeat:repeat; 
font-family: 'VT323', cursive; 
text-align:center; 
} 


a{ 
text-decoration:none; 
color:#F00; 
font-size: 20px; 
} 

a:hover{ 
text-decoration:underline; 
} 

a:active{ 
text-decoration:line-through; 
color:#F00; 
} 


#logo 
{ 
position:fixed; 
left:50%; 
top:0; 
padding-top:10px; 
} 



#contact 
{ 
position:fixed; 
left:0; 
bottom:0; 
padding-bottom:10px;  
} 


#projects 
{ 
position:fixed; 
left:0; 
top:50%; 
transform:rotate(-90deg); 
-ms-transform:rotate(-90deg); /* IE 9 */ 
-moz-transform:rotate(-90deg); /* Firefox */ 
-webkit-transform:rotate(-90deg); /* Safari and Chrome */ 
-o-transform:rotate(-90deg); /* Opera */" 

} 

#writings 
{ 
position:fixed; 
right:0; 
top:50%; 
transform:rotate(90deg); 
-ms-transform:rotate(90deg); /* IE 9 */ 
-moz-transform:rotate(90deg); /*Firefox */ 
-webkit-transform:rotate(90deg); /* Safari and Chrome */ 
-o-transform:rotate(90deg); /* Opera */ 
} 

#first 
{ 
position:fixed; 
top:50%; 
    left:50%; 
} 
+0

可能是因为它,因为它是在DOM准备,而不是窗口负载点火没有高度等呢。这可能有所帮助:http://stackoverflow.com/questions/10778070/window-onload-vs-document-ready-jquery/10778083#10778083 – ahren 2013-04-04 06:19:46

+0

Fwiw,你应该保存对'$(窗口)'的引用在顶部你的脚本,所以你不会反复制作一个jQuery对象。像'$ window = $(window); //其余代码使用$ window' – Impirator 2013-04-04 06:23:47

回答

0

试试这个,你应该把你的代码放到一个命名函数,然后调用该函数。

例如:

function onResize() { ... } 

然后

$(document).ready(function(){ 
    $(onResize); 
    $(window).resize(onResize); 
}); 

$(document).ready(function(){ 
    $(window).load('resize', onResize); 
    $(window).bind('resize', onResize); 
}); 

了解更多:jQuery resize function doesn't work on page load

0

试试这个

现场小提琴here

function center_div(){ 
      $('#projects').css({ 
      position:'fixed', 
      top: ($(window).height() - $('#projects').outerHeight())/2 
     }); 

     $('#writings').css({ 
      position:'fixed', 
      top: ($(window).height() - $('#writings').outerHeight())/2 
     }); 

     $('#logo').css({ 
      position:'fixed', 
      left: ($(window).width() - $('#logo').outerWidth())/2 
     }); 

     $('#contact').css({ 
      position:'fixed', 
      left: ($(window).width() - $('#contact').outerWidth())/2 
     }); 

     $('#first').css({ 
      position:'fixed', 
      top: ($(window).height() - $('#first').outerHeight())/2, 
      left: ($(window).width() - $('#first').outerWidth())/2 
     }); 
    } 

$(window).load(center_div); 
$(window).resize(center_div); 
+0

感谢它的工作!我应该把.load的东西.. – 2013-04-04 06:35:41

+0

@NamPham:如果它是根据你的期望,比你应该把它标记为答案。 – 2013-04-04 06:39:35