我是HTML新手。
我的问题是,如何根据媒体查询的大小显示不同的 iframe?
例:for @media (max-width:400px)
加载 iframe01,对于其余大小,加载 iframe02。
有什么想法吗?
我的代码如下:
.container {
position: relative;
width: 100%;
overflow: hidden;
padding-top: 75%; /* 4:3 Aspect Ratio */
}
.responsive-iframe {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
border: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<div class="container">
<iframe class="responsive-iframe" src="iframe_sample"></iframe>
</div>
</body>
</html>
这是可以实现的。
下面是一个示例:
html {
background: blue;
}
.container {
position: relative;
width: 100%;
overflow: hidden;
/* padding-top: 75%; */
/* 4: 3 Aspect Ratio; */
}
.responsive-iframe,
.responsive-iframe2 {
/* position: absolute; */
top: 0;
left: 0;
/* bottom: 0; */
right: 0;
width: 100%;
height: 100%;
border: none;
}
@media only screen and (max-width: 400px) {
html {
background: red;
}
.responsive-iframe {
/* position: absolute; */
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
border: none;
}
.responsive-iframe2 {
display: none;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<div class="container">
<iframe class="responsive-iframe" src="https://www.facebook.com/"></iframe>
<iframe class="responsive-iframe2" src="https://twitter.com/home"></iframe>
</div>
</body>
</html>
iframe 是断开的链接。但是,您可以为每个 iframe 提供单独的 ID 或类,然后在不希望可见的 ID 或类上使用display: none
属性。
我提供的代码显示了桌面格式的两个 iframe。但是,一旦您将页面缩放到 400 像素或更小,它只会显示一个。我还更改了背景颜色以供参考。
希望这能回答你的问题。