Skip to content

Instantly share code, notes, and snippets.

Created May 22, 2012 03:05
var items = [];
var Item = function(img, gallery){
// init with an image in jquery form
var self = this;
this.src = img.attr('src');
this.setDimensions = function(){
var temp_height, temp_width;
self.image = $('<img />').attr('src', self.src).load(function(){
self.height = this.height;
self.width = this.width;
items.push(self);
});
};
// init functions
this.setDimensions();
}; // Item
var Gallery = function(){
this.width = $(window).width();
this.height = $(window).height();
this.images = [];
this.createElements = function(){
$('body').append('<div id="the_gallery"></div>');
$('#the_gallery').css({width: this.width, height: this.height});
};
this.open = function(){
this.createElements();
var img = items[0];
// see if the image is bigger than the window
if( this.width >= img.width && this.height >= img.height) {
console.log('image < window');
// just show the image, centered
}
else {
console.log('image > window');
var temp_width, temp_height;
// constrain to height
if( img.width < img.height ) {
console.log('image width < image height');
temp_height = this.height;
temp_width = (temp_height/img.height) * img.width;
img.css({height:temp_height, width:temp_width});
}
// constrain to width
else {
console.log('image width > image height');
console.log(img);
console.log('img.width: ' + img.width);
temp_width = this.width;
temp_height = (temp_width/img.width) * img.height;
img.image.css({height:temp_height, width:temp_width});
}
}
img.image.appendTo($('#the_gallery'));
}; // open
this.close = function(){
$('#the_gallery').remove();
}; // close
}; // Gallery
$(document).ready(function(){
$('#gallery img').click(function(){
launchGallery($(this));
}); // click
//var gallery = new Gallery(),
var test = new Item($('#gallery img:first'), gallery);
}); // doc ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment