Today we will continue our jQuery tutorials. And today i m going to talk about image zoom tools. This is nice image zooming tool. It looks like round lens and have very easy initialization. There were many libraries tools are also available, but they was a little ugly, more like squares than the magnifying glass. I like this plugin, hope that you will like it too. And I added a demo also, we can see it in the last part of the article.
Step 1.HTML
As usual, we start with the HTML. This is source code of our sample:
Step 3.JS
Everything is quite easy – when the page loads I perform initialization of imageLens plugin for each image. Each image will have different lens properties. Default lensSize is 100, size – 4, and color – #888. Of course, if you don`t going to use different lens on your set of images and want single – you can just change jquery selector.
Step 1.HTML
As usual, we start with the HTML. This is source code of our sample:
index.html
As I hope – all clean here, no need explain it. Here are just main page with 2 images inside.
Step 2.CSS
<link rel="stylesheet" href="css/main.css" type="text/css" />
<script src="js/jquery-1.5.2.min.js"></script>
<script src="js/jquery.imageLens.js"></script>
<script src="js/main.js"></script>
<div class="example">
<h3><a href="#">imageLens samples</a></h3>
<img id="img_01" src="data_images/img1.jpg" width="350" height="262" />
<img id="img_02" src="data_images/img2.jpg" width="350" height="262" />
<img id="img_03" src="data_images/img3.jpg" width="350" height="262" />
<img id="img_04" src="data_images/img4.jpg" width="350" height="262" />
</div>
As I hope – all clean here, no need explain it. Here are just main page with 2 images inside.
Step 2.CSS
Here are single CSS file with all necessary styles:
main.css
body{background:#eee;margin:0;padding:0}
.example{background:#FFF;width:800px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
.example img {margin:22px}
Step 3.JS
Here are all JS files:
main.js
$(document).ready(function(){
$('#img_01').imageLens(); // default initialization
$('#img_02').imageLens({ lensSize: 200 }); // set lens size
});
imagelense.js
(function ($) {
$.fn.imageLens = function (options) {
var defaults = {
lensSize: 100,
borderSize: 4,
borderColor: "#888"
};
var options = $.extend(defaults, options);
var lensStyle = "background-position: 0px 0px;width: " + String(options.lensSize) + "px;height: " + String(options.lensSize)
+ "px;float: left;display: none;border-radius: " + String(options.lensSize / 2 + options.borderSize)
+ "px;border: " + String(options.borderSize) + "px solid " + options.borderColor
+ ";background-repeat: no-repeat;position: absolute;";
return this.each(function () {
obj = $(this);
var offset = $(this).offset();
// Creating lens
var target = $("<div style='" + lensStyle + "' class='" + options.lensCss + "'> </div>").appendTo($(this).parent());
var targetSize = target.size();
// Calculating actual size of image
var imageSrc = options.imageSrc ? options.imageSrc : $(this).attr("src");
var imageTag = "<img style='display:none;' src='" + imageSrc + "' />";
var widthRatio = 0;
var heightRatio = 0;
$(imageTag).load(function () {
widthRatio = $(this).width() / obj.width();
heightRatio = $(this).height() / obj.height();
}).appendTo($(this).parent());
target.css({ backgroundImage: "url('" + imageSrc + "')" });
target.mousemove(setPosition);
$(this).mousemove(setPosition);
function setPosition(e) {
var leftPos = parseInt(e.pageX - offset.left);
var topPos = parseInt(e.pageY - offset.top);
if (leftPos < 0 || topPos < 0 || leftPos > obj.width() || topPos > obj.height()) {
target.hide();
}
else {
target.show();
leftPos = String(((e.pageX - offset.left) * widthRatio - target.width() / 2) * (-1));
topPos = String(((e.pageY - offset.top) * heightRatio - target.height() / 2) * (-1));
target.css({ backgroundPosition: leftPos + 'px ' + topPos + 'px' });
leftPos = String(e.pageX - target.width() / 2);
topPos = String(e.pageY - target.height() / 2);
target.css({ left: leftPos + 'px', top: topPos + 'px' });
}
}
});
};
})(jQuery);
Everything is quite easy – when the page loads I perform initialization of imageLens plugin for each image. Each image will have different lens properties. Default lensSize is 100, size – 4, and color – #888. Of course, if you don`t going to use different lens on your set of images and want single – you can just change jquery selector.
No comments:
Post a Comment