cops/fancybox/helpers/jquery.fancybox-thumbs.js

162 lines
3.7 KiB
JavaScript
Raw Normal View History

2012-06-02 09:48:07 +03:00
/*!
* Thumbnail helper for fancyBox
* version: 1.0.7 (Mon, 01 Oct 2012)
2012-06-02 09:48:07 +03:00
* @requires fancyBox v2.0 or later
*
* Usage:
* $(".fancybox").fancybox({
* helpers : {
* thumbs: {
* width : 50,
* height : 50
* }
2012-06-02 09:48:07 +03:00
* }
* });
*
*/
(function ($) {
//Shortcut for fancyBox object
var F = $.fancybox;
//Add helper object
F.helpers.thumbs = {
defaults : {
width : 50, // thumbnail width
height : 50, // thumbnail height
position : 'bottom', // 'top' or 'bottom'
source : function ( item ) { // function to obtain the URL of the thumbnail image
var href;
if (item.element) {
href = $(item.element).find('img').attr('src');
}
2012-06-02 09:48:07 +03:00
if (!href && item.type === 'image' && item.href) {
href = item.href;
}
2012-06-02 09:48:07 +03:00
return href;
}
2012-06-02 09:48:07 +03:00
},
wrap : null,
list : null,
width : 0,
init: function (opts, obj) {
2012-06-02 09:48:07 +03:00
var that = this,
list,
thumbWidth = opts.width,
thumbHeight = opts.height,
thumbSource = opts.source;
2012-06-02 09:48:07 +03:00
//Build list structure
list = '';
for (var n = 0; n < obj.group.length; n++) {
2012-06-02 09:48:07 +03:00
list += '<li><a style="width:' + thumbWidth + 'px;height:' + thumbHeight + 'px;" href="javascript:jQuery.fancybox.jumpto(' + n + ');"></a></li>';
}
this.wrap = $('<div id="fancybox-thumbs"></div>').addClass(opts.position).appendTo('body');
2012-06-02 09:48:07 +03:00
this.list = $('<ul>' + list + '</ul>').appendTo(this.wrap);
//Load each thumbnail
$.each(obj.group, function (i) {
var href = thumbSource( obj.group[ i ] );
if (!href) {
return;
}
2012-06-02 09:48:07 +03:00
$("<img />").load(function () {
var width = this.width,
2012-06-02 09:48:07 +03:00
height = this.height,
widthRatio, heightRatio, parent;
if (!that.list || !width || !height) {
return;
}
//Calculate thumbnail width/height and center it
widthRatio = width / thumbWidth;
2012-06-02 09:48:07 +03:00
heightRatio = height / thumbHeight;
2012-06-02 09:48:07 +03:00
parent = that.list.children().eq(i).find('a');
if (widthRatio >= 1 && heightRatio >= 1) {
if (widthRatio > heightRatio) {
width = Math.floor(width / heightRatio);
2012-06-02 09:48:07 +03:00
height = thumbHeight;
} else {
width = thumbWidth;
2012-06-02 09:48:07 +03:00
height = Math.floor(height / widthRatio);
}
}
$(this).css({
width : width,
height : height,
top : Math.floor(thumbHeight / 2 - height / 2),
left : Math.floor(thumbWidth / 2 - width / 2)
2012-06-02 09:48:07 +03:00
});
parent.width(thumbWidth).height(thumbHeight);
$(this).hide().appendTo(parent).fadeIn(300);
}).attr('src', href);
2012-06-02 09:48:07 +03:00
});
//Set initial width
this.width = this.list.children().eq(0).outerWidth(true);
this.list.width(this.width * (obj.group.length + 1)).css('left', Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5)));
2012-06-02 09:48:07 +03:00
},
beforeLoad: function (opts, obj) {
2012-06-02 09:48:07 +03:00
//Remove self if gallery do not have at least two items
if (obj.group.length < 2) {
obj.helpers.thumbs = false;
2012-06-02 09:48:07 +03:00
return;
}
//Increase bottom margin to give space for thumbs
obj.margin[ opts.position === 'top' ? 0 : 2 ] += ((opts.height) + 15);
2012-06-02 09:48:07 +03:00
},
afterShow: function (opts, obj) {
2012-06-02 09:48:07 +03:00
//Check if exists and create or update list
if (this.list) {
this.onUpdate(opts, obj);
2012-06-02 09:48:07 +03:00
} else {
this.init(opts, obj);
2012-06-02 09:48:07 +03:00
}
//Set active element
this.list.children().removeClass('active').eq(obj.index).addClass('active');
2012-06-02 09:48:07 +03:00
},
//Center list
onUpdate: function (opts, obj) {
if (this.list) {
this.list.stop(true).animate({
'left': Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5))
}, 150);
}
2012-06-02 09:48:07 +03:00
},
beforeClose: function () {
if (this.wrap) {
this.wrap.remove();
}
this.wrap = null;
this.list = null;
2012-06-02 09:48:07 +03:00
this.width = 0;
}
}
}(jQuery));