gpt4 book ai didi

javascript - CSS 图像没有覆盖整个屏幕

转载 作者:行者123 更新时间:2023-11-28 05:53:05 29 4
gpt4 key购买 nike

我希望我的 css 图像覆盖整个屏幕,我从 JS 数组中随机选择一个图像。它显示图像,但未显示完美的整页背景图像。

ma​​in.html

<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body>

<button id = "button1">Click</button>


<script src = "main.js"></script>
</body>
</html>

ma​​in.js

    document.getElementById("button1").addEventListener("click", function(){
showImage();
});
...
function showImage(){
document.write('<img class = "bg" src="'+theImages[whichImage]+'">');
}

ma​​in.css

img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;

/* Set up proportionate scaling */
width: 100%;
height: auto;

/* Set up positioning */
position: fixed;
top: 0;
left: 0;
}

@media screen and (max-width: 1024px) { /* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px; /* 50% */
}
}

最佳答案

那是什么CSS?我也不建议使用 vw/vh。

你最好使用标准的背景尺寸和位置,比如

/*background-image: url(../images/background.png);*/ optional
background-size: cover;
background-repeat: no-repeat;
background-position: left top;

你可以随时添加

-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;

为了全面的跨浏览器兼容性

关于javascript - CSS 图像没有覆盖整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217968/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com