📄 jquery.jcarousel.js
字号:
/** * jCarousel - Riding carousels with jQuery * http://sorgalla.com/jcarousel/ * * Copyright (c) 2006 Jan Sorgalla (http://sorgalla.com) * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * * Built on top of the jQuery library * http://jquery.com * * Inspired by the "Carousel Component" by Bill Scott * http://billwscott.com/carousel/ */(function($) { /** * Creates a carousel for all matched elements. * * @example $("#mycarousel").jcarousel(); * @before <ul id="mycarousel"><li>First item</li><li>Second item</li></ul> * @result * * <div class="jcarousel-skin-name jcarousel-container"> * <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div> * <div class="jcarousel-next"></div> * <div class="jcarousel-clip"> * <ul class="jcarousel-list"> * <li class="jcarousel-item-1">First item</li> * <li class="jcarousel-item-2">Second item</li> * </ul> * </div> * </div> * * @name jcarousel * @type jQuery * @param Hash o A set of key/value pairs to set as configuration properties. * @cat Plugins/jCarousel */ $.fn.jcarousel = function(o) { return this.each(function() { new $jc(this, o); }); }; // Default configuration properties. var defaults = { vertical: false, start: 1, offset: 1, size: null, scroll: 3, visible: null, animation: 'normal', easing: 'swing', auto: 0, wrap: null, initCallback: null, reloadCallback: null, itemLoadCallback: null, itemFirstInCallback: null, itemFirstOutCallback: null, itemLastInCallback: null, itemLastOutCallback: null, itemVisibleInCallback: null, itemVisibleOutCallback: null, buttonNextHTML: '<div></div>', buttonPrevHTML: '<div></div>', buttonNextEvent: 'click', buttonPrevEvent: 'click', buttonNextCallback: null, buttonPrevCallback: null }; /** * The jCarousel object. * * @constructor * @name $.jcarousel * @param Object e The element to create the carousel for. * @param Hash o A set of key/value pairs to set as configuration properties. * @cat Plugins/jCarousel */ $.jcarousel = function(e, o) { this.options = $.extend({}, defaults, o || {}); this.locked = false; this.container = null; this.clip = null; this.list = null; this.buttonNext = null; this.buttonPrev = null; this.wh = !this.options.vertical ? 'width' : 'height'; this.lt = !this.options.vertical ? 'left' : 'top'; if (e.nodeName == 'UL' || e.nodeName == 'OL') { this.list = $(e); this.container = this.list.parent(); if ($.className.has(this.container[0].className, 'jcarousel-clip')) { if (!$.className.has(this.container[0].parentNode.className, 'jcarousel-container')) this.container = this.container.wrap('<div></div>'); this.container = this.container.parent(); } else if (!$.className.has(this.container[0].className, 'jcarousel-container')) this.container = this.list.wrap('<div></div>').parent(); // Move skin class over to container var split = e.className.split(' '); for (var i = 0; i < split.length; i++) { if (split[i].indexOf('jcarousel-skin') != -1) { this.list.removeClass(split[i]); this.container.addClass(split[i]); break; } } } else { this.container = $(e); this.list = $(e).children('ul,ol'); } this.clip = this.list.parent(); if (!this.clip.length || !$.className.has(this.clip[0].className, 'jcarousel-clip')) this.clip = this.list.wrap('<div></div>').parent(); this.buttonPrev = $('.jcarousel-prev', this.container); if (this.buttonPrev.size() == 0 && this.options.buttonPrevHTML != null) this.buttonPrev = this.clip.before(this.options.buttonPrevHTML).prev(); this.buttonPrev.addClass(this.className('jcarousel-prev')); this.buttonNext = $('.jcarousel-next', this.container); if (this.buttonNext.size() == 0 && this.options.buttonNextHTML != null) this.buttonNext = this.clip.before(this.options.buttonNextHTML).prev(); this.buttonNext.addClass(this.className('jcarousel-next')); this.clip.addClass(this.className('jcarousel-clip')); this.list.addClass(this.className('jcarousel-list')); this.container.addClass(this.className('jcarousel-container')); var di = this.options.visible != null ? Math.ceil(this.clipping() / this.options.visible) : null; var li = this.list.children('li'); var self = this; if (li.size() > 0) { var wh = 0, i = this.options.offset; li.each(function() { self.format(this, i++); wh += self.dimension(this, di); }); this.list.css(this.wh, wh + 'px'); // Only set if not explicitly passed as option if (!o || o.size == undefined) this.options.size = li.size(); } // For whatever reason, .show() does not work in Safari... this.container.css('display', 'block'); this.buttonNext.css('display', 'block'); this.buttonPrev.css('display', 'block'); this.funcNext = function() { self.next(); }; this.funcPrev = function() { self.prev(); }; $(window).bind('resize', function() { self.reload(); }); if (this.options.initCallback != null) this.options.initCallback(this, 'init'); this.setup(); }; // Create shortcut for internal use var $jc = $.jcarousel; $jc.fn = $jc.prototype = { jcarousel: '0.2.2' }; $jc.fn.extend = $jc.extend = $.extend; $jc.fn.extend({ /** * Setups the carousel. * * @name setup * @type undefined * @cat Plugins/jCarousel */ setup: function() { this.first = null; this.last = null; this.prevFirst = null; this.prevLast = null; this.animating = false; this.timer = null; this.tail = null; this.inTail = false; if (this.locked) return; this.list.css(this.lt, this.pos(this.options.offset) + 'px'); var p = this.pos(this.options.start); this.prevFirst = this.prevLast = null; this.animate(p, false); }, /** * Clears the list and resets the carousel. * * @name reset * @type undefined * @cat Plugins/jCarousel */ reset: function() { this.list.empty(); this.list.css(this.lt, '0px'); this.list.css(this.wh, '0px'); if (this.options.initCallback != null) this.options.initCallback(this, 'reset'); this.setup(); }, /** * Reloads the carousel and adjusts positions. * * @name reload * @type undefined * @cat Plugins/jCarousel */ reload: function() { if (this.tail != null && this.inTail) this.list.css(this.lt, $jc.intval(this.list.css(this.lt)) + this.tail); this.tail = null; this.inTail = false; if (this.options.reloadCallback != null) this.options.reloadCallback(this); if (this.options.visible != null) { var self = this; var di = Math.ceil(this.clipping() / this.options.visible), wh = 0, lt = 0; $('li', this.list).each(function(i) { wh += self.dimension(this, di); if (i + 1 < self.first) lt = wh; }); this.list.css(this.wh, wh + 'px'); this.list.css(this.lt, -lt + 'px'); } this.scroll(this.first, false); }, /** * Locks the carousel. * * @name lock * @type undefined * @cat Plugins/jCarousel */ lock: function() { this.locked = true; this.buttons(); }, /** * Unlocks the carousel. * * @name unlock * @type undefined * @cat Plugins/jCarousel */ unlock: function() { this.locked = false; this.buttons(); }, /** * Sets the size of the carousel. * * @name size * @type undefined * @param Number s The size of the carousel. * @cat Plugins/jCarousel */ size: function(s) { if (s != undefined) { this.options.size = s; if (!this.locked) this.buttons(); } return this.options.size; }, /** * Checks whether a list element exists for the given index (or index range). * * @name get * @type bool * @param Number i The index of the (first) element. * @param Number i2 The index of the last element. * @cat Plugins/jCarousel */ has: function(i, i2) { if (i2 == undefined || !i2) i2 = i; for (var j = i; j <= i2; j++) { var e = this.get(j).get(0); if (!e || $.className.has(e, 'jcarousel-item-placeholder')) return false; } return true; }, /** * Returns a jQuery object with list element for the given index. * * @name get * @type jQuery * @param Number i The index of the element. * @cat Plugins/jCarousel */ get: function(i) { return $('.jcarousel-item-' + i, this.list); }, /** * Adds an element for the given index to the list. * If the element already exists, it updates the inner html. * Returns the created element as jQuery object. * * @name add * @type jQuery * @param Number i The index of the element. * @param String s The innerHTML of the element. * @cat Plugins/jCarousel */ add: function(i, s) { var e = this.get(i), old = 0; if (e.length == 0) { var c, e = this.create(i), j = $jc.intval(i); while (c = this.get(--j)) { if (j <= 0 || c.length) { j <= 0 ? this.list.prepend(e) : c.after(e); break; } } } else old = this.dimension(e); e.removeClass(this.className('jcarousel-item-placeholder')); typeof s == 'string' ? e.html(s) : e.empty().append(s); var di = this.options.visible != null ? Math.ceil(this.clipping() / this.options.visible) : null; var wh = this.dimension(e, di) - old; if (i > 0 && i < this.first) this.list.css(this.lt, $jc.intval(this.list.css(this.lt)) + wh + 'px'); this.list.css(this.wh, $jc.intval(this.list.css(this.wh)) + wh + 'px'); return e; }, /** * Removes an element for the given index from the list. * * @name remove * @type undefined * @param Number i The index of the element. * @cat Plugins/jCarousel */ remove: function(i) { var e = this.get(i); // Check if item exists and is not currently visible if (!e.length || (i >= this.first && i <= this.last)) return; var d = this.dimension(e); if (i < this.first) this.list.css(this.lt, $jc.intval(this.list.css(this.lt)) + d + 'px'); e.remove(); this.list.css(this.wh, $jc.intval(this.list.css(this.wh)) - d + 'px'); }, /** * Moves the carousel forwards. * * @name next * @type undefined * @cat Plugins/jCarousel */ next: function() { this.stopAuto(); if (this.tail != null && !this.inTail) this.scrollTail(false); else this.scroll(((this.options.wrap == 'both' || this.options.wrap == 'last') && this.options.size != null && this.last == this.options.size) ? 1 : this.first + this.options.scroll); }, /** * Moves the carousel backwards. * * @name prev * @type undefined * @cat Plugins/jCarousel */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -