Advertisement
Guest User

gallery.js

a guest
Oct 19th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var gallery={object:{},object_key:'',current_index:0,is_scrollable:!1,constants:{WINDOW_WIDTH:0,WINDOW_HEIGHT:0,BACKGROUND_OPACITY:0.9,IMAGE_HOVER_OPACITY:0.7,CONSTRUCT_STRING_SEPARATOR:'|'},construct:function(a){this.setGallery();var c,b=a.split(this.constants.CONSTRUCT_STRING_SEPARATOR);for(c in b)b[c]='a[rel='+b[c]+']';var d=b.toString();$(d).on('click',function(){var g,h,e=$(this).attr('rel'),f='a[rel='+e+']';return gallery.object[e]=[],gallery.object_key=e,gallery.current_index=$(this).parent().parent().find(f).index(this),gallery.object[e].length||$(f).each(function(){g=$(this).attr('href'),h=$(this).attr('title'),gallery.object[e].push({href:g,title:h})}),gallery.setWindowSize(gallery.is_scrollable),gallery.setBackground(gallery.current_index),!1})},setGallery:function(){$(window).on('load',function(){gallery.is_scrollable=!!($(document).height()>$(window).height())});var d,a=$('div.gallery').width(),b='div.gallery > div',c=0;switch($(b).removeClass('eol'),!0){case 767>=a:d=2;break;case 768<=a&&1023>=a:d=3;break;case 1024<=a&&1199>=a:d=4;break;case 1200<=a:d=5;}$(b).each(function(){++c,0==c%d&&$(this).addClass('eol')})},setWindowSize:function(a){a&&$('body').css('overflowY','hidden'),this.constants.WINDOW_WIDTH=$(window).width(),this.constants.WINDOW_HEIGHT=$(window).height()},getDimension:function(a,b){var c=this.constants.WINDOW_WIDTH>a?(this.constants.WINDOW_WIDTH-a)/2:0,d=this.constants.WINDOW_HEIGHT>b?(this.constants.WINDOW_HEIGHT-b)/2:0;return[c,d]},setBackground:function(a){$('body').append('<div id="background"><div class="content top"></div><div class="close"></div><div class="loading"></div><div class="content bottom"></div></div>'),$('div#background').css('opacity',this.constants.BACKGROUND_OPACITY);var c=this.getDimension(50,50);$('div.loading','div#background').css({top:c[1],left:c[0]}),this.setForeground(a+1)},setForeground:function(a){$('body').append('<div id="foreground"><div class="image"></div></div>');var c=this.object[this.object_key].length;this.current_index=a-1,$('div.image','div#foreground').append('<img src="'+this.object[this.object_key][this.current_index].href+'" onload="gallery.getImage();">'),$('div.content.top','div#background').text(this.object[this.object_key][this.current_index].title),$('div.content.bottom','div#background').text(a+' / '+c),1!==a&&c!==a&&(this.setPreviousLink(a),this.setNextLink(a)),1===a&&1!==c&&this.setNextLink(a),a===c&&1!==c&&this.setPreviousLink(a),this.close()},getImage:function(){var a=new Image;a.src=$('div.image > img','div#foreground').attr('src');var b=!1,c=a.width,d=a.height;d>this.constants.WINDOW_HEIGHT-100&&(b=!0,d=this.constants.WINDOW_HEIGHT-98,c=(a.width/a.height*d).toFixed(0)),c>this.constants.WINDOW_WIDTH-100&&(b=!0,c=this.constants.WINDOW_WIDTH-102,d=(a.height/a.width*c).toFixed(0)),b&&($('div.image > img','div#foreground').attr('width',c),$('div.image > img','div#foreground').attr('height',d));var e=this.getDimension(c,d);$('div.loading','div#background').animate({width:c,height:d,top:e[1],left:e[0]},200),$('div#foreground').css({width:c,height:d,top:e[1],left:e[0]}).fadeIn(600)},setPreviousLink:function(a){$('div#background').prepend('<div class="previous"></div>'),$('div.previous','div#background').on('click',function(){$('div#foreground').is(':animated')||($('div.previous','div#background').remove(),$('div#foreground').remove(),--a,gallery.setForeground(a))})},setNextLink:function(a){$('div#background').append('<div class="next"></div>'),$('div.next','div#background').on('click',function(){$('div#foreground').is(':animated')||($('div.next','div#background').remove(),$('div#foreground').remove(),++a,gallery.setForeground(a))})},close:function(){$('div.close','div#background').on('click',function(){$('div#background, div#foreground').remove(),gallery.is_scrollable&&$('body').css('overflowY','scroll')})},setOrientationChange:function(){$(window).on('resize',function(){gallery.setGallery(),$('div.loading').length&&($('div#background, div#foreground').remove(),gallery.setWindowSize(gallery.is_scrollable),gallery.setBackground(gallery.current_index))})}(),setGalleryHover:function(){$('div.gallery > div > a > img').hover(function(){$(this).fadeTo(200,gallery.constants.IMAGE_HOVER_OPACITY)},function(){$(this).fadeTo(200,1)})}(),keydown:function(){$(document).on('keydown',function(a){switch(a.which){case 27:$('div.close','div#background').trigger('click');break;case 37:$('div.previous','div#background').trigger('click');break;case 39:$('div.next','div#background').trigger('click');break;case 13:return!1;}})}()};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement