在 Firefox 中将背景拉伸至 100%

发布于 2024-10-22 06:57:19 字数 1440 浏览 6 评论 0原文

我目前正在使用一个 javascript 背景脚本,该脚本可以更改计时器上的背景,并且参考白天和黑夜,这效果很好,但我正在尝试使背景拉伸到浏览器宽度的 100%,我已经这样做了通过CSS。它可以在 safari 和 chrome 中工作,但不能在 firefox 中工作(也许 IE 也是如此),而是以 100% 的尺寸显示图像,有人可以帮忙吗?

我不确定是否有一个可以与 body 一起使用的 hack,或者我必须将 javascript 重写为 div 或其他内容,但不知道如何操作,因此非常感谢您的帮助!

JavaScript

    <script language="JavaScript1.2">

//Specify background images to slide
var bgslides=new Array()

var currentTime = new Date().getHours();
if (9 <= currentTime && currentTime < 18) {
   bgslides[0]="images/webcam1.jpg"
   bgslides[1]="images/webcam2.jpg"
   bgslides[2]="images/webcam3.jpg"
}
else
{
   bgslides[0]="images/webcamnight.jpg"
   bgslides[1]="images/webcamnight.jpg"
   bgslides[2]="images/webcamnight.jpg"
}

//Specify interval between slide (in miliseconds)
var speed=5000

//preload images
var processed=new Array()
for (i=0;i<bgslides.length;i++){
processed[i]=new Image()
processed[i].src=bgslides[i]
}

var inc=-1

function slideback(){
if (inc<bgslides.length-1)
inc++
else
inc=0
document.body.background=processed[inc].src
}

if (document.all||document.getElementById)
window.onload=new Function('setInterval("slideback()",speed)')
window.onload=new Function('setInterval("slideback()",speed)')

</script>

和 CSS

  body{

background-position: 0 0;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100%


}

I am currently working with a javascript background script that changes the background on a timer and and in reference to day and night this works great but I'm trying to make the background stretch to 100% of the browser width, I've done this through CSS. It works in safari and chrome but not firefox (maybe IE too) instead it see's the 100% as showing the image at 100% of it's size, can anybody help?

I'm not sure if there is a hack to work with body or I'll have to rewrite the javascript to a div or something but not sure how, so any help is appreciated!

The javascript

    <script language="JavaScript1.2">

//Specify background images to slide
var bgslides=new Array()

var currentTime = new Date().getHours();
if (9 <= currentTime && currentTime < 18) {
   bgslides[0]="images/webcam1.jpg"
   bgslides[1]="images/webcam2.jpg"
   bgslides[2]="images/webcam3.jpg"
}
else
{
   bgslides[0]="images/webcamnight.jpg"
   bgslides[1]="images/webcamnight.jpg"
   bgslides[2]="images/webcamnight.jpg"
}

//Specify interval between slide (in miliseconds)
var speed=5000

//preload images
var processed=new Array()
for (i=0;i<bgslides.length;i++){
processed[i]=new Image()
processed[i].src=bgslides[i]
}

var inc=-1

function slideback(){
if (inc<bgslides.length-1)
inc++
else
inc=0
document.body.background=processed[inc].src
}

if (document.all||document.getElementById)
window.onload=new Function('setInterval("slideback()",speed)')
window.onload=new Function('setInterval("slideback()",speed)')

</script>

and the css

  body{

background-position: 0 0;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100%


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-10-29 06:57:19

尝试使用:

background-size: cover;

background-size: 100% cover;

try using:

background-size: cover;

or

background-size: 100% cover;
伤痕我心 2024-10-29 06:57:19
background-size: 100% 100%; /* w3 spec - no browser supports it yet */
-moz-background-size: 100% 100%; /* used for firefox */
-o-background-size: 100% 100%; /* used for opera */
-webkit-background-size: 100% 100%; /* used for safari and chrome */
background-size: 100% 100%; /* w3 spec - no browser supports it yet */
-moz-background-size: 100% 100%; /* used for firefox */
-o-background-size: 100% 100%; /* used for opera */
-webkit-background-size: 100% 100%; /* used for safari and chrome */
烈酒灼喉 2024-10-29 06:57:19

虽然此属性是 Gecko 1.9.2 (Firefox 3.6) 中的新属性,但在 Firefox 3.5 中可以使用 -moz-border-image 将图像完全拉伸到背景上。

    .foo {
           background-image: url(bg-image.png);

           -moz-background-size: 100% 100%;         /* Gecko 1.9.2 (Firefox 3.6) */
           -o-background-size: 100% 100%;           /* Opera 9.5 */
           -webkit-background-size: 100% 100%;      /* Safari 3.0 */
           background-size: 100% 100%;              /* Gecko 2.0 (Firefox 4.0) and other CSS3-compliant browsers */

           -moz-border-image: url(bg-image.png) 0;  /* Gecko 1.9.1 (Firefox 3.5) */
    }

您还可以尝试假拉伸背景:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
       <head>
           <title>Stretched image in background</title>

           <style type="text/css">
              body, html { margin: 0; padding: 0 }
              #bgimg { position:absolute; z-index:-1; width:100%; height:100% }
           </style>

      </head>
      <body>

         <img id="bgimg" src="http://www.lcvm.nl/images/nieuwsbrief/illusie2.gif" />
         <!-- content will go here -->

      </body>
  </html>

通过 dom 您可以更改 img。

While this property is new in Gecko 1.9.2 (Firefox 3.6), it is possible to stretch a image fully over the background in Firefox 3.5 by using -moz-border-image .

    .foo {
           background-image: url(bg-image.png);

           -moz-background-size: 100% 100%;         /* Gecko 1.9.2 (Firefox 3.6) */
           -o-background-size: 100% 100%;           /* Opera 9.5 */
           -webkit-background-size: 100% 100%;      /* Safari 3.0 */
           background-size: 100% 100%;              /* Gecko 2.0 (Firefox 4.0) and other CSS3-compliant browsers */

           -moz-border-image: url(bg-image.png) 0;  /* Gecko 1.9.1 (Firefox 3.5) */
    }

You can also try to fake stretching a background:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
       <head>
           <title>Stretched image in background</title>

           <style type="text/css">
              body, html { margin: 0; padding: 0 }
              #bgimg { position:absolute; z-index:-1; width:100%; height:100% }
           </style>

      </head>
      <body>

         <img id="bgimg" src="http://www.lcvm.nl/images/nieuwsbrief/illusie2.gif" />
         <!-- content will go here -->

      </body>
  </html>

By way of dom you can change the img.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文