回答
使谷歌搜索不會傷害......
與CSS定心:
http://www.bluerobot.com/web/css/center1.html
http://www.spanish-translator-services.com/espanol/t/007/center.html
http://simplebits.com/notebook/2004/09/08/centering/
與JavaScript(jQuery的)定心:
沒有你的答案的一部分解決純JavaScript的解決方案(你提到jquery,但OP沒有要求)。 *但是,CSS可能是最好的方法,如果使用javascript,jQuery可能是最好的選擇。所以+1 :) – Cam 2010-08-23 04:42:36
jQuery版本比純粹的Javascrip版本更加優雅,因爲所有的瀏覽器兼容性問題已經被jQuery解決了。如果可能的話,我的建議總是使用jQuery。感謝您的支持。 – 2010-08-23 04:59:17
真的,CSS是最好的方式來做到這一點(根據Sebastián的回答),如果你必須使用JS然後去jQuery。但是,您要求提供JavaScript解決方案,因此您會在下面找到一個。
真的只有兩個reaons我可以看到的js是必要有:
- 如果圖像爲中心作爲用戶交互的結果或
- 如果圖像需要進行一次中心,和那麼應該保持靜態(而不是保持居中,就像在CSS解決方案中那樣)。
反正...享受:
用法:
imgToMiddle('imageid');
注意, '圖像標識' 是要在屏幕的中心放置圖像的ID。該函數修改圖像的CSS屬性,將其放置在屏幕中間。
代碼:
//viewport width/height code from here: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
function imgToMiddle(imgid){
function viewportWidth(){
var viewportwidth;
if (typeof window.innerWidth != 'undefined'){
viewportwidth = window.innerWidth;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
viewportwidth = document.documentElement.clientWidth;
}
else{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
}
return viewportwidth;
}
function viewportHeight(){
var viewportheight;
if (typeof window.innerWidth != 'undefined'){
viewportheight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
viewportheight = document.documentElement.clientHeight;
}
else{
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}
return viewportheight;
}
var img=document.getElementById(imgid);
img.style.position="absolute";
img.style.left=viewportWidth()/2-img.width/2;
img.style.top=viewportHeight()/2-img.height/2;
}
您的代碼運行良好,但我稍微修改了它,並將它作爲一個更多的答案發布,但參考您的答案。 – TARKUS 2013-10-23 14:04:53
請參考我following answer
這是凸輪的回答修改(我有一些輕微的修改工作)。這個答案提供了更多的jQuery,最重要的是,position:fixed,這樣無論你需要滾動多少或多少,結果div總是在你的視口中間。
function imgToMiddle(imgid){
function viewportWidth(){
var viewportwidth;
if (typeof window.innerWidth != 'undefined'){
viewportwidth = window.innerWidth;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
viewportwidth = document.documentElement.clientWidth;
}
else{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
}
return viewportwidth;
}
function viewportHeight(){
var viewportheight;
if (typeof window.innerWidth != 'undefined'){
viewportheight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
viewportheight = document.documentElement.clientHeight;
}
else{
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}
return viewportheight;
}
var img=document.getElementById(imgid);
$(img).css("position","fixed");//note: position:"fixed" will keep the div exactly in the middle of your browser viewport. This can be useful as a modal dialog box.
$(img).css("left",parseInt(viewportWidth()/2) - 100);//note: "100" is half the width of the target div.
$(img).css("top",parseInt(viewportHeight()/2) - 100);//note: "100" is half the height of the target div.
}
- 1. 在不同屏幕上定位位圖
- 2. 在動畫div中定位圖像 - 粘貼到屏幕左側
- 3. 如何設置定位屏幕「肖像」
- 4. Bootstrap Carousel在屏幕圖像
- 5. 圖像在屏幕全
- 6. 在屏幕中心定位div元素
- 7. 滾動android屏幕上的圖像屏幕上的圖像
- 8. 指定圖像覆蓋的屏幕位置
- 9. Android的圖像定位支持多種屏幕分辨率
- 10. 在沒有JavaScript的屏幕中間定位彈出窗口
- 11. 將光標固定在屏幕中間?
- 12. 屏幕中間的位置div w/jQuery
- 13. UIScrollView - 檢測圖像是在屏幕中間
- 14. 使用ScaleAnimation在屏幕中間縮放圖像
- 15. 機器人:在屏幕中間顯示的圖像編程
- 16. Android的一個PNG屏幕圖像與位於屏幕上的EditTexts
- 17. Tranparent屏幕圖像Silverlight
- 18. iPhone全屏幕圖像
- 19. Android屏幕設計圖像
- 20. 圖像比屏幕大
- 21. SDL圖像分割屏幕
- 22. 屏幕圖像沒有出現在屏幕上
- 23. 在屏幕的特定位置繪製自定義視圖 - Android
- 24. 頁腳在屏幕中間
- 25. 32位bmp圖像不顯示在pygame的屏幕上
- 26. 屏幕+覆蓋中間的中心圖像
- 27. 如何在屏幕上定位元素?
- 28. 當我拍攝屏幕圖像時,圖像的位置在哪裏?
- 29. 自定義Android應用程序屏幕圖像是在應用程序屏幕之間切換可見
- 30. 定位我的圖像滑塊在屏幕上,沒有它自動調整
使用javascript?這很奇怪。這不能用CSS快速完成嗎? – tr4656 2010-08-23 04:28:38