我对html没有太多经验,但是我需要在我的网站上设置图片幻灯片。我在w3schools上找到了这段代码,但是在添加媒体查询后,它在图像之间显示了一个空白屏幕。我如何摆脱它,而仅拥有背对背的图像?谢谢
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none;}
@media only screen and (min-width: 480px) {
#web {
display: none;
}
}
@media only screen and (max-width: 480px) {
#mobile {
display: none;
}
}
</style>
</head>
<body>
<div class="w3-content w3-section" id="web">
<img class="mySlides" src="https://www.91-img.com/pictures/126849-v6-honor-10-mobile-phone-large-1.jpg" style="width:100%">
<img class="mySlides" src="https://static.toiimg.com/photo/64428999/Vivo-NEX.jpg" style="width:100%">
</div>
<div class="w3-content w3-section" id="mobile">
<img class="mySlides" src="https://japan-magazine.jnto.go.jp/jnto2wm/wp-content/uploads/1603_iwate_main.jpg" style="width:100%">
<img class="mySlides" src="https://image.shutterstock.com/image-photo/panoramic-view-idyllic-mountain-scenery-260nw-622792952.jpg" style="width:100%">
</div>
<script>
var myIndex = 1;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 1; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 2000);
}
</script>
</body>
</html>
答案 0 :(得分:0)
所有索引均以0开头。因此,将for
循环中的初始索引更改为0并使其起作用。另外,您还必须在移动版本中更改图像的类名(这就是为什么在显示一些图像后出现空白页的原因),并且实现了错误的媒体查询。
var myIndex = 0;
carousel();
function carousel() {
var i;
if(screen.width <= 480){
var x = document.getElementsByClassName("mySlides_mobile");
}else{
var x = document.getElementsByClassName("mySlides");
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 2000);
}
.mySlides, .mySlides_mobile {
display:none;
}
@media only screen and (max-width: 480px) {
#web {
display: none;
}
}
@media only screen and (min-width: 480px) {
#mobile {
display: none;
}
}
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div class="w3-content w3-section" id="web">
<img class="mySlides" src="https://www.91-img.com/pictures/126849-v6-honor-10-mobile-phone-large-1.jpg" style="width:100%">
<img class="mySlides" src="https://static.toiimg.com/photo/64428999/Vivo-NEX.jpg" style="width:100%">
</div>
<div class="w3-content w3-section" id="mobile">
<img class="mySlides_mobile" src="https://japan-magazine.jnto.go.jp/jnto2wm/wp-content/uploads/1603_iwate_main.jpg" style="width:100%">
<img class="mySlides_mobile" src="https://image.shutterstock.com/image-photo/panoramic-view-idyllic-mountain-scenery-260nw-622792952.jpg" style="width:100%">
</div>
</body>
</html>