html5怎么设置整页背景图片

html-css06

html5怎么设置整页背景图片,第1张

html5中设置整页背景图片的方法是利用css3样式:

写法如下:

img.bg {

 /* Set rules to fill background */

 min-height: 100%

 min-width: 1024px

 /* Set up proportionate scaling */

 width: 100%

 height: auto

 /* Set up positioning */

 position: fixed

 top: 0

 left: 0

}

@media screen and (max-width: 1024px) { /* Specific to this particular image */

 img.bg {

   left: 50%

   margin-left: -512px  /* 50% */

 }

}

效果如:

第一步:准备工作

先准备好一个视频

第二步:引入视频

这里我们需要用到了video/标签,然后在source里面写视频的路径,autoplay用来使其自动播放,muted用来使其静音,loop为循环播放

<video id="v1" autoplay muted loop style="width: 100%">    <source  src="mp4/loading.mp4">

</video>

第三步:调节视频,使其适应屏幕

以上的步骤还不能满足我们的需求,这个时候我们会发现有滚动条,而且视频会遮挡我们要显示的内容,可这远远不是我们想要的结果啊,不怕,有我呢

    video{          position: fixed          right:0          bottom: 0          min-width: 100%          min-height: 100%          width: auto          height: auto          z-index: -9999          /*灰色调*/          /*-webkit-filter:grayscale(100%)*/      }

这样就OK了,-webkit-filter:grayscale(100%)为调节会色调的,同样适用于img

第四步:视频播放速度

可以利用video的playbackRate属性来控制video的播放速度,如果要让背景视频以慢速播放的话可以加上下面的javascript

<script>    var video= document.getElementById('v1')    video.playbackRate = 0.5</script>