/** * copyright (c) 2010 anders ekdahl (http://coffeescripter.com/) * dual licensed under the mit (http://www.opensource.org/licenses/mit-license.php) * and gpl (http://www.opensource.org/licenses/gpl-license.php) licenses. * * version: 1.2.4 * * */ (function ($) { $.fn.adgallery = function (options) { var defaults = { loader_image: '/scripts/pic_jquery/img/loader.gif', start_at_index: 0, description_wrapper: false, thumb_opacity: 0.7, animate_first_image: false, animation_speed: 400, width: false, height: false, display_next_and_prev: true, display_back_and_forward: true, scroll_jump: 0, // if 0, it jumps the width of the container slideshow: { enable: true, autostart: false, speed: 5000, start_label: '播放', stop_label: '停止', stop_on_scroll: true, countdown_prefix: '(', countdown_sufix: ')', onstart: false, onstop: false }, effect: 'slide-hori', // or 'slide-vert', 'fade', or 'resize', 'none' enable_keyboard_move: true, cycle: true, callbacks: { init: false, afterimagevisible: false, beforeimagevisible: false } }; var settings = $.extend(false, defaults, options); if (options && options.slideshow) { settings.slideshow = $.extend(false, defaults.slideshow, options.slideshow); }; if (!settings.slideshow.enable) { settings.slideshow.autostart = false; }; var galleries = []; $(this).each(function () { var gallery = new adgallery(this, settings); galleries[galleries.length] = gallery; }); // sorry, breaking the jquery chain because the gallery instances // are returned so you can fiddle with them return galleries; }; function verticalslideanimation(img_container, direction, desc) { var current_top = parseint(img_container.css('top'), 10); if (direction == 'left') { var old_image_top = '-' + this.image_wrapper_height + 'px'; img_container.css('top', this.image_wrapper_height + 'px'); } else { var old_image_top = this.image_wrapper_height + 'px'; img_container.css('top', '-' + this.image_wrapper_height + 'px'); }; if (desc) { desc.css('bottom', '-' + desc[0].offsetheight + 'px'); desc.animate({ bottom: 0 }, this.settings.animation_speed * 2); }; if (this.current_description) { this.current_description.animate({ bottom: '-' + this.current_description[0].offsetheight + 'px' }, this.settings.animation_speed * 2); }; return { old_image: { top: old_image_top }, new_image: { top: current_top } }; }; function horizontalslideanimation(img_container, direction, desc) { var current_left = parseint(img_container.css('left'), 10); if (direction == 'left') { var old_image_left = '-' + this.image_wrapper_width + 'px'; img_container.css('left', this.image_wrapper_width + 'px'); } else { var old_image_left = this.image_wrapper_width + 'px'; img_container.css('left', '-' + this.image_wrapper_width + 'px'); }; if (desc) { desc.css('bottom', '-' + desc[0].offsetheight + 'px'); desc.animate({ bottom: 0 }, this.settings.animation_speed * 2); }; if (this.current_description) { this.current_description.animate({ bottom: '-' + this.current_description[0].offsetheight + 'px' }, this.settings.animation_speed * 2); }; return { old_image: { left: old_image_left }, new_image: { left: current_left } }; }; function resizeanimation(img_container, direction, desc) { var image_width = img_container.width(); var image_height = img_container.height(); var current_left = parseint(img_container.css('left'), 10); var current_top = parseint(img_container.css('top'), 10); img_container.css({ width: 0, height: 0, top: this.image_wrapper_height / 2, left: this.image_wrapper_width / 2 }); return { old_image: { width: 0, height: 0, top: this.image_wrapper_height / 2, left: this.image_wrapper_width / 2 }, new_image: { width: image_width, height: image_height, top: current_top, left: current_left } }; }; function fadeanimation(img_container, direction, desc) { img_container.css('opacity', 0); return { old_image: { opacity: 0 }, new_image: { opacity: 1 } }; }; // sort of a hack, will clean this up... eventually function noneanimation(img_container, direction, desc) { img_container.css('opacity', 0); return { old_image: { opacity: 0 }, new_image: { opacity: 1 }, speed: 0 }; }; function adgallery(wrapper, settings) { this.init(wrapper, settings); }; adgallery.prototype = { // elements wrapper: false, image_wrapper: false, gallery_info: false, nav: false, loader: false, preloads: false, thumbs_wrapper: false, scroll_back: false, scroll_forward: false, next_link: false, prev_link: false, slideshow: false, image_wrapper_width: 0, image_wrapper_height: 0, current_index: 0, current_image: false, current_description: false, nav_display_width: 0, settings: false, images: false, in_transition: false, animations: false, init: function (wrapper, settings) { var context = this; this.wrapper = $(wrapper); this.settings = settings; this.setupelements(); this.setupanimations(); if (this.settings.width) { this.image_wrapper_width = this.settings.width; this.image_wrapper.width(this.settings.width); this.wrapper.width(this.settings.width); } else { this.image_wrapper_width = this.image_wrapper.width(); }; if (this.settings.height) { this.image_wrapper_height = this.settings.height; this.image_wrapper.height(this.settings.height); } else { this.image_wrapper_height = this.image_wrapper.height(); }; this.nav_display_width = this.nav.width(); this.current_index = 0; this.current_image = false; this.current_description = false; this.in_transition = false; this.findimages(); if (this.settings.display_next_and_prev) { this.initnextandprev(); }; // the slideshow needs a callback to trigger the next image to be shown // but we don't want to give it access to the whole gallery instance var nextimage_callback = function (callback) { return context.nextimage(callback); }; this.slideshow = new adgalleryslideshow(nextimage_callback, this.settings.slideshow); this.controls.append(this.slideshow.create()); if (this.settings.slideshow.enable) { this.slideshow.enable(); } else { this.slideshow.disable(); }; if (this.settings.display_back_and_forward) { this.initbackandforward(); }; if (this.settings.enable_keyboard_move) { this.initkeyevents(); }; var start_at = parseint(this.settings.start_at_index, 10); if (window.location.hash && window.location.hash.indexof('#ad-image') === 0) { start_at = window.location.hash.replace(/[^0-9]+/g, ''); // check if it's a number if ((start_at * 1) != start_at) { start_at = this.settings.start_at_index; }; }; this.loading(true); this.showimage(start_at, function () { // we don't want to start the slideshow before the image has been // displayed if (context.settings.slideshow.autostart) { context.preloadimage(start_at + 1); context.slideshow.start(); }; } ); this.firecallback(this.settings.callbacks.init); }, setupanimations: function () { this.animations = { 'slide-vert': verticalslideanimation, 'slide-hori': horizontalslideanimation, 'resize': resizeanimation, 'fade': fadeanimation, 'none': noneanimation }; }, setupelements: function () { this.controls = this.wrapper.find('.ad-controls'); this.gallery_info = $('
'); this.controls.append(this.gallery_info); this.image_wrapper = this.wrapper.find('.ad-image-wrapper'); this.image_wrapper.empty(); this.nav = this.wrapper.find('.ad-nav'); this.thumbs_wrapper = this.nav.find('.ad-thumbs'); this.preloads = $(''); this.loader = $('' + title + desc + '
'); }; return desc; }, /** * @param function callback gets fired when the image has loaded, is displaying * and it's animation has finished */ showimage: function (index, callback) { if (this.images[index] && !this.in_transition) { var context = this; var image = this.images[index]; this.in_transition = true; if (!image.preloaded) { this.loading(true); this.preloadimage(index, function () { context.loading(false); context._showwhenloaded(index, callback); }); } else { this._showwhenloaded(index, callback); }; }; }, /** * @param function callback gets fired when the image has loaded, is displaying * and it's animation has finished */ _showwhenloaded: function (index, callback) { if (this.images[index]) { var context = this; var image = this.images[index]; var img_container = $(document.createelement('div')).addclass('ad-image'); var img = $(new image()).attr('src', image.image); if (image.link) { var link = $(''); link.append(img); img_container.append(link); } else { img_container.append(img); } this.image_wrapper.prepend(img_container); var size = this._getcontainedimagesize(image.size.width, image.size.height); img.attr('width', size.width); img.attr('height', size.height); img_container.css({ width: size.width + 'px', height: size.height + 'px' }); this._centerimage(img_container, size.width, size.height); var desc = this._getdescription(image, img_container); this.settings.description_wrapper = $('#descriptions');//强制内容加载在外容器 外容器id #descriptions if (desc) { if (!this.settings.description_wrapper) { img_container.append(desc); var width = size.width - parseint(desc.css('padding-left'), 10) - parseint(desc.css('padding-right'), 10); desc.css('width', width + 'px'); } else { this.settings.description_wrapper.append(desc); } }; this.highlightthumb(this.nav.find('.ad-thumb' + index)); var direction = 'right'; if (this.current_index < index) { direction = 'left'; }; this.firecallback(this.settings.callbacks.beforeimagevisible); if (this.current_image || this.settings.animate_first_image) { var animation_speed = this.settings.animation_speed; var easing = 'swing'; var animation = this.animations[this.settings.effect].call(this, img_container, direction, desc); if (typeof animation.speed != 'undefined') { animation_speed = animation.speed; }; if (typeof animation.easing != 'undefined') { easing = animation.easing; }; if (this.current_image) { var old_image = this.current_image; var old_description = this.current_description; old_image.animate(animation.old_image, animation_speed, easing, function () { old_image.remove(); if (old_description) old_description.remove(); } ); }; img_container.animate(animation.new_image, animation_speed, easing, function () { context.current_index = index; context.current_image = img_container; context.current_description = desc; context.in_transition = false; context._aftershow(); context.firecallback(callback); } ); } else { this.current_index = index; this.current_image = img_container; context.current_description = desc; this.in_transition = false; context._aftershow(); this.firecallback(callback); }; }; }, nextindex: function () { if (this.current_index == (this.images.length - 1)) { if (!this.settings.cycle) { return false; }; var next = 0; } else { var next = this.current_index + 1; }; return next; }, nextimage: function (callback) { var next = this.nextindex(); if (next === false) return false; this.preloadimage(next + 1); this.showimage(next, callback); return true; }, previndex: function () { if (this.current_index == 0) { if (!this.settings.cycle) { return false; }; var prev = this.images.length - 1; } else { var prev = this.current_index - 1; }; return prev; }, previmage: function (callback) { var prev = this.previndex(); if (prev === false) return false; this.preloadimage(prev - 1); this.showimage(prev, callback); return true; }, preloadall: function () { var context = this; var i = 0; function preloadnext() { if (i < context.images.length) { i++; context.preloadimage(i, preloadnext); }; }; context.preloadimage(i, preloadnext); }, preloadimage: function (index, callback) { if (this.images[index]) { var image = this.images[index]; if (!this.images[index].preloaded) { var img = $(new image()); img.attr('src', image.image); if (!this.isimageloaded(img[0])) { this.preloads.append(img); var context = this; img.load( function () { image.preloaded = true; image.size = { width: this.width, height: this.height }; context.firecallback(callback); } ).error( function () { image.error = true; image.preloaded = false; image.size = false; } ); } else { image.preloaded = true; image.size = { width: img[0].width, height: img[0].height }; this.firecallback(callback); }; } else { this.firecallback(callback); }; }; }, isimageloaded: function (img) { if (typeof img.complete != 'undefined' && !img.complete) { return false; }; if (typeof img.naturalwidth != 'undefined' && img.naturalwidth == 0) { return false; }; return true; }, highlightthumb: function (thumb) { this.thumbs_wrapper.find('.ad-active').removeclass('ad-active'); thumb.addclass('ad-active'); if (this.settings.thumb_opacity < 1) { this.thumbs_wrapper.find('a:not(.ad-active) img').fadeto(300, this.settings.thumb_opacity); thumb.find('img').fadeto(300, 1); }; var left = thumb[0].parentnode.offsetleft; left -= (this.nav_display_width / 2) - (thumb[0].offsetwidth / 2); this.thumbs_wrapper.animate({ scrollleft: left + 'px' }); }, firecallback: function (fn) { if ($.isfunction(fn)) { fn.call(this); }; } }; function adgalleryslideshow(nextimage_callback, settings) { this.init(nextimage_callback, settings); }; adgalleryslideshow.prototype = { start_link: false, stop_link: false, countdown: false, controls: false, settings: false, nextimage_callback: false, enabled: false, running: false, countdown_interval: false, init: function (nextimage_callback, settings) { var context = this; this.nextimage_callback = nextimage_callback; this.settings = settings; }, create: function () { this.start_link = $(' '); this.stop_link = $(' '); this.countdown = $(''); this.controls = $(''); this.controls.append(this.start_link).append(this.stop_link).append(this.countdown); this.countdown.hide(); var context = this; this.start_link.click( function () { context.start(); } ); this.stop_link.click( function () { context.stop(); } ); $(document).keydown( function (e) { if (e.keycode == 83) { // 's' if (context.running) { context.stop(); } else { context.start(); }; }; } ); return this.controls; }, disable: function () { this.enabled = false; this.stop(); this.controls.hide(); }, enable: function () { this.enabled = true; this.controls.show(); }, toggle: function () { if (this.enabled) { this.disable(); } else { this.enable(); }; }, start: function () { if (this.running || !this.enabled) return false; var context = this; this.running = true; this.controls.addclass('ad-slideshow-running'); this._next(); this.firecallback(this.settings.onstart); return true; }, stop: function () { if (!this.running) return false; this.running = false; this.countdown.hide(); this.controls.removeclass('ad-slideshow-running'); clearinterval(this.countdown_interval); this.firecallback(this.settings.onstop); return true; }, _next: function () { var context = this; var pre = this.settings.countdown_prefix; var su = this.settings.countdown_sufix; clearinterval(context.countdown_interval); this.countdown.show().html(pre + (this.settings.speed / 1000) + su); var slide_timer = 0; this.countdown_interval = setinterval( function () { slide_timer += 1000; if (slide_timer >= context.settings.speed) { var whennextisshown = function () { // a check so the user hasn't stoped the slideshow during the // animation if (context.running) { context._next(); //处理 自动播放 图片说明 processimgdes($(".ad-info").text().split('/')[0].trim() * 1); }; slide_timer = 0; }; if (!context.nextimage_callback(whennextisshown)) { context.stop(); }; slide_timer = 0; }; var sec = parseint(context.countdown.text().replace(/[^0-9]/g, ''), 10); sec--; if (sec > 0) { context.countdown.html(pre + sec + su); }; }, 1000 ); }, firecallback: function (fn) { if ($.isfunction(fn)) { fn.call(this); }; } }; })(jquery); if (!string.prototype.trim) { //判断下浏览器是否自带有trim()方法 string.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); } } function processimgdes(e) { var nexta2 = ''; nexta2 = $($(".ad-thumb-list li:eq(" + (e - 1) + ")").find("a")[0]); $("#descriptions").text(nexta2.attr("des")); } $(function () { $(".ad-thumb-list li a").click(function () { $("#descriptions").text($(this).attr("des")); }) $(document).on('click', ".ad-next-image", function () { var totalimg = $(".ad-thumb-list > li").length; var curindex = $(".ad-info").text().split('/')[0].trim() * 1; var txt = ""; var nexta = ''; console.log(curindex); if (curindex == totalimg) { nexta = $($(".ad-thumb-list li:eq(0)").find("a")[0]); } else { nexta = $($(".ad-thumb-list li:eq(" + curindex + ")").find("a")[0]); } txt = nexta.attr("des"); $("#descriptions").text(txt); }) $(document).on('click', ".ad-prev-image", function () { var totalimg1 = $(".ad-thumb-list > li").length; var curindex1 = $(".ad-info").text().split('/')[0].trim() * 1; var txt1 = ""; var nexta1 = ''; console.log(curindex1); if (curindex1 == 1) { nexta1 = $($(".ad-thumb-list li:eq(" + (totalimg1 - 1) + ")").find("a")[0]); } else { nexta1 = $($(".ad-thumb-list li:eq(" + (curindex1 - 2) + ")").find("a")[0]); } txt1 = nexta1.attr("des"); $("#descriptions").text(txt1); }) })