The simple "Lightbox + Image magnifier" using HTML, CSS, Javascript and JQUery.
A Pen by Teja Babu S on CodePen.
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <title>LightBox</title> | |
| <!-- Required meta tags --> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
| <!-- Bootstrap CSS --> | |
| <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous"> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.css"> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h3 class="text-center mt-5">Simple Gallery Box width Magnify Glass</h3> | |
| <h4 class="text-center text-secondary mb-0 pb-0">click on image to magnify image</h4> | |
| </div> | |
| <div class="container"> | |
| <div class="main-wrapper"> | |
| <div class="row"> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/269923/pexels-photo-269923.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/693267/pexels-photo-693267.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/1366929/pexels-photo-1366929.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/34199/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/288530/pexels-photo-288530.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/106344/pexels-photo-106344.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/266176/pexels-photo-266176.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/1162968/pexels-photo-1162968.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/270700/pexels-photo-270700.jpeg?auto=compress&cs=tinysrgb&h=350" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/313736/pexels-photo-313736.jpeg?auto=compress&cs=tinysrgb&h=350" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/270404/pexels-photo-270404.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-3 align-self-center"> | |
| <div class="img-wrapper"> | |
| <div class="img-item text-center"> | |
| <img src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?auto=compress&cs=tinysrgb&h=350" alt="" class="img-fluid"> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="gallary-bg"> </div> | |
| <!-- Optional JavaScript --> | |
| <!-- jQuery first, then Popper.js, then Bootstrap JS --> | |
| <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> | |
| <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script> | |
| </body> | |
| </html> |
The simple "Lightbox + Image magnifier" using HTML, CSS, Javascript and JQUery.
A Pen by Teja Babu S on CodePen.
| function magnify(imgID, zoom) { | |
| var img, glass, w, h, bw; | |
| img = document.getElementById(imgID); | |
| /*create magnifier glass:*/ | |
| glass = document.createElement("DIV"); | |
| glass.setAttribute("class", "img-magnifier-glass"); | |
| /*insert magnifier glass:*/ | |
| img.parentElement.insertBefore(glass, img); | |
| /*set background properties for the magnifier glass:*/ | |
| glass.style.backgroundImage = "url('" + img.src + "')"; | |
| glass.style.backgroundRepeat = "no-repeat"; | |
| glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px"; | |
| bw = 3; | |
| w = glass.offsetWidth / 2; | |
| h = glass.offsetHeight / 2; | |
| /*execute a function when someone moves the magnifier glass over the image:*/ | |
| glass.addEventListener("mousemove", moveMagnifier); | |
| img.addEventListener("mousemove", moveMagnifier); | |
| /*and also for touch screens:*/ | |
| glass.addEventListener("touchmove", moveMagnifier); | |
| img.addEventListener("touchmove", moveMagnifier); | |
| function moveMagnifier(e) { | |
| var pos, x, y; | |
| /*prevent any other actions that may occur when moving over the image*/ | |
| e.preventDefault(); | |
| /*get the cursor's x and y positions:*/ | |
| pos = getCursorPos(e); | |
| x = pos.x; | |
| y = pos.y; | |
| /*prevent the magnifier glass from being positioned outside the image:*/ | |
| if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);} | |
| if (x < w / zoom) {x = w / zoom;} | |
| if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);} | |
| if (y < h / zoom) {y = h / zoom;} | |
| /*set the position of the magnifier glass:*/ | |
| glass.style.left = (x - w) + "px"; | |
| glass.style.top = (y - h) + "px"; | |
| /*display what the magnifier glass "sees":*/ | |
| glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px"; | |
| } | |
| function getCursorPos(e) { | |
| var a, x = 0, y = 0; | |
| e = e || window.event; | |
| /*get the x and y positions of the image:*/ | |
| a = img.getBoundingClientRect(); | |
| /*calculate the cursor's x and y coordinates, relative to the image:*/ | |
| x = e.pageX - a.left; | |
| y = e.pageY - a.top; | |
| /*consider any page scrolling:*/ | |
| x = x - window.pageXOffset; | |
| y = y - window.pageYOffset; | |
| return {x : x, y : y}; | |
| } | |
| } | |
| $("img").click(function(){ | |
| const imgClone = $(this).parent(); | |
| $(".gallary-bg").addClass("gallary-bg-block") | |
| $(".gallary-bg").append(imgClone.clone()); | |
| $(".gallary-bg .img-item img").addClass("animated fadeIn"); | |
| $(".gallary-bg").removeClass("animated fadeOut"); | |
| $('.gallary-bg .img-item img').attr('id', 'myimage'); | |
| magnify("myimage", 2); | |
| }); | |
| $(".gallary-bg").click(function() { | |
| $(this).addClass("animated fadeOut"); | |
| $(this).children().children().addClass("fadeLeft"); | |
| $(this).children().remove(); | |
| var rmBg= $(this); | |
| setTimeout(() => { | |
| rmBg.removeClass("gallary-bg-block"); | |
| }, 400); | |
| }); | |
| .main-wrapper { | |
| padding: 50px 0; | |
| } | |
| img { | |
| cursor: pointer; | |
| } | |
| .img-wrapper { | |
| margin: 10px 0; | |
| transition: left 1s linear; | |
| } | |
| .img-wrapper .img-item{ | |
| overflow: hidden; | |
| } | |
| .img-wrapper .img-item img{ | |
| transition: all ease-in 0.1s; | |
| } | |
| .img-wrapper .img-item img:hover{ | |
| transform: scale(1.1); | |
| } | |
| .gallary-bg { | |
| position: fixed; | |
| top: 0; | |
| bottom: 0; | |
| left: 0; | |
| right: 0; | |
| background-color: #000000c7; | |
| display: none; | |
| cursor: pointer; | |
| } | |
| .gallary-bg-block { | |
| display: block; | |
| } | |
| .gallary-bg-block .img-item { | |
| position: absolute; | |
| left: 50%; | |
| top: 50%; | |
| transform: translate(-50%, -50%); | |
| } | |
| .gallary-bg .img-item > img { | |
| transition: all ease-in 0.5s; | |
| transform: scale(0); | |
| } | |
| .gallary-bg-block .img-item > img { | |
| transform: scale(1); | |
| padding: 5px; | |
| border: 2px solid #ffffff; | |
| border-radius: 2px; | |
| } | |
| .img-magnifier-glass { | |
| position: absolute; | |
| border: 3px solid #000; | |
| border-radius: 50%; | |
| cursor: none; | |
| /*Set the size of the magnifier glass:*/ | |
| width: 200px; | |
| height: 200px; | |
| z-index: 999999; | |
| } |