当前位置: 代码网 > it编程>编程语言>Javascript > vue实现背景图片铺满整个屏幕(适配所有机型)

vue实现背景图片铺满整个屏幕(适配所有机型)

2024年10月28日 Javascript 我要评论
vue背景图片铺满整个屏幕背景全屏,不用定位内容完全显示且可滚动查看html:<div id="home"></div>css:<style>#home { wi

vue背景图片铺满整个屏幕

背景全屏,不用定位内容完全显示且可滚动查看

html:
<div id="home"></div>

css:
<style>
#home {
  width: 100%;
  min-height: 100vh;
  background: url("~@/images/home/h_bg.png") center center no-repeat;
  background-size: 100% 100%;
  
}
</style>

固定一屏大小,内容超出一屏会显示不全,不可滚动查看内容

html:
<div id="home"></div>

css:
<style>
#home {
  width: 100%;
  height: 100vh;
  background: url("~@/images/home/h_bg.png") center center no-repeat;
  background-size: 100% 100%;
  position: fixed;//固定定位
}
</style>

背景全屏,内容完全显示且可滚动查看

html:
<div id="home"></div>

css:
<style>
#home {
  width: 100%;
  height: 100vh;
  background: url("~@/images/home/h_bg.png") center center no-repeat;
  background-size: 100% 100%;
  position:absolute;//绝对定位
}
</style>

vue创建铺满整个页面的背景图

最近在做登陆页面时,遇到背景图不能完全展示,图片会自动切割重复显示等情况。经过一番搜索大佬的方法后终于发现解决方法。

网上一搜都是一堆的复杂方法,经过精简之后 代码如下。

<!-- 布局容器 -->
 <div id="all"></div>

使用top和left去除默认和浏览器的边距,设置背景图重复方式为no-repeat不重复

设置宽度高度为100%盛满全屏 设置固定位置 不跟随浏览器比例变化而变化

    <style>
        #all {
          top: 0;
          left: 0;
          background: url("@/assets/login/login_bg.jpg") no-repeat;
          background-size: 100% 100%;
          width: 100%;
          height: 100%;
          position: fixed;
        }
    </style>

总结

总结以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com