简体   繁体   English

将背景图像高度设置为视口高度

[英]Set background image height to viewport height

In my Nuxt.js application using Vuetify.js , I want to set a background image for the whole content:在我使用Vuetify.js Nuxt.js应用程序中,我想为整个内容设置背景图像:

    <v-content>
      <v-img
        src="https://picsum.photos/id/11/500/300"
      >
        <v-container justify-center fill-height>
          <v-row justify="center" align="center">
            <v-col cols="12">
              <nuxt />
            </v-col>
          </v-row>
        </v-container>
      </v-img>
    </v-content>

The problem is that the content is not centered vertically because the image takes the full height of the window, thus I need to scroll down to see the content.问题是内容没有垂直居中,因为图像占据了窗口的整个高度,因此我需要向下滚动才能看到内容。

Is there a way to set the image's height to exactly that of the viewport's height in order to see my content centered vertically and the window does not net scroll down because of the image ?有没有办法将图像的高度设置为视口的高度,以便看到我的内容垂直居中,并且窗口不会因为图像而向下滚动?

If the content of the page is long, I should be able to scroll down and still see the image set as the background of the page如果页面内容很长,我应该能够向下滚动并仍然看到设置为页面背景的图像

Minimalist demo code on Github in case you have time to run it. Github 上的极简演示代码,以防您有时间运行它。

Not really a vue-javascript solution but a CSS one.不是真正的 vue-javascript 解决方案,而是 CSS 解决方案。 The following setup will result in a fullpage background image and the content being vertically aligned without a scrollbar.以下设置将导致整页背景图像和内容在没有滚动条的情况下垂直对齐。

EDIT: Use static asset编辑:使用静态资产

In your template:在您的模板中:

<v-content 
  :style="`background-image: url(${require('../assets/image.jpg')})`" 
  class="fill-height bg-image">
  <v-container justify-center fill-height>
    <v-row justify="center" align="center">
      <v-col cols="12" justify="center" style="border: 1px solid;">
        <nuxt />
      </v-col>
    </v-row>
  </v-container>
</v-content>

And in your styles:在你的风格中:

<style>
 .bg-image {
   background-repeat: no-repeat;
   background-position: center center;
   background-attachment: fixed;
   background-size: cover;
 }
</style>

Height:100vh maybe?高度:100vh 也许?

vw Relative to 1% of the width of the viewport* vw 相对于视口宽度的 1%*
vh Relative to 1% of the height of the viewport* vh 相对于视口高度的 1%*

https://www.w3schools.com/cssref/css_units.asp https://www.w3schools.com/cssref/css_units.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM