简体   繁体   English

如何根据图像缩略图在锚标签上使用 onclick 打开(全屏)图像

[英]how to open (full-screen) images with onclick on anchor tag based on image thumbnail

I want to show the image on the pop-up div depend on the image thumbnail.我想在弹出 div 上显示图像取决于图像缩略图。 like on clicking onclick of images/thumbnails/apple.jpg will open a pop-up image images/actual/apple.jpg就像点击 images/thumbnails/apple.jpg 的 onclick 将打开一个弹出图像 images/actual/apple.jpg

HTML HTML

<div class="row">
                <div name="filter-img" class="col-lg-3 col-md-4 col-6 pencil zoom">
                    <a onclick="showImg()" class="d-block mb-4 h-100">
                        <img class="img-fluid img-hov" src="images/thumbnails/pencil.jpg" alt="1">
                    </a>
                </div>
                <div name="filter-img" class="col-lg-3 col-md-4 col-6 pencil zoom">
                    <a onclick="showImg()" class="d-block mb-4 h-100">
                        <img class="img-fluid img-hov" src="images/thumbnails/pencil2.jpg" alt="1">
                    </a>
                </div>
//another option is using onclick in img tag if this makes easy.
                <div name="filter-img" class="col-lg-3 col-md-4 col-6 web zoom">
                    <div class="d-block mb-4 h-100">
                        <img onclick="showImg()" class="img-fluid img-hov" src="images/thumbnails/web.jpg" alt="">
                    </div>
                </div>
                <div name="filter-img" class="col-lg-3 col-md-4 col-6 sketch zoom">
                    <div class="d-block mb-4 h-100">
                        <img onclick="showImg()" class="img-fluid img-hov" src="images/thumbnails/sketch.jpg" alt="1">
                    </div>
                </div>
                <div name="filter-img" class="col-lg-3 col-md-4 col-6 sketch zoom">
                    <div class="d-block mb-4 h-100">
                        <img onclick="showImg()" class="img-fluid img-hov" src="images/thumbnails/sketch2.jpg" alt="">
                    </div>
                </div>
</div>

let's say I want to show the image inside this假设我想在其中显示图像

HTML HTML

<div id="imgbox" class="d-none">
    <div id="mainimg">

    </div>
</div>

My idea was to get the src of the clicked link and then replacing thumbnails with actual on src and adding to HTML.我的想法是获取点击链接的 src,然后用 src 上的实际替换缩略图并添加到 HTML。

JS JS

function showImg() {
    var imgbox = document.getElementById("imgbox");
    imgbox.classList.toggle("d-none");
    document.getElementById("mainimg").innerHTML = "<img class='img-fluid' src='I dont know'>";
}

Please help me with the showImg() function for making this thing work.请帮助我使用 showImg() function 以使这件事起作用。

You must define "actual image url".您必须定义“实际图片网址”。 In this case, you can define this value in the function.在这种情况下,您可以在 function 中定义此值。

<div name="filter-img" class="col-lg-3 col-md-4 col-6 pencil zoom">
  <a onclick="showImg('images/actual/pencil.jpg')" class="d-block mb-4 h-100">
    <img class="img-fluid img-hov" src="images/thumbnails/pencil.jpg" alt="1">
  </a>
</div>

And you can access value in the function like this:您可以像这样访问 function 中的值:

function showImg(imageUrl) {
  document.getElementById("mainimg").innerHTML = "<img class='img-fluid' src='"+imageUrl+"'>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM