【问题标题】:Positioning buttons next to image图像旁边的定位按钮
【发布时间】:2021-01-07 19:05:00
【问题描述】:

我已经问过similar question,但现在我换了一些样式,不知道如何将关闭按钮定位到右上角以及图像左侧和右侧的上一个和下一个按钮到它的给定距离

就在我页面的页脚之前:

<div class="modal" id="modal">
    <div id="modal-content">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        <img id="modal-image">
    </div>
</div>

我如何显示模态:

function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}

这是我的样式:

/* Background when image is shown */
#modal {
    display: none; /* Hidden by default */
    /* Position */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    /* Sizing */
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(119, 119, 119); /* Fallback color */
    background-color: rgba(119, 119, 119, 0.7); /* Black w/ opacity */
}

/* Image wrapper */
#modal-content {
    /* Sizing */
    width: 100%;
    height: 100%;
}

/* Image */
#modal-image {
    /* Sizing */
    max-width: 80%;
    height: calc(100vh * 0.6);
    /* Style */
    border: 10px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
    /* Horizontally center image */
    margin: auto;
    /* Zoom (image gets bigger) on open */
    animation-name: zoom;
    animation-duration: 0.6s;
    /* Vertically center image */
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

@keyframes zoom {
    from {
        transform: scale(0)
    }
    to {
        transform: scale(1)
    }
}

/* Gallery Buttons */
.gallery-button {
    /* Style */
    color: #ffffff;
    transition: 0.3s;
    background-color: #000000;
    border: 2px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
    /* Makes the Button round */
    width: 20px;
    height: 20px;
    line-height: 20px;
    border-radius: 50%;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
}

/* Previous Button */
#previous {
    font-size: 12px;
    /* Position */
    position: absolute;
    /* Horizontally */
    left: 30px;
    /* Vertically */
    top: 50%;
    transform: translateY(-50%);
}

/* Next Button */
#next {
    font-size: 12px;
    /* Position */
    position: absolute;
    /* Horizontally */
    right: 30px;
    /* Vertically */
    top: 50%;
    transform: translateY(-50%);
}

/* Close Button */
#close {
    font-size: 15px;
    /* Position */
    position: absolute;
    top: 25px;
    right: 30px;
}

/* Change cursor when pointing on button */
.gallery-button:hover,
.gallery-button:focus {
    text-decoration: none;
    cursor: pointer;
}

/* 100% image width on smaller screens */
@media only screen and (max-width: 700px) {
    .modal-content {
        width: 100%;
    }
}

目前关闭按钮位于整个页面的右上角,并且上一个和下一个按钮已经垂直居中(这很好),但尚未靠近图像。我怎样才能将按钮附加到图像上。调整图像大小有点困难,我不知道如何以不同的方式进行调整,以便我可以将调整大小的图像和按钮放在 div 中。

【问题讨论】:

  • 如果您能够创建一个minimal, reproducible example,那将会很有帮助。这样我们就可以体验您的代码,而无需想象它会是什么样子。尝试省略您的问题不需要的代码。
  • 我编辑了我的问题。我希望现在更容易理解。
  • #modal-content 样式中尝试添加position: relative。它根本不会干扰包装器,但它会设置子代相对于它的位置,而不是它的父代。
  • 我将这一行添加到#modal-content,但它根本没有改变任何东西。我需要添加任何其他样式属性吗?

标签: javascript html css position


【解决方案1】:

我已经排除了你的 CSS。因为真的很乱。但是如果你检查下面的这段代码,你可以理解如何设置定位。希望你能完成剩下的工作。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #modal-content{
      position: relative;
      width:50%;
      height:50%;
    }
    #modal-image{
      width:100%;
      height:auto;
    }
    #close{
      position: absolute;
      right: 0px;
      top: 0px;
    }
    #previous{
      position: absolute;
      top: 50%;
      left: 1%;
      
    }
    #next{
      position: absolute;
      top: 50%;
      right: 1%;
      
    }
  </style>
</head>
<body>
  <div class="modal" id="modal">
    <div id="modal-content">
        
        
        <img id="modal-image" src="https://dummyimage.com/vga" alt="">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        
    </div>
</div>
<script>
  function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}
</script>
</body>
</html>

更新代码(作为要求)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #modal{
          width:100vw;
          height: 100vh;
      }
    #modal-content{
      position: relative;
      width:35%;
      height:90%;
    }
    #modal-image{
      width:100%;
      height:100%;
    }
    #close{
      position: absolute;
      right: 0px;
      top: 0px;
    }
    #previous{
      position: absolute;
      top: 50%;
      left: 1%;
      
    }
    #next{
      position: absolute;
      top: 50%;
      right: 1%;
      
    }
  </style>
</head>
<body>
  <div class="modal" id="modal">
    <div id="modal-content">
        
        
        <img id="modal-image" src="https://dummyimage.com/vga" alt="">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        
    </div>
</div>
<script>
  function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}
</script>
</body>
</html>

【讨论】:

  • 感谢您,您已经帮了很多忙。现在的问题是我不知道modal-content 的高度。我可以将宽度设置为 35%,但我希望高度可以使图像保持其纵横比。由于 modal-content 不是图像本身,我不知道该怎么做。当我将其设置为 100% 时(这样图像的高度由 modal-image 中的 height:auto 确定,关闭按钮当然在页面顶部。
  • 我现在能够解决这个问题,但由于某种原因,图像比modal-content 大。如果我设置width: 100%,这怎么会发生?你可以在这里看到它:link。此外,如何指定max-height?为modal-content 设置max-height 属性只会使modal-content 更小。对于modal-content,我设置了width: 35%; height: auto;modal-image width: 100%; height: auto;
  • 总结一下我上面写的:我的最后一个问题是我不知道如何将图像的最大高度设置为屏幕高度的90%。
  • 如果我理解正确,请在父 Div 上制作 100vw100vh。这样父 div 就可以从整个屏幕中获取 100% 宽度100% 高度。之后,使用 width:35%height:90% (根据您的要求)制作您的 modal-content。现在制作你的 modal-image width:100%height:100% 。我已经更新了我的主要答案。检查那里以获得更好的理解。谢谢
  • 感谢您的帮助,但不幸的是,这不起作用,因为当我同时将宽度和高度用于模态内容时,图像无法保持其纵横比。如果您没有不同的想法,我可能不得不保持原样,即使它不是 100% 完美(不适用于旋转到横向的移动设备)。
猜你喜欢
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 2019-01-13
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多