html5怎么设置整页背景图片

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>

大概原理就是将视频的堆叠顺序即z-index设置小一点,可以设置为负值,然后将视频position设为fixed,宽高都设为100%即可,大概代码如下:

video{

position: fixed

right: 0px

bottom: 0px

min-width: 100%

min-height: 100%

height: auto

width: auto

/*加滤镜*/

/*filter: blur(15px)//背景模糊设置 */

/*-webkit-filter: grayscale(100%)*/

/*filter:grayscale(100%)//背景灰度设置*/

z-index:-11

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/6995372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-31
下一篇 2023-03-31

发表评论

登录后才能评论

评论列表(0条)

保存