如何在移动设备中更改背景图像
How to change background image in mobile?
>我正在开发一个简单的带有背景图像的html页面。在桌面上,背景显示完美,但我正在尝试更改移动设备上的背景图像。
html 和 css 如下:
.HTML
<div class="pc-img" ></div>
<div class="mobile-img" ></div>
.CSS
body
{
margin:0px;
padding: 0px;
}
.pc-img {
min-height: 95% !important;
height: 100%;
width: 100%;
background-image: url('http://www.laminaresearchcenter.com/images/comingsoon.png');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
text-align: center;
color: white;
}
}
.mobile-img
{
display: none;
}
@media only screen and (max-width: 384px) {
.mobile-img
{
background: url('http://www.outbarga.in/images/comingsoon.jpg');
visibility: visible;
}
.pc-img
{
visibility: hidden;
}
}
你不需要 2 个类和 2 个div 来做到这一点。您可以为一个div 设置@media查询,并更改其属性。
.CSS:
.pc-img {
width: 100%;
height: 400px;
background: #000;
}
@media screen and (min-width: 100px) and (max-width: 480px) {
.pc-img {
background: purple;
}
}
@media screen and (min-width: 480px) and (max-width: 768px) {
.pc-img {
background: yellow;
}
}
查看此演示:演示 1
顺便说一句,如果你想要两个div,请制作这个:演示2
.pc-img {
width: 100%;
height: 400px;
background: #000;
}
.device-img {
background: steelblue;
width: 100%;
height: 400px;
}
@media screen and (min-width: 100px) and (max-width: 480px) {
.device-img {
display: block;
}
.pc-img {
display: none;
}
}
@media screen and (min-width: 480px) and (max-width: 768px) {
.device-img {
display: block;
}
.pc-img {
display: none;
}
}
您必须在媒体查询中为类提供display:block
mobile-img
pc-img
类display:none
@media only screen and (max-width: 384px) {
.mobile-img
{
display: block;
background: url('http://www.outbarga.in/images/comingsoon.jpg');
visibility: visible;
}
.pc-img {
display: none;
}
响应式页面
body
{
margin:0px;
padding: 0px;
}
.pc-img {
min-height: 95% !important;
height: 100%;
width: 100%;
background-image: url('http://www.laminaresearchcenter.com/images/comingsoon.png');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
text-align: center;
color: white;
}
}
.mobile-img
{
display: none;
}
@media only screen and (max-width: 384px) {
.mobile-img
{
background: url('http://www.outbarga.in/images/comingsoon.jpg');
display: block;
}
.pc-img
{
display: none;
}
}
<div class="pc-img" ></div>
<div class="mobile-img" ></div>
使用display:none
和display:block
属性代替visibility
.mobile-img{display:none;}
@media only screen and (max-width: 384px) {
.mobile-img {
display: block;
background-image: url('http://www.outbarga.in/images/comingsoon.jpg');
}
.pc-img {
display: none;
}
让它background-image
,而不仅仅是background
@media only screen and (max-width: 384px) {
.mobile-img
{
background-image: url('http://www.outbarga.in/images/comingsoon.jpg');
visibility: visible;
}
}
相关文章:
- 可以在这里为背景图像设置滤镜吗
- 使用 js 将背景图像设置为 HTML
- 如何使用css动画/javascript使具有背景图像的元素出现
- 循环浏览多个身体背景图像
- 背景图像顶部的滚动图像不移动
- 在pdfmake中嵌入背景图像
- 修复选择菜单时的背景图像
- 使用css()设置背景图像;不起作用
- 鼠标悬停时如何居中放大背景图像
- 强制谷歌为javascript背景图像编制索引
- 淡入不带HTML的背景图像
- 具有淡入淡出效果的全背景图像
- 未加载随机背景图像
- 背景图像前显示Javascript警报
- javascript背景图像位置循环
- CSS动画背景图像的过渡越来越暗
- "Ken Burns效应”;悬停背景图像
- 通过ID JavaScript显示随机背景图像
- JavaScript背景图像
- 带有图像和背景图像的ClipTo