简体   繁体   中英

Lightbox Effect in HTML

I'm trying to edit my javascript code to enable lightbox when clicked on an image. Presently, it opens the image in another tab. I've searched around online but could not seem to find an answer..

<script language="JavaScript">
$(function() {
  /* Lets the user click on the images to view them in full resolution. */
  $("img").wrap(function() {
    var link = $('<a/>');
    link.attr('href', $(this).attr('src'));
    link.attr('title', $(this).attr('alt'));
    link.attr('target', '_blank');
    return link;
  });
});
</script>

<img src="img/4_email_a.png" style="border: #A9A9A9 1px solid; width:70%">

Could someone point me in the right direction? Thank you!

You have to use a lightbox script like fancybox ( https://fancyapps.com/fancybox/3/ ), you would just need to add data-fancybox attributes for your links, example:

<a data-fancybox="gallery" href="big_1.jpg"><img src="small_1.jpg"></a>

It is also possible to make it work without links - https://codepen.io/anon/pen/rRVeJW

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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