HTML code
This is the html code used to create this bootstrap snippet, You can copy and paste the following html code inside a page with bootstrap 5.1.3 included, to get the result that you can see in the preview selection
Download<!DOCTYPE html>
<html lang="en">
<head>
<title>Resource efficient image zoom jQuery</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="container pt-5">
<div class="row">
<div class="col-12">
<h1>Resource efficient image zoom jQuery</h1>
<hr>
<p>Initialize the plugin:</p>
<textarea class="form-control mb-4" rows="3">$(document).ready(function () {
$(document).find('[data-toggle="image-zoom"]').modalZoom();
});</textarea>
<p>Element sample:</p>
<textarea class="form-control mb-4" rows="8"><img
src="https://www.bootdey.com/image/150/000/fff?Text=mmltools.com"
data-toggle="image-zoom"
data-src="https://www.bootdey.com/image/1920x1080/000/fff?Text=mmltools.com"
data-caption="Test Caption"
data-description="Test description"
alt=""
class="mr-3 image-zoom" /></textarea>
<p>Data elements:</p>
<ul>
<li><strong>src</strong> - Full or relative path to the thumbnail</li>
<li><strong>data-toggle="image-zoom"</strong> - Tells the script that this image can be zoomed</li>
<li><strong>data-src</strong> - Full or relative path to the big image</li>
<li><strong>data-caption</strong> - Title of the image, if not set, it won't show anything</li>
<li><strong>data-description</strong> - Description of the image, if not set, it won't show anything</li>
</ul>
<p>Click the image below:</p>
<img src="https://www.bootdey.com/image/150/000/fff?Text=mmltools.com" data-toggle="image-zoom" data-src="https://www.bootdey.com/image/1920x1080/000/fff?Text=mmltools.com" data-caption="Test Caption" data-description="Test description" alt="" class="mr-3 image-zoom" />
</div>
</div>
</div>
</body>
<div style="position: fixed;bottom: 5%;right: 5%;padding: 1rem;background: #fff;border: 1px solid #e9ecef;border-radius: 0.35rem;color: #a9aeb3;box-shadow: 0 0 20px #e9ecef;">
<h5 style="margin-bottom: 1rem;padding-bottom: 1rem;text-align: center;">Support my work @ MMLTech</h5>
<div style="display: flex;align-items: center;">
<a style="margin-right:1rem;background: #d1e6fd;display: block;padding: 1rem;border-radius: 0.25rem;border: 1px solid #e9ecef;color: #000;font-weight: bold;" href="https://ko-fi.com/mmltech" target="_blank"><img src="https://storage.ko-fi.com/cdn/Kofi_Logo_Blue.svg" width="60px"/> Buy me a coffee</a>
<a style="background: #d1e6fd;display: block;padding: 1rem;border-radius: 0.25rem;border: 1px solid #e9ecef;color: #000;font-weight: bold;" href="https://obscountdown.com" target="_blank"><img src="https://streamcd.net/Assets/images/logo-white.png" width="60px" height="19px" /> My projects</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="main.js"></script>
</html>
CSS code
This is the css code used to create this bootstrap snippet, You can copy and paste the following css code inside a page with bootstrap 5.1.3 included, to get the result that you can see in the preview selection
Download.image-zoom-modal .modal-fit-content {
max-width: 70%;
width: -moz-fit-content;
width: fit-content;
}
.image-zoom-modal .modal-fit-content .image-zoom-out {
max-width: 100%;
cursor: zoom-out;
}
.image-zoom {
cursor: zoom-in;
}
/*# sourceMappingURL=style.css.map */
Javascript/Jquery code
This is the javascript code used to create this bootstrap snippet, You can copy and paste the following javascript code inside a page with bootstrap 5.1.3 included, to get the result that you can see in the preview selection
Download(function($){
$.fn.modalZoom = function(options) {
let caption = "",
description = "",
src = "";
let ShowModal = function(){
let modal_element = $('<div class="modal image-zoom-modal" tabindex="-1" role="dialog" id="DonationModal"></div>'),
dialog_element = $('<div class="modal-dialog modal-fit-content" role="document"></div>'),
content_element = $('<div class="modal-content"></div>'),
body_element = $('<div class="modal-body mt-4"></div>'),
src_element = $('<img class="image-zoom-out">'),
caption_element = $('<h4 class="image-zoom-caption">' + caption + '</h4>'),
description_element = $('<p class="image-zoom-description">' + description + '</p>'),
close_element = $('<button type="button" class="close modal-close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>');
close_element.css({
position: "absolute",
right: "5px",
opacity: 1
});
if(src.length > 0)
{
src_element.attr("src", src);
body_element.append(src_element);
}
if(caption.length > 0)
body_element.append(caption_element);
if(src.length > 0)
body_element.append(description_element);
content_element.append(close_element);
content_element.append(body_element);
dialog_element.append(content_element);
modal_element.append(dialog_element);
$("body").append(modal_element);
modal_element.modal("show");
};
let build = function (e) {
/**
* Build the modal using the dom element data items
*/
$(e).on("click", function () {
caption = $(this).attr("data-caption");
description = $(this).attr("data-description");
src = $(this).attr("data-src");
ShowModal();
});
/**
* Close the modal if zoom-out image is clicked
*/
$(document).on("click", ".image-zoom-out", function () {
$(document).find(".modal").modal("hide");
});
/**
* Remove the modal from DOM
*/
$(document).on("hidden.bs.modal", ".image-zoom-modal", function () {
$(this).remove();
})
};
return this.each(function() {
return new build(this);
});
};
})(jQuery);
$(document).ready(function () {
$(document).find('[data-toggle="image-zoom"]').modalZoom();
});
About this bootstrap example/template
This example/template, Resource efficient image zoom jQuery plugin, was published on May 17th 2022, 05:45 by Marco Maxim and it is free.
We hope you will enjoy this awesome snippet and stay tuned for the latest updates, bootdey snippets are already used in thousands of blogs, websites and projects. We believe it will save your precious time and gives trendy look to your next web project.
We always try to offer the best beautiful and responsive source of Bootstrap code examples and components.
This code example currectly have 717 views, Using this bootstrap snippet you have the following benefits:
Bootstrap 5.1.3
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js'></script>
This code example is based on bootstrap 5.1.3 and the grid system of this framework
Responsive
Based on bootstrap framework makes all the layouts perfectly responsive for all devices
Crossbrowser compatibility
Tested on all major browsers, it works smoothly on all of them
Jquery plugins
Great built-in plugins with jquery framework, you can easy to change all declarations
semantic html 5
Built on html / css3 the code quality is really amazing
Simple Integration
This code example can be simply integrated on existing sites and new ones too, all you need to do is copy the code and start working