如何阻止背景图像在屏幕调整大小时失去质量/模糊

How to stop background image from losing quality/ blurring on screen resize

当我创建网站时,我注意到我的背景由于屏幕尺寸而变得模糊 我希望停止这种情况,这样我的网站就会更快乐,访问者也会更好。此外,我的背景在 bg 的原始图像尺寸上看起来不错。这是我的代码和代码笔。

function openNav() {
  document.getElementById("myNav").style.width = "100%";
}

function closeNav() {
  document.getElementById("myNav").style.width = "0%";
}
body {
  background-image: url("https://i.dlpng.com/static/png/6750263_preview.png");
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  font-family: "Lato", sans-serif;
}

.overlay {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.9);
  overflow-x: hidden;
  transition: 0.5s;
}

.overlay-content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.overlay a:hover,
.overlay a:focus {
  color: #f1f1f1;
}

.overlay .closebtn {
  position: absolute;
  top: 20px;
  right: 45px;
  font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay a {
    font-size: 20px;
  }
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
<!doctype html>
<html>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span>
<div id="myNav" class="overlay">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">Home</a>
    <a href="#">Shop</a>
    <a href="#">My Projects</a>
    <a href="#">Contact</a>
    <a href="#">About</a>
  </div>
</div>

</html>

https://codepen.io/Javscript/pen/XWMwNYN?editors=1000

您可以在 CSS 中使用媒体查询来加载适合用户屏幕尺寸的较大图像。

body {
  background-image: url("/image.png");
}

@media screen and (min-width: 640px) {
  body {
    background-image: url("/image-small.png");
  }
}

@media screen and (min-width: 768px) {
  body {
    background-image: url("/image-medium.png");
  }
}

@media screen and (min-width: 1024px) {
  body {
    background-image: url("/image-large.png");
  }
}

您需要托管多种尺寸的图像才能正常工作。

您可以在此处阅读有关媒体查询的更多信息:https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries