使用css relative和absolute

时间:2016-10-01 20:47:13

标签: html css css-position

我正试图在div中将图像无形地叠加在一起,以便您可以在它们之间切换。我有javascript工作但是在理解各种静态/绝对/相对css属性时遇到了问题。

我试图让它成为页面标题,几行解释性文字,一些切换控件然后是图像。 在各种图像ID的定义中,我指定了z顺序,因此图像可以堆叠在imagewrapper div中。问题在于imagewrapper和overlayimage的定义。在我的电脑上它看起来很好,但使用“顶部”和“左”意味着在手机或手机上观看时文字会被遮挡。有人可以建议一个解决方法吗?

到目前为止,完整的代码如下 - 它只是导致问题的定位;许多变量和函数目前只是存根:

var nt = 0;

function togglent() {

  if (nt == 0) {
    document.getElementById("nighttime").style.visibility = "visible";
    document.getElementById("nighttime").style.opacity = 1;
    nt = 1;
  } else {
    document.getElementById("nighttime").style.visibility = "visible";
    document.getElementById("nighttime").style.opacity = document.getElementById("ntSlider").value;
    nt = 0;
  }

  // when clicked, switch back and forth between nighttime; if nt=1, the nighttime needs to be from the slider value



}

function handleClick(cb) {

    var myInterval = setInterval(function() {
      var imgmod = cb.value + "24";

      if (cb.checked) {
        document.getElementById(cb.value).style.opacity = parseFloat(document.getElementById(cb.value).style.opacity) + 0.1;

        if (cb.value != "backdrop" && cb.value != "aurora") {
          document.getElementById(imgmod).style.opacity = parseFloat(document.getElementById(imgmod).style.opacity) + 0.1;
        }

        if (document.getElementById(cb.value).style.opacity >= 1) {
          document.getElementById(cb.value).style.opacity = 1;

          if (cb.value != "backdrop" && cb.value != "aurora") {
            document.getElementById(imgmod).style.opacity = 1;
          }

          clearInterval(myInterval);
        }
      } else {
        document.getElementById(cb.value).style.opacity = parseFloat(document.getElementById(cb.value).style.opacity) - 0.1;
        if (cb.value != "backdrop" && cb.value != "aurora") {
          document.getElementById(imgmod).style.opacity = parseFloat(document.getElementById(imgmod).style.opacity) - 0.1;
        }

        if (document.getElementById(cb.value).style.opacity <= 0) {
          document.getElementById(cb.value).style.opacity = 0;

          if (cb.value != "backdrop" && cb.value != "aurora") {
            document.getElementById(imgmod).style.opacity = 0;
          }

          clearInterval(myInterval);
        }
      }

    }, 100);

  } // end of handleClick

function showSliderValue(sl) {

  if (sl > 0) {
    document.getElementById("nighttime").style.visibility = "visible";
    document.getElementById("nighttime").style.opacity = sl;
  } else if (sl == 0) {
    document.getElementById("nighttime").style.visibility = "visible";
    document.getElementById("nighttime").style.opacity = sl;
  }

}



function alldata(cb) {
  if (cb.checked) {

    document.getElementById("mainindex").style.visibility = "visible";

    document.getElementById("mainindex24").style.visibility = "hidden";

  } else {
    document.getElementById("mainindex").style.visibility = "hidden";

    document.getElementById("mainindex24").style.visibility = "visible";

  }

}
.imageWrapper {
  position: relative;
}
.overlayImage {
  position: absolute;
  top: 330;
  left: 0;
}
#nighttime {
  z-index: 10;  /* An element with greater stack order is always in front of an element with a lower stack order. */
  opacity: 0.0;
  visibility: hidden;
}
#background {
  z-index: -2;
  opacity: 1.0;
  visibility: visible;
}
#backdrop {
  z-index: 0;
  opacity: 0.0;
  visibility: visible;
}
#aurora {
  z-index: 1;
  opacity: 1.0;
  visibility: visible;
}
#mainindex {
  z-index: 3;
  opacity: 1.0;
  visibility: visible;
}
#mainindex24 {
  z-index: 3;
  opacity: 1.0;
  visibility: hidden;
}
input {
  margin: 10px 10px 10px 10px;
}
<div class="imageWrapper" onclick='togglent();'>
  <img id="nighttime" class="overlayImage" src="nt.jpg">
  <img id="background" class="overlayImage" src="background.jpg">
  <img id="backdrop" class="overlayImage" src="Equirectangular_projection_SW.jpg">
  <img id="aurora" class="overlayImage" src="aurora.png">
</div>
<p>

  <form action="">
    <input type="checkbox" onclick='handleClick(this);' value="backdrop" checked><span style="color: black;">Backdrop</span>
    <input type="checkbox" onclick='handleClick(this);' value="aurora" checked>
  </form>

1 个答案:

答案 0 :(得分:0)

在所做的更改中,与OP的问题最相关的是:

  .imageWrapper {
      position: relative;
      height: auto;
      width: 100vw;
    }
    .overlayImage {
      position: absolute;
      top: 10vh;
      left: 0;
    }

使用相对度量单位,例如百分比(%),em s,rem s,视口高度和宽度(vhvw ),我们可以获得足够的弹性,以避免在不同维度的范围内重要区域的重叠。

&#13;
&#13;
<html>

<head>
  <style>
    html,
    body {
      width: 100%;
      height: 100%;
      box-sizing: border-box;
    }
    *,
    *:before,
    *:after {
      box-sizing: inherit;
    }
    .imageWrapper {
      position: relative;
      height: auto;
      width: 100vw;
    }
    .overlayImage {
      position: absolute;
      top: 10vh;
      left: 0;
    }
    #nighttime {
      z-index: 10;
      opacity: 0.0;
    }
    #background {
      z-index: -2;
      opacity: 1.0;
    }
    #backdrop {
      z-index: 0;
      opacity: 0.0;
    }
    #aurora {
      z-index: 1;
      opacity: 1.0;
    }
    #mainindex {
      z-index: 3;
      opacity: 1.0;
    }
    #mainindex24 {
      z-index: 3;
      opacity: 1.0;
    }
    input {
      margin: 10px 0;
      display: none;
    }
    fieldset {
      width: 720px;
      line-height: 1.5;
      min-height: 20px;
    }
    img {
      display: block;
      margin: 0 auto;
      max-width: 720px;
      height: auto;
    }
    label input + span {
      padding: 5px 10px;
      background: rgba(255, 0, 128, .4);
      height: 100%;
      width: 100%;
    }
    label input:checked + span {
      background: rgba(0, 0, 255, .4);
      height: 100%;
      width: 100%;
    }
  </style>
</head>

<body>
  <div class="imageWrapper" onclick='togglent();'>
    <img id="nighttime" class="overlayImage" src="http://orig15.deviantart.net/8697/f/2016/047/5/c/windows_nt_by_gpolydoros-d7k5mry.png">
    <img id="background" class="overlayImage" src="http://vignette3.wikia.nocookie.net/masseffect/images/1/1f/Background.jpg/revision/latest?cb=20121030013132">
    <img id="backdrop" class="overlayImage" src="https://upload.wikimedia.org/wikipedia/commons/8/83/Equirectangular_projection_SW.jpg">
    <img id="aurora" class="overlayImage" src="http://www.luostonporosafarit.fi/media/wwwkuvat/porokuvia_2014/northern_lights.jpg.jpg">
  </div>

  <form action="">
    <fieldset>
      <label>Back
        <input type="checkbox" onclick='handleClick(this);' value="backdrop" checked>
        <span></span>
      </label>


      <label>drop
        <input type="checkbox" onclick='handleClick(this);' value="aurora" checked>
        <span></span>
      </label>
    </fieldset>
  </form>
  <script>
    var nt = 0;

    function togglent() {

      if (nt === 0) {
        document.getElementById("nighttime").style.visibility = "visible";
        document.getElementById("nighttime").style.opacity = 1;
        nt = 1;
      } else {
        document.getElementById("nighttime").style.visibility = "visible";
        document.getElementById("nighttime").style.opacity = document.getElementById("ntSlider").value;
        nt = 0;
      }

      // when clicked, switch back and forth between nighttime; if nt=1, the nighttime needs to be from the slider value



    }

    function handleClick(cb) {

        var myInterval = setInterval(function() {
          var imgmod = cb.value + "24";

          if (cb.checked) {
            document.getElementById(cb.value).style.opacity = parseFloat(document.getElementById(cb.value).style.opacity) + 0.1;

            if (cb.value != "backdrop" && cb.value != "aurora") {
              document.getElementById(imgmod).style.opacity = parseFloat(document.getElementById(imgmod).style.opacity) + 0.1;
            }

            if (document.getElementById(cb.value).style.opacity >= 1) {
              document.getElementById(cb.value).style.opacity = 1;

              if (cb.value != "backdrop" && cb.value != "aurora") {
                document.getElementById(imgmod).style.opacity = 1;
              }

              clearInterval(myInterval);
            }
          } else {
            document.getElementById(cb.value).style.opacity = parseFloat(document.getElementById(cb.value).style.opacity) - 0.1;
            if (cb.value != "backdrop" && cb.value != "aurora") {
              document.getElementById(imgmod).style.opacity = parseFloat(document.getElementById(imgmod).style.opacity) - 0.1;
            }

            if (document.getElementById(cb.value).style.opacity <= 0) {
              document.getElementById(cb.value).style.opacity = 0;

              if (cb.value != "backdrop" && cb.value != "aurora") {
                document.getElementById(imgmod).style.opacity = 0;
              }

              clearInterval(myInterval);
            }
          }

        }, 100);

      } // end of handleClick

    function showSliderValue(sl) {

      if (sl > 0) {
        document.getElementById("nighttime").style.visibility = "visible";
        document.getElementById("nighttime").style.opacity = sl;
      } else if (sl == 0) {
        document.getElementById("nighttime").style.visibility = "visible";
        document.getElementById("nighttime").style.opacity = sl;
      }

    }



    function alldata(cb) {
      if (cb.checked) {

        document.getElementById("mainindex").style.visibility = "visible";

        document.getElementById("mainindex24").style.visibility = "hidden";

      } else {
        document.getElementById("mainindex").style.visibility = "hidden";

        document.getElementById("mainindex24").style.visibility = "visible";

      }

    }
  </script>
</body>

</html>
&#13;
&#13;
&#13;