【问题标题】:Trouble keeping buttons positioned at the bottom of the image - In responsive image gallery无法将按钮保持在图像底部 - 在响应式图片库中
【发布时间】:2018-02-27 23:26:38
【问题描述】:

我已经制作了一个图片库,并且从现在开始我就一直困扰着这个问题,因为我想将下一个和上一个按钮定位在图像的底部,但是我怎样才能使按钮相对于图像为图像是不同的和变化的。如何将控制器按钮放在图像底部。

我想做的事:

  1. 它具有全屏背景模式。 (完成)
  2. 在该模式上,它会显示图像。 (完成)
  3. 在图像的底部,我想保持一个恒定的显示控制器按钮,这些按钮是下一个和上一个按钮,可以通过图像进行更改。 (两天后卡住了)

尝试到现在:

  1. 尝试制作相对于 div 类“模态内容和图像”的按钮,但根据宽度大小,我的按钮会移动。

  2. 尝试更改 div 的高度和宽度,并尝试了其他一些方法。

  3. 最后什么都没用。我试图通过媒体查询将按钮从 1900 像素一直定位到 400 像素,以不正确的方式实现布局。但它仍然看起来并不好和准确。 (只能使用媒体查询。)

  4. 我知道这不是正确的方法。我在这里想念什么?如何让元素在这种情况下做出响应?

如果我应该尝试任何不同的方法,或者如果我的 CSS 代码出现一些错误,请以您的专业知识告诉我。

function openModal() {
  document.getElementById('myModal').style.display = "block";
}

function closeModal() {
  document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");

  if (n > slides.length) {
    slideIndex = 1;
  }

  if (n < 1) {
    slideIndex = slides.length;
  }

  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }

  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }

  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
}
.photos-thumbnail {
  width: 440px;
}

.photos-thumbnail img {
  margin: 10px 2px 0 0;
  width: 140px;
  height: 100px;
  cursor: pointer;
}

.view-all-photos:hover {
  opacity: 0.7;
}

.column {
  float: left;
  width: 16.6%;
}

.pro-modal {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.9);
}

.modal-content {
  position: relative;
  margin: 50px auto 0;
  width: 100%;
  height: 90%;
  background-color: rgba(0, 0, 0, 0.1);
}

.myslides img {
  object-fit: contain;
  background-color: rgba(0, 0, 0, 0.1);
  width: 100%;
  height: 100%;
}

.mySlides {
  display: none;
}

.column img {
  width: 100%;
}

.controller {
  position: absolute;
  background: linear-gradient(transparent, #1A1A1A);
  text-align: center;
  height: 12%;
  width: 100%;
  padding-top: 20px;
  margin-top: -110px;
}

.prev,
.next {
  position: absolute;
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.4);
  color: #ffffff;
  border: 1px solid #02BFC1;
  border-radius: 50%;
  width: 33px;
  padding: 15px;
  font-size: 25pt;
  transition: 0.6s ease;
  user-select: none;
  -webkit-user-select: none;
}

.next {
  margin-left: 50px;
}

.prev {
  margin-left: -50px;
}
<div class="photos-thumbnail">
  <img class="hover-shadow" onclick="openModal(); currentSlide(1)" src="https://wallpaperscraft.com/image/hitman_xbox_360_absolution_76000_1280x720.jpg">
  <img class="hover-shadow" onclick="openModal(); currentSlide(2)" src="https://www.technobuffalo.com/wp-content/uploads/2014/03/Hitman-Absolution-2-1280x720.jpg">
  <img class="hover-shadow" onclick="openModal(); currentSlide(3)" src="http://fextralife.com/wp-content/uploads/2015/01/Far-Cry-4-Tiger.jpg">
</div>

<div id="myModal" class="pro-modal">
  <div class="modal-content">

    <div onkeydown="ff(e)" class="mySlides">
      <img src="https://wallpaperscraft.com/image/hitman_xbox_360_absolution_76000_1280x720.jpg">
    </div>

    <div onkeydown="ff(e)" class="mySlides">
      <img src="https://www.technobuffalo.com/wp-content/uploads/2014/03/Hitman-Absolution-2-1280x720.jpg">
    </div>

    <div onkeydown="ff(e)" class="mySlides">
      <img src="http://fextralife.com/wp-content/uploads/2015/01/Far-Cry-4-Tiger.jpg">
    </div>

    <div class="controller">
      <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="sep">|</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>

  </div>
</div>

即使在调整大小时我也想要输出

这发生在浏览器窗口调整大小之后。图像将开始适合窗口并根据需要缩小,但按钮将静态位于底部。

【问题讨论】:

    标签: html css responsive-design media-queries image-gallery


    【解决方案1】:

    您可以添加display: flexjustify-content: (wherever in the image you want your buttons to be)text-align 以使您的按钮及其文本居中。

    【讨论】:

    • 不,我不会投反对票。感谢您的回答.. 但是,我不能使用 flex 作为 I.E. 10以下的版本不支持。我们可以做点别的吗?
    【解决方案2】:

    如果您将幻灯片的显示更改为:

    slides[slideIndex - 1].style.display = "inline-grid";
    

    并且对 css 进行一些额外的更改,它应该可以工作。

    绿色标记是我的更改

    我插入了一个较小的图像,以便您可以在下面的代码中看到结果。 希望对你有帮助。

    编辑:

    要调整图片大小,只需添加到您的 css 中:

    img{
      max-width: 100%;
      height: auto;
    }
    

    如果你想让按钮在调整大小时保持在中间,只需将controller div 放入mySlides div -> 参见我的代码 sn-p

    function openModal() {
      document.getElementById('myModal').style.display = "block";
    }
    
    function closeModal() {
      document.getElementById('myModal').style.display = "none";
    }
    
    var slideIndex = 1;
    
    function plusSlides(n) {
      showSlides(slideIndex += n);
    }
    
    function currentSlide(n) {
      showSlides(slideIndex = n);
    }
    
    function showSlides(n) {
      var i;
      var slides = document.getElementsByClassName("mySlides");
      var dots = document.getElementsByClassName("demo");
    
      if (n > slides.length) {
        slideIndex = 1;
      }
    
      if (n < 1) {
        slideIndex = slides.length;
      }
    
      for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
      }
    
      for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
      }
    
      slides[slideIndex - 1].style.display = "inline-grid";
      dots[slideIndex - 1].className += " active";
    }
    .photos-thumbnail {
      width: 440px;
    }
    
    .photos-thumbnail img {
      margin: 10px 2px 0 0;
      width: 140px;
      height: 100px;
      cursor: pointer;
    }
    
    .view-all-photos:hover {
      opacity: 0.7;
    }
    
    .column {
      float: left;
      width: 16.6%;
    }
    
    .pro-modal {
      display: none;
      position: fixed;
      z-index: 1;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.9);
    }
    
    .modal-content {
      text-align: center;
      position: relative;
      width: 100%;
      height: 90%;
      background-color: rgba(0, 0, 0, 0.1);
    }
    
    .mySlides img {
      padding-left:75px;
      object-fit: contain;
      background-color: rgba(0, 0, 0, 0.1);
    }
    
    img{
      max-width: 100%;
      height: auto;
    }
    .mySlides {
      display: none;
      margin-right:75px;
      margin-top:25px;
    }
    
    .column img {
      width: 100%;
    }
    
    .controller {
      background: linear-gradient(transparent, #1A1A1A);
      
      height: 100px;
      width: 100%;
      padding-top: 20px;
      margin-top: -100px;
    }
    
    .prev,
    .next {
    text-align: center;
      position: absolute;
      cursor: pointer;
      background-color: rgba(0, 0, 0, 0.4);
      color: #ffffff;
      border: 1px solid #02BFC1;
      border-radius: 50%;
      width: 50px;
      padding: 15px;
      font-size: 25pt;
      transition: 0.6s ease;
      user-select: none;
      -webkit-user-select: none;
    }
    
    .next {
      margin-left: 50px;
    }
    
    .prev {
      margin-left: -50px;
    }
    <div class="photos-thumbnail">
      <img class="hover-shadow" onclick="openModal(); currentSlide(1)" src="https://wallpaperscraft.com/image/hitman_xbox_360_absolution_76000_1280x720.jpg">
      <img class="hover-shadow" onclick="openModal(); currentSlide(2)" src="https://www.technobuffalo.com/wp-content/uploads/2014/03/Hitman-Absolution-2-1280x720.jpg">
      <img class="hover-shadow" onclick="openModal(); currentSlide(3)" src="http://lorempixel.com/400/200/">
    </div>
    
    <div id="myModal" class="pro-modal">
      <div class="modal-content">
    
        <div onkeydown="ff(e)" class="mySlides">
          <img src="https://wallpaperscraft.com/image/hitman_xbox_360_absolution_76000_1280x720.jpg">
              <div class="controller">
          <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
          <a class="sep">|</a>
          <a class="next" onclick="plusSlides(1)">&#10095;</a>
        </div>
        </div>
    
        <div onkeydown="ff(e)" class="mySlides">
          <img src="https://www.technobuffalo.com/wp-content/uploads/2014/03/Hitman-Absolution-2-1280x720.jpg">
              <div class="controller">
          <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
          <a class="sep">|</a>
          <a class="next" onclick="plusSlides(1)">&#10095;</a>
        </div>
        </div>
    
        <div onkeydown="ff(e)" class="mySlides">
          <img src="http://lorempixel.com/400/200/">
              <div class="controller">
          <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
          <a class="sep">|</a>
          <a class="next" onclick="plusSlides(1)">&#10095;</a>
        </div>
        </div>
    
      </div>
    </div>

    【讨论】:

    • 感谢您的回答,但效果不佳。请查看当我调整窗口大小时,由于“object-fit: contains”,我自己的代码会自动调整图像大小。调整窗口大小时,图像停留在页面中间。但我面临的问题是按钮保持在底部但不会上升,并且在调整浏览器大小时会自动调整图像大小。它静态地停留在底部,因为我无法将这些按钮与图像相关联。您的代码也仍然是静态的。
    • 您使用哪种浏览器?对我来说,在 Chrome 和 Firefox 中,图像根本没有调整大小,它只是停留在左侧。因此,您希望图像调整大小并且按钮保持在图像中间?
    • 我刚刚用图片示例更新了我的帖子,请也检查一下。我正在使用带有 Brackets IDE Live View 功能的 Chrome。是的,在我的浏览器中,图像也位于左侧,其上方的按钮位于底部。但现在的问题是,图像不会随着浏览器窗口大小的调整而调整大小。图像没有响应。这就是为什么我的 CSS 中有 object fit: contains、width: 100% 和 height: 90% 的原因。虽然图像根据窗口大小调整大小,但我希望图像底部有按钮。
    • 查看我的编辑,如果我理解正确的话,这应该可以解决您的问题。
    • 哦!心情舒畅。你做到了!谢谢.. 但是我们可以在调整大小时自动将该图像保持在中心,同时在底部有相同的按钮.. 当前图像位于顶部。
    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2017-06-22
    • 2018-11-22
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    相关资源
    最近更新 更多