我正在尝试使用媒体查询根据屏幕大小更改图像src.我试过背景:url(x); 但它不起作用.我在某处读到了我应该使用内容:url(x)代替,但是当我这样做时,我得到一个空白页面.谁能告诉我我的代码有什么问题?
HTML:
<div class="container" id="at-header">
<img class="image" id="logo" src="img/logo_white.png" />
<img class="image" id="main-img" src="img/desktop_homepage.jpg" />
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
@media screen and (max-width: 767px){
#main-img{
content:url("img/mobile_homepage.jpg");
}
}
@media screen and (min-width: 768px) {
#main-img{
content:url("img/tablet_homepage.jpg");
}
}
@media (min-width: 992px){
#main-img{
content:url("img/desktop_homepage.jpg");
}
}
@media (min-width: 1200px) {
#main-img{
content:url("img/desktop_homepage.jpg");
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下显示图像的HTML代码:
<div>
<img id="wm01" alt="PP" title="PP" u="image" src="theImages/wm01.jpg" />
</div>
Run Code Online (Sandbox Code Playgroud)
我要做的是根据屏幕大小显示不同的图像.首先我用CSS隐藏图像:
#wm01 {
display: none;
}
Run Code Online (Sandbox Code Playgroud)
然后在我的身体中,我添加以下代码:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
if (x<568) {
//alert(x);
document.getElementById("wm01").src="theImages/wm01_app.jpg";
document.getElementById("wm01").style.display = "block";
}
else {
document.getElementById("wm01").src="theImages/wm01.jpg";
document.getElementById("wm01").style.display = "block";
}
Run Code Online (Sandbox Code Playgroud)
图像未显示在任何尺寸的屏幕中.我该如何解决?