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



当我创建一个网站时,我注意到我的背景因为屏幕大小而模糊我希望停止这种情况,这样我就可以更快乐地使用我的网站,访问者也会更好。此外,我的背景在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

最新更新