/* example usage: $('a.fullsizable').fullsizable(); options: **detach_id** (optional, defaults to null) - id of an element that will temporarely be set to ``display: none`` after the curtain loaded. **navigation** (optional, defaults to true) - show next and previous links when working with a set of images. **closebutton** (optional, defaults to true) - show a close link. **fullscreenbutton** (optional, defaults to true) - show full screen button for native html5 fullscreen support in supported browsers. **openonclick** (optional, defaults to true) - set to false to disable default behavior which fullsizes an image when clicking on a thumb. **clickbehaviour** (optional, 'next' or 'close', defaults to 'close') - whether a click on an opened image should close the viewer or open the next image. **preload** (optional, defaults to true) - lookup selector on initialization, set only to false in combination with ``reloadonopen: true`` or ``fullsizable:reload`` event. **reloadonopen** (optional, defaults to false) - lookup selector every time the viewer opens. */ /* 本代码由素材家园收集并编辑整理; 尊重他人劳动成果; 转载请保留素材家园链接 - www.sucaijiayuan.com */ (function() { var $, $image_holder, bindcurtainevents, closefullscreen, closeviewer, container_id, current_image, hasfullscreensupport, hidechrome, image_holder_id, images, keypressed, makefullsizable, mousemovement, mousestart, nextimage, openviewer, options, preloadimage, preparecurtain, previmage, resizeimage, showchrome, showimage, spinner_class, stored_scroll_position, togglefullscreen, unbindcurtainevents; $ = jquery; container_id = '#jquery-fullsizable'; image_holder_id = '#fullsized_image_holder'; spinner_class = 'fullsized_spinner'; $image_holder = $('
'); images = []; current_image = 0; options = null; stored_scroll_position = null; resizeimage = function() { var image; image = images[current_image]; if (image.ratio == null) { image.ratio = (image.naturalheight / image.naturalwidth).tofixed(2); } //if ($(window).height() / image.ratio > $(window).width()) { $(image).width($(window).width()*2/3); $(image).height($(window).width() * image.ratio*2/3); return $(image).css('margin-top', ($(window).height() - $(image).height()) / 2); //} //else { // $(image).height($(window).height()*2/3); // $(image).width($(window).height()*2/3 / image.ratio); // return $(image).css('margin-top', 0); //} }; keypressed = function(e) { if (e.keycode === 27) { closeviewer(); } if (e.keycode === 37) { previmage(true); } if (e.keycode === 39) { return nextimage(true); } }; previmage = function(shouldhidechrome) { if (shouldhidechrome == null) { shouldhidechrome = false; } if (current_image > 0) { console.log(images.length); return showimage(images[current_image - 1], -1, shouldhidechrome); } }; nextimage = function(shouldhidechrome) { if (shouldhidechrome == null) { shouldhidechrome = false; } if (current_image < images.length - 1) { return showimage(images[current_image + 1], 1, shouldhidechrome); } }; showimage = function(image, direction, shouldhidechrome) { if (direction == null) { direction = 1; } if (shouldhidechrome == null) { shouldhidechrome = false; } current_image = image.index; $(image_holder_id).hide(); $(image_holder_id).html(image); if (options.navigation) { if (shouldhidechrome === true) { hidechrome(); } else { showchrome(); } } if (image.loaded != null) { $(container_id).removeclass(spinner_class); resizeimage(); $(image_holder_id).fadein('fast'); return preloadimage(direction); } else { $(container_id).addclass(spinner_class); image.onload = function() { resizeimage(); $(image_holder_id).fadein('slow', function() { return $(container_id).removeclass(spinner_class); }); this.loaded = true; return preloadimage(direction); }; return image.src = image.buffer_src; } }; preloadimage = function(direction) { var preload_image; if (direction === 1 && current_image < images.length - 1) { preload_image = images[current_image + 1]; } else if ((direction === -1 || current_image === (images.length - 1)) && current_image > 0) { preload_image = images[current_image - 1]; } else { return; } preload_image.onload = function() { return this.loaded = true; }; if (preload_image.src === '') { return preload_image.src = preload_image.buffer_src; } }; openviewer = function(image) { $('body').append($image_holder); $(window).bind('resize', resizeimage); showimage(image); return $(container_id).hide().fadein(function() { if (options.detach_id != null) { stored_scroll_position = $(window).scrolltop(); $('#' + options.detach_id).css('display', 'none'); resizeimage(); } bindcurtainevents(); return $(document).trigger('fullsizable:opened'); }); }; closeviewer = function() { if (options.detach_id != null) { $('#' + options.detach_id).css('display', 'block'); $(window).scrolltop(stored_scroll_position); } $(container_id).fadeout(function() { return $image_holder.remove(); }); closefullscreen(); $(container_id).removeclass(spinner_class); unbindcurtainevents(); return $(window).unbind('resize', resizeimage); }; makefullsizable = function() { images.length = 0; console.log(options.selector); return $(options.selector).each(function() { var image; image = new image; image.buffer_src = $(this).attr('href'); image.index = images.length; images.push(image); if (options.openonclick) { return $(this).click(function(e) { e.preventdefault(); if (options.reloadonopen) { makefullsizable(); } return openviewer(image); }); } }); }; preparecurtain = function() { if (options.navigation) { $image_holder.append(''); $(document).on('click', '#fullsized_go_prev', function(e) { e.preventdefault(); e.stoppropagation(); return previmage(); }); $(document).on('click', '#fullsized_go_next', function(e) { e.preventdefault(); e.stoppropagation(); return nextimage(); }); } if (options.closebutton) { $image_holder.append(''); $(document).on('click', '#fullsized_close', function(e) { e.preventdefault(); e.stoppropagation(); return closeviewer(); }); } if (options.fullscreenbutton && hasfullscreensupport()) { $image_holder.append(''); $(document).on('click', '#fullsized_fullscreen', function(e) { e.preventdefault(); e.stoppropagation(); return togglefullscreen(); }); } switch (options.clickbehaviour) { case 'close': return $(document).on('click', container_id, closeviewer); case 'next': return $(document).on('click', container_id, function() { return nextimage(true); }); } }; bindcurtainevents = function() { $(document).bind('keydown', keypressed); $(document).bind('fullsizable:next', function() { return nextimage(true); }); $(document).bind('fullsizable:prev', function() { return previmage(true); }); return $(document).bind('fullsizable:close', closeviewer); }; unbindcurtainevents = function() { $(document).unbind('keydown', keypressed); $(document).unbind('fullsizable:next'); $(document).unbind('fullsizable:prev'); return $(document).unbind('fullsizable:close'); }; hidechrome = function() { var $chrome; $chrome = $image_holder.find('a'); if ($chrome.is(':visible') === true) { $chrome.toggle(false); return $image_holder.bind('mousemove', mousemovement); } }; mousestart = null; mousemovement = function(event) { var distance; if (mousestart === null) { mousestart = [event.clientx, event.clienty]; } distance = math.round(math.sqrt(math.pow(mousestart[1] - event.clienty, 2) + math.pow(mousestart[0] - event.clientx, 2))); if (distance >= 10) { $image_holder.unbind('mousemove', mousemovement); mousestart = null; return showchrome(); } }; showchrome = function() { $('#fullsized_close, #fullsized_fullscreen').toggle(true); $('#fullsized_go_prev').toggle(current_image !== 0); return $('#fullsized_go_next').toggle(current_image !== images.length - 1); }; $.fn.fullsizable = function(opts) { options = $.extend({ selector: ".bigimg", detach_id: null, navigation: true, closebutton: true, fullscreenbutton: true, openonclick: true, clickbehaviour: 'close', preload: true, reloadonopen: false }, opts || {}); preparecurtain(); if (options.preload) { makefullsizable(); } $(document).bind('fullsizable:reload', makefullsizable); $(document).bind('fullsizable:open', function(e, target) { var image, _i, _len, _results; if (options.reloadonopen) { makefullsizable(); } _results = []; for (_i = 0, _len = images.length; _i < _len; _i++) { image = images[_i]; if (image.buffer_src === $(target).attr('href')) { _results.push(openviewer(image)); } else { _results.push(void 0); } } return _results; }); return this; }; hasfullscreensupport = function() { var fs_dom; fs_dom = $image_holder.get(0); if (fs_dom.requestfullscreen || fs_dom.webkitrequestfullscreen || fs_dom.mozrequestfullscreen) { return true; } else { return false; } }; closefullscreen = function() { return togglefullscreen(true); }; togglefullscreen = function(force_close) { var fs_dom; fs_dom = $image_holder.get(0); if (fs_dom.requestfullscreen) { if (document.fullscreen || force_close) { return document.exitfullscreen(); } else { return fs_dom.requestfullscreen(); } } else if (fs_dom.webkitrequestfullscreen) { if (document.webkitisfullscreen || force_close) { return document.webkitcancelfullscreen(); } else { return fs_dom.webkitrequestfullscreen(); } } else if (fs_dom.mozrequestfullscreen) { if (document.mozfullscreen || force_close) { return document.mozcancelfullscreen(); } else { return fs_dom.mozrequestfullscreen(); } } }; }).call(this); /* 本代码由素材家园收集并编辑整理; 尊重他人劳动成果; 转载请保留素材家园链接 - www.sucaijiayuan.com */