【问题标题】:show clicked images in new template [closed]在新模板中显示点击的图像[关闭]
【发布时间】:2021-05-11 14:13:39
【问题描述】:
<body>
    <div class="images">
        <img  src="image1" id="1">
        <img  src="image2" id="2">
        <img  src="image3" id="3">
    </div>

    
    
</body>

如何在 div 的新页面中显示点击的图像

【问题讨论】:

  • 你应该为它写一些javascript
  • 我应该写什么
  • 在我们提供任何帮助之前,请提供更多有关该问题的背景信息。

标签: javascript html css


【解决方案1】:

您好,您必须使用 javascript 来处理它。我有一个非常非常简单的代码。与您分享:

当然,你必须自己组织。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Zoom Img</title>
    <style>
      * {
    border: 0;
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.container {
    width: 90%;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
}

.pics {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    margin: 20px auto;
    border-radius: 5px;
}

.pics img {
    width: 33.3333%;
    border-radius: 2px;
    cursor: pointer;
}

.cover {
    position: absolute;
    /*display: flex;*/
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.726);
    margin: 0 auto;

    display: none
}

.cover a {
    text-decoration: none;
    color: rgb(233, 44, 44);
    font-size: 25px;
    /* font-weight: 800; */
    margin: 20px 20px 0 0;
    background: rgb(49, 47, 43);
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 50%;
    padding-top: 2px;
    position: absolute;
    top: 15px;
    right: 15px;
}

.cover #newImage {
    position: absolute;
    width: 80%;
   z-index: 999;
   border-radius: 10px;
}
    </style>
</head>

<body>
    <div class="container">
        <div class="pics">
            <img src="./images/1.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/2.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/3.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/4.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/5.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/6.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/7.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/8.jpg" alt="" onclick="zoomIn(this)">
            <img src="./images/9.jpg" alt="" onclick="zoomIn(this)">
        </div>
    </div>
    <div class="cover">
        <a href="#" onclick="closeCover(event)">X</a>
    </div>

    <script >
      let cover = document.getElementsByClassName("cover")[0];

      function zoomIn(img) {
        let newImg = document.createElement("img");
        newImg.src = img.src;
        newImg.id = "newImage";
        cover.appendChild(newImg);

        cover.style.display = "flex";
      }

      function closeCover(e) {
        e.preventDefault();
        cover.removeChild(document.getElementById("newImage"));
        cover.style.display = "none";
      }

    </script>
</body>

</html>

【讨论】:

  • 这很好,但我想在新页面中显示图像,如 details.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多