2009-12-30 48 views
0

http://grab.by/1rI8圖像不反映它的特性

我寫了一些jQuery的腳本,使背景圖像總是會擴展,永遠不會顯示一個空白點。

function runIt() { 
currentHeight = $("#bg").height() //useless for you 
currentWidth = $("#bg").width() //useless for you 
ccurrentHeight = $("#cover").height() //useless for you 
ccurrentWidth = $("#cover").width() //useless for you 
wcurrentHeight = $(window).height() //useless for you 
wcurrentWidth = $(window).width() //useless for you 
$("#auto").empty() //useless for you 
$("#auto").append('BG Current Height:"'+currentHeight+'"Width"'+currentWidth+'"<br \>') //useless for you 
$("#auto").append('Cover Current Height:"'+ccurrentHeight+'"Width"'+ccurrentWidth+'"<br \>') //useless for you 
$("#auto").append('Window Current Height:"'+wcurrentHeight+'"Width"'+wcurrentWidth+'"') //useless for you 
if ($("#bg").height() < $(window).height()) { 
// $("#bg").empty() //useless for you 
// $("#bg").append('<img src="'+imgPath+'" style="height:100%" />') //useless for you 
     $("#bg").css({ 
     width: $("#cover").width(), 
     height: "auto" 
     }) 
    } 
else { 
// $("#bg").empty() //useless for you 
// $("#bg").append('<img src="'+imgPath+'" style="width:100%" />') //useless for you 
     $("#bg").css({ 
     height: $("#cover").height(), 
     width: "auto" 
     }) 
    } 
} 
setInterval(runIt, 50); 

任何人有任何想法,爲什麼這不能正常工作?

感謝, 士瑞茲

+2

HTML代碼/適用的CSS本身將是一個很好的補充這個問題... – jchapa 2009-12-30 01:16:40

回答

0
function runIt() { 
winprop = $(window).height()/$(window).width(); 
var bgprop = $("#bgimg").height()/$("#bgimg").width(); 
if(winprop < bgprop) 
{ 
        $("#bgimg").css({ 
        width: $(window).width(), 
        height: "auto" 
        }) 
} else { 
        $("#bgimg").css({ 
        width: "auto", 
        height: $(window).height() 
        }) 
   } 
} 
function doSomething() { 
    alert("I'm done resizing for the moment"); 
}; 
runIt(); 
var resizeTimer = null; 
$(window).bind('resize', function() { 
    if (resizeTimer) clearTimeout(resizeTimer); 
    resizeTimer = setTimeout(runIt, 20); 
}); 

給這個背景圖片到自己的ID。改變圖像的大小,而不是div。