html - 如何根据屏幕尺寸将背景图像更改为背景视频,可能使用 Bootstrap 4

标签 html css sass background

我的部分有背景图片,但当屏幕宽度超过 768 像素时,我需要将此图片更改为响应式背景视频。我知道我可以使用媒体查询来做到这一点,但是如何将背景图像更改为响应式背景视频?

我的代码如下:

  .welcome {
      height: 100vh;
      background: url("../images/welcome-bg.png") no-repeat center center;
      background-size: cover;
     }
<section class="d-flex welcome align-items-center">
        <div class="container-fluid">
            <h1 class="welcome-text">Welcome To Kramerica Industries</h1>
            <div class="explore-container">
                <a class="v-explore-button" href="#about">V</a>
                <p class="explore-text">Explore</p>
            </div>
        </div>
    </section>

最佳答案

假设您有 2 个 div,一个用于具有类 divImage 的图像,一个用于具有类 divVideo 的视频,然后执行如下操作:

@media (max-width: 768px)
        {
          .welcome {
             height: 100vh;
             background: url("../images/welcome-bg.png") no-repeat center center;
             background-size: cover;
     }
          video {
            display:none;
          }
        
        }
        @media (min-width: 769px) and (max-width : 1024px) {
            .welcome {
             height: 0vh;
             background: url("") no-repeat center center;
            
     }
           video {
              display:block;
            }
        }
<section class="d-flex welcome align-items-center">

 <!-- Video Tag Here -->
   <video id="bgVideo" autoplay loop poster="image.png">
      <!-- Video is embedded in the WEBM format -->
      <source src="video.webm" type="video/webm">
    </video>

        <div class="container-fluid">
            <h1 class="welcome-text">Welcome To Kramerica Industries</h1>
            <div class="explore-container">
                <a class="v-explore-button" href="#about">V</a>
                <p class="explore-text">Explore</p>
            </div>
        </div>
    </section>

关于html - 如何根据屏幕尺寸将背景图像更改为背景视频,可能使用 Bootstrap 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517697/

相关文章:

css - 居中内联表有问题

html5 href 下载属性不适用于 .pdf 文件

html - 电子邮件通讯 : how do I keep line height for headers consistent?

css - 无限背景图像(PNG)动画不无缝

javascript - Svg 元素在悬停时改变位置

ASP.NET/CSS - 无法获得 :active to work

javascript - 为什么我使用 fadeTo 时会产生闪烁效果? (jQuery)

css - 如何让div在一行中正确显示?

html - 我可以将一个网格的元素设置为另一个网格的区域吗?

sass - Webstorm SCSS File Watcher 不尊重父目录