2013-07-19 40 views
-1

如何使图像适合屏幕(不是完整的背景图像),以便在向下滚动时显示其他内容?图像覆盖页面加载的窗口

什么我谈论的例子是在这里:

http://www.microsoft.com/visualstudio/eng/products/visual-studio-professional-2012

在那个例子看,你调整屏幕和向下滚动影像如何反应。

我期待您的回复。

谢谢。 PS:如果你也可以发送它的演示,那真的很有帮助。谢谢!

+2

你试过什么?你打算用什么来实现这个目标? – Seth

+0

嗯,我试图只是将图像的宽度设置为100%,但它仍然达到高度明智的效果...不知道如果图像必须是一个特定的大小或如果它必须做一个jQuery插件.. –

+1

请在Google Chrome浏览器中按F12键,同时访问该页面,您可以看到您需要的一切。 –

回答

0

下JS应该有所帮助:

function resizeBackground() 
{ 
    var targetWidth; 
    var targetHeight; 

    if (typeof window.innerWidth != 'undefined') 
    { 
     targetWidth = window.innerWidth, 
     targetHeight = window.innerHeight 
    } 
    else if (typeof document.documentElement != 'undefined'&& typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) 
    { 
     targetWidth = document.documentElement.clientWidth, 
     targetHeight = document.documentElement.clientHeight 
    } 
    else 
    { 
     targetWidth = document.getElementsByTagName('body')[0].clientWidth, 
     targetHeight = document.getElementsByTagName('body')[0].clientHeight 
    } 

    var background = new Image(); 
    background.src = document.getElementById("backgroundImage").src; 
    var target = document.getElementById("backgroundImage"); 
    var sourceWidth = background.width; 
    var sourceHeight = background.height; 

    if (targetWidth/sourceWidth > targetHeight/sourceHeight) 
    { 
     target.width = targetWidth; 
     target.height = sourceHeight * (targetWidth/sourceWidth); 
    } 
    else 
    { 
     target.width = sourceWidth * (targetHeight/sourceHeight); 
     target.height = targetHeight; 
    } 
} 
window.onload = resizeBackground; 
window.onresize = resizeBackground; 

然后插入DIV无论你想你的图像像这样:

<div id= 'backgroundimage'></div> 

最后CSS:

#backgroundimage {background-image:url(image.jpg); width:100%;} 
+0

嗨Liane,当我执行您的代码并在线查看它时,图像没有显示 –

+0

既不适用于我,也不适用于Firefox v22。 –

+1

@SavionSmith看看下面的链接,这可能有帮助,它是**纯CSS **:http://voormedia.com/blog/2012/11/responsive-background-images-with-fixed-or-fluid -aspect-比率 –