/*! picbox v2.2 (c) 2010 ben kay based on code from slimbox v1.7 - the ultimate lightweight lightbox clone (c) 2007-2009 christophe beyls uses jquery-mousewheel version: 3.0.2 (c) 2009 brandon aaron mit-style license. */ /* 代码整理:懒人之家 www.lanrenzhijia.com */ (function($) { var win = $(window), options, images, activeimage = -1, activeurl, previmage, nextimage, ie6 = ((window.xmlhttprequest == undefined) && (activexobject != undefined)), browseriscrap, middlex, middley, imagex, imagey, currentsize, initialsize, imagedrag, timer, fitsonscreen, // preload images preload = {}, preloadprev = new image(), preloadnext = new image(), // dom elements overlay, closebtn, image, prevbtn, nextbtn, leftbtn, rightbtn, bottom, caption, nav, number, // effects fxoverlay, fxresize, iebrowser = $.browser.msie && (($.browser.version == "6.0") || ($.browser.version == "7.0") || ($.browser.version == "8.0")), // css classes zoomed = "pbzoomed", greyed = "pbgreyed"; /* initialization */ $(document).ready(function() { $(document.body).append( $([ overlay = $('
').click(close).append( closebtn = $('
')[0] )[0], image = $('').dblclick(doubleclick)[0], bottom = $('
').append([ // caption = $('
')[0], $('
').append([ leftbtn = $('').click(rotateleft)[0], prevbtn = $('').click(previous)[0], zoombtn = $('').click(doubleclick)[0], leftbtn = $('').click(rotateright)[0], nextbtn = $('').click(next)[0] ])[0], number = $('
')[0] ])[0] ]).css("display", "none") ); browseriscrap = ie6 || (overlay.currentstyle && (overlay.currentstyle.position != "fixed")); if (browseriscrap) { $([overlay, closebtn, image, bottom]).css("position", "absolute"); } $(image).attr("data-rotate", 0); $(image).tinydrag(function() { var i = $(image), pos = i.position(); imagex = (pos.left - win.scrollleft()) + i.width() / 2; imagey = (pos.top - win.scrolltop()) + i.height() / 2; $(zoombtn).addclass(zoomed); }); }); $.picbox = function(_images, startimage, _options) { options = $.extend({ loop: false, // allows to navigate between first and last images overlayopacity: 0.8, // 1 is opaque, 0 is completely transparent (change the color in the css file) overlayfadeduration: 200, // duration of the overlay fade-in and fade-out animations (in milliseconds) resizeduration: 300, // duration of each of the image resize animations (in milliseconds) resizeeasing: "swing", // swing uses the jquery default easing controlsfadedelay: 3000, // time delay before controls fade when not moving the mouse (in milliseconds) countertext: false, // counter text. use {x} for current image and {y} for total e.g. image {x} of {y} hideflash: true, // hides flash elements on the page when picbox is activated. note: flash elements must have wmode parameter set to "opaque" or "transparent" if this is set to false closekeys: [27, 88, 67], // array of keycodes to close picbox, default: esc (27), 'x' (88), 'c' (67) previouskeys: [37, 80], // array of keycodes to navigate to the previous image, default: left arrow (37), 'p' (80) nextkeys: [39, 78], // array of keycodes to navigate to the next image, default: right arrow (39), 'n' (78) margins: 0 // margin between the image and the sides of the window (in pixels) }, _options || {}); // the function is called for a single image, with url and title as first two arguments if (typeof _images == "string") { _images = [[_images, startimage]]; startimage = 0; } $(overlay).css("opacity", 0).fadeto(options.overlayfadeduration, options.overlayopacity); $(bottom).css("display", ""); mousemove(); // so controls dissapear if even if mouse is never moved position(); setup(1); images = _images; options.loop = options.loop && (images.length > 1); return changeimage(startimage); } $.fn.picbox = function(_options, linkmapper, linksfilter) { linkmapper = linkmapper || function(el) { return [el.href, el.title]; }; linksfilter = linksfilter || function() { return true; }; var links = this; $(links).unbind("click").click(function() { var link = this, linksmapped = []; // build the list of images that will be displayed filteredlinks = $.grep(links, function(el) { return linksfilter.call(link, el); }); // can't use $.map() as it flattens array for (var i = 0; i < filteredlinks.length; i++) linksmapped[i] = linkmapper(filteredlinks[i]); return $.picbox(linksmapped, $.inarray(this, filteredlinks), _options); }); return links; } /* internal functions */ function position() { var scroll = {x: win.scrollleft(), y: win.scrolltop()} middlex = win.width() / 2; middley = win.height() / 2; if (browseriscrap) { middlex = middlex + scroll.x; middley = middley + scroll.y; $(overlay).css({left: scroll.x, top: scroll.y, width: win.width(), height: win.height()}); } $(image).css({top: middley, left: middlex, width: '1px', height: '1px'}); } function setup(open) { if (options.hideflash) { $.each(["object", "embed"], function(i, val) { $(val).each(function() { // jquery 1.4 doesn't allow .data() on object tags if (open) this._picbox = this.style.visibility; this.style.visibility = open ? "hidden" : this._picbox; }); }); } /* 代码整理:懒人之家 www.lanrenzhijia.com */ overlay.style.display = ""; var fn = open ? "bind" : "unbind"; $(document)[fn]("keydown", keydown); $(document)[fn]("mousewheel", scrollzoom); $(document)[fn]("mousemove", mousemove); $(bottom)[fn]("mouseover", function(){preventfade(1)}); $(bottom)[fn]("mouseout", preventfade); } function keydown(event) { var code = event.keycode; // prevent default keyboard action (like navigating inside the page) return $.inarray(code, options.closekeys) >= 0 ? close() : $.inarray(code, options.nextkeys) >= 0 ? next() : $.inarray(code, options.previouskeys) >= 0 ? previous() : false; } function mousemove() { flashfade([bottom, prevbtn, zoombtn, nextbtn, leftbtn, rightbtn]); } function flashfade(targets, out) { cleartimeout(timer); $(targets).fadein(); targets = out ? $.merge(targets, out) : targets; timer = settimeout(function(){$(targets).fadeout()}, options.controlsfadedelay); } function preventfade(over) { var fn = 1 == over ? "unbind" : "bind"; $(document)[fn]("mousemove", mousemove); cleartimeout(timer); } function previous() { return changeimage(previmage, true); } function next() { return changeimage(nextimage, true); } function rotateleft() { return rotateimage(false); } function rotateright() { return rotateimage(true); } function changeimage(imageindex, noanim) { if (imageindex >= 0) { activeimage = imageindex; activeurl = images[imageindex][0]; previmage = (activeimage || (options.loop ? images.length : 0)) - 1; nextimage = ((activeimage + 1) % images.length) || (options.loop ? 0 : -1); stop(); overlay.classname = "pbloading"; $(image).css("display", "none"); if (!images[activeimage][1]) $(caption).html("").hide(); else $(caption).html(images[activeimage][1]).show(); $(number).html((((images.length > 1) && options.countertext) || "").replace(/{x}/, activeimage + 1).replace(/{y}/, images.length)); if (previmage >= 0) {preloadprev.src = images[previmage][0]; $(prevbtn).removeclass(greyed);} if (nextimage >= 0) {preloadnext.src = images[nextimage][0]; $(nextbtn).removeclass(greyed);} preload = new image(); preload.onload = function(){showimage(noanim);}; preload.src = activeurl; } return false; } function showimage(noanim) { resetimagecenter(); var mw = win.width() - options.margins, mh = win.height() - options.margins, size = 1; if ((preload.width > mw) || (preload.height > mh)) { size = math.min(mw / preload.width, mh / preload.height); $(zoombtn).removeclass(greyed); fitsonscreen = false; } else { $(zoombtn).addclass(greyed); fitsonscreen = true; } currentsize = initialsize = size; resizeimage(size, noanim); $(image).attr("src", activeurl); $(image).css("display", ""); //$(image).rotate(0); $(image).rotateimg({"angle" : 0}); overlay.classname = ""; flashfade([bottom], [prevbtn, zoombtn, nextbtn]); } /* 代码整理:懒人之家 www.lanrenzhijia.com */ function resizeimage(to, noanim) { var amount = to / currentsize; imagex = middlex - (middlex - imagex) * amount; imagey = middley - (middley - imagey) * amount; currentsize = to; var width = preload.width * to, height = preload.height * to, // round these as some browsers don't like very small css values left = imagex - (width / 2) >> 0, top = imagey - (height / 2) >> 0, dur = noanim ? 0 : options.resizeduration, fn = (0 == to) ? function(){$(image).hide()}:function(){}; $(image).animate({width: width, height: height, top: top, left: left}, {queue:false, duration: dur, easing: options.resizeeasing, complete: fn}); return false; } /* 图片的旋转 */ function rotateimage(clockwise) { var deg = parseint($(image).attr("data-rotate")); if(clockwise){ deg = (deg + 1) % 4; }else{ if(deg == 0){ deg = 3; }else{ deg = (deg - 1) % 4; } } //$(image).rotate(deg * 90); $(image).rotateimg({"angle" : deg * 90}); $(image).attr("data-rotate", deg); return false; } function resetimagecenter() { imagex = middlex; imagey = middley; } function scrollzoom(e, delta) { $(zoombtn).addclass(zoomed); var to = currentsize + delta * currentsize / 10; return resizeimage(to); } function doubleclick() { if (currentsize == initialsize && imagex == middlex && imagey == middley && !fitsonscreen) { $(zoombtn).addclass(zoomed); return resizeimage(1); } else { $(zoombtn).removeclass(zoomed); resetimagecenter(); return resizeimage(initialsize); } } function stop() { preload.onload = function(){}; preload.src = preloadprev.src = preloadnext.src = activeurl; $(image).stop(); $([prevbtn, nextbtn]).addclass(greyed); $(zoombtn).removeclass(zoomed); } function close() { if (activeimage >= 0) { stop(); activeimage = previmage = nextimage = -1; resizeimage(0); setup(); $(bottom).stop().hide(); $(overlay).stop().fadeout(); } return false; } /*! * tinydrag v0.9.2 * (c) 2010 ben kay */ $.fn.tinydrag = function(callback) { return $.tinydrag(this, callback); } $.tinydrag = function(el, callback) { var mousestart, elstart, moved, doc = $(document), abs = math.abs; el.mousedown(function(e) { moved = false; mousestart = {x: e.pagex, y: e.pagey}; elstart = {x: parseint(el.css("left")), y: parseint(el.css("top"))} doc.mousemove(drag).mouseup(stop); return false; }); function drag(e) { var x = e.pagex, y = e.pagey; if (moved) { el.css({left: elstart.x + (x - mousestart.x), top: elstart.y + (y - mousestart.y)}); } else { if (abs(x - mousestart.x) > 1 || abs(y - mousestart.y) > 1) moved = true; } return false; } function stop() { doc.unbind("mousemove", drag).unbind("mouseup"); moved&&callback&&callback() } return el; } /*! * jquery mousewheel * version: 3.0.2 * (c) 2009 brandon aaron (http://brandonaaron.net) */ var types = ['dommousescroll', 'mousewheel']; $.event.special.mousewheel = { setup: function() { if ( this.addeventlistener ) for ( var i=types.length; i; ) this.addeventlistener( types[--i], handler, false ); else this.onmousewheel = handler; }, teardown: function() { if ( this.removeeventlistener ) for ( var i=types.length; i; ) this.removeeventlistener( types[--i], handler, false ); else this.onmousewheel = null; } }; $.fn.extend({ mousewheel: function(fn) { return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel"); }, unmousewheel: function(fn) { return this.unbind("mousewheel", fn); } }); function handler(event) { var args = [].slice.call( arguments, 1 ), delta = 0, returnvalue = true; event = $.event.fix(event || window.event); event.type = "mousewheel"; if ( event.wheeldelta ) delta = event.wheeldelta/120; if ( event.detail ) delta = -event.detail/3; // add events and delta to the front of the arguments args.unshift(event, delta); return $.event.handle.apply(this, args); } /*! * rotateimg * by van */ $.fn.rotateimg = function(options){ var defaults = { scale:1, angle:0 } var options = $.extend(defaults, options); this.each(function(){ var $img=$(this); var angle = parsefloat(options.angle) || 0; var rad = angle * (math.pi / 180); var m11 = math.cos(rad) * options.scale, m12 = -1 * math.sin(rad) * options.scale, m21 = math.sin(rad) * options.scale, m22 = m11; if (iebrowser) { $img.css("filter", "progid:dximagetransform.microsoft.matrix(m11="+ m11 +",m12="+ m12 +",m21="+ m21 +",m22="+ m22 +",sizingmethod='auto expand')"); }else{ $img.css({ "transform" : "rotate(" + options.angle + "deg)", "-ms-transform" : "rotate(" + options.angle + "deg)", "-webkit-transform" : "rotate(" + options.angle + "deg)", "-o-transform" : "rotate(" + options.angle + "deg)", "-moz-transform" : "rotate(" + options.angle + "deg)" }); } }); }; })(jquery); if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.useragent)) { jquery(function($) { $("a[rel^='lightbox']").picbox({/* put custom options here */}, null, function(el) { return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel)); }); }); } /* 代码整理:懒人之家 www.lanrenzhijia.com */