如何在手机中更改背景图片?

时间:2016-01-28 05:51:24

标签: javascript html css media-queries

我正在开发一个带有背景图像的简单html页面。在桌面上,背景显示完美但我正在尝试更改移动设备上的背景图像。

html和css如下:

HTML

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;
}
}

CSS

uses-feature android:name="android.hardware.camera.autofocus"

5 个答案:

答案 0 :(得分:2)

您必须在媒体查询中将display:block提交至mobile-img课程并将display:none提交至pc-img课程

@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;
    }

答案 1 :(得分:2)

您不需要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;
  }
}

看一下这个演示: Demo 1

顺便说一句,如果你想要两个div,请这样做: Demo 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;
  }
}

答案 2 :(得分:1)

响应页面

&#13;
&#13;
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;
}
}
&#13;
<div class="pc-img" ></div>
<div class="mobile-img" ></div>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

使用display:nonedisplay: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;
}

答案 4 :(得分:0)

使其成为background-image,而不仅仅是background

@media only screen and (max-width: 384px) {
    .mobile-img
    {
       background-image: url('http://www.outbarga.in/images/comingsoon.jpg'); 
       visibility: visible;
    }
}