carousel-beta.js
来自「这是YUI的源码及相关示例。里面有很多很炫的Javascript效果。」· JavaScript 代码 · 共 2,061 行 · 第 1/5 页
JS
2,061 行
*/ blur: function () { this._carouselEl.blur(); this.fireEvent(blurEvent); }, /** * Clears the items from Carousel. * * @method clearItems * public */ clearItems: function () { var n = this.get("numItems"); while (n > 0) { this.removeItem(0); n--; } }, /** * Set focus on the Carousel. * * @method focus * @public */ focus: function () { var selItem, numVisible, selectOnScroll, selected, first, last, isSelectionInvisible, focusEl, itemsTable; if (this._isAnimationInProgress) { // this messes up real bad! return; } selItem = this.get("selectedItem"); numVisible = this.get("numVisible"); selectOnScroll = this.get("selectOnScroll"); selected = this.getItem(selItem); first = this.get("firstVisible"); last = first + numVisible - 1; isSelectionInvisible = (selItem < first || selItem > last); focusEl = (selected && selected.id) ? Dom.get(selected.id) : null; itemsTable = this._itemsTable; if (!selectOnScroll && isSelectionInvisible) { focusEl = (itemsTable && itemsTable.items && itemsTable.items[first]) ? Dom.get(itemsTable.items[first].id) : null; } if (focusEl) { try { focusEl.focus(); } catch (ex) { // ignore focus errors } } this.fireEvent(focusEvent); }, /** * Hide the Carousel. * * @method hide * @public */ hide: function () { if (this.fireEvent(beforeHideEvent) !== false) { this.removeClass(this.CLASSES.VISIBLE); this.fireEvent(hideEvent); } }, /** * Initialize the Carousel. * * @method init * @public * @param el {HTMLElement | String} The html element that represents * the Carousel container. * @param attrs {Object} The set of configuration attributes for * creating the Carousel. */ init: function (el, attrs) { var elId = el, // save for a rainy day parse = false; if (!el) { return; } this._itemsTable = { loading: {}, numItems: 0, items: [], size: 0 }; if (JS.isString(el)) { el = Dom.get(el); } else if (!el.nodeName) { return; } if (el) { if (!el.id) { // in case the HTML element is passed el.setAttribute("id", Dom.generateId()); } this._parseCarousel(el); parse = true; } else { el = this._createCarousel(elId); } elId = el.id; Carousel.superclass.init.call(this, el, attrs); this.initEvents(); if (parse) { this._parseCarouselItems(); } if (!attrs || typeof attrs.isVertical == "undefined") { this.set("isVertical", false); } this._parseCarouselNavigation(el); this._navEl = this._setupCarouselNavigation(); instances[elId] = this; loadItems.call(this); }, /** * Initialize the configuration attributes used to create the Carousel. * * @method initAttributes * @public * @param attrs {Object} The set of configuration attributes for * creating the Carousel. */ initAttributes: function (attrs) { attrs = attrs || {}; Carousel.superclass.initAttributes.call(this, attrs); /** * @attribute currentPage * @description The current page number (read-only.) * @type Number */ this.setAttributeConfig("currentPage", { readOnly : true, value : 0 }); /** * @attribute firstVisible * @description The index to start the Carousel from (indexes begin * from zero) * @default 0 * @type Number */ this.setAttributeConfig("firstVisible", { method : this._setFirstVisible, validator : this._validateFirstVisible, value : attrs.firstVisible || this.CONFIG.FIRST_VISIBLE }); /** * @attribute selectOnScroll * @description Set this to true to automatically set focus to * follow scrolling in the Carousel. * @default true * @type Boolean */ this.setAttributeConfig("selectOnScroll", { validator : JS.isBoolean, value : attrs.selectOnScroll || true }); /** * @attribute numVisible * @description The number of visible items in the Carousel's * viewport. * @default 3 * @type Number */ this.setAttributeConfig("numVisible", { method : this._setNumVisible, validator : this._validateNumVisible, value : attrs.numVisible || this.CONFIG.NUM_VISIBLE }); /** * @attribute numItems * @description The number of items in the Carousel. * @type Number */ this.setAttributeConfig("numItems", { method : this._setNumItems, validator : this._validateNumItems, value : this._itemsTable.numItems }); /** * @attribute scrollIncrement * @description The number of items to scroll by for arrow keys. * @default 1 * @type Number */ this.setAttributeConfig("scrollIncrement", { validator : this._validateScrollIncrement, value : attrs.scrollIncrement || 1 }); /** * @attribute selectedItem * @description The index of the selected item. * @type Number */ this.setAttributeConfig("selectedItem", { method : this._setSelectedItem, validator : JS.isNumber, value : 0 }); /** * @attribute revealAmount * @description The percentage of the item to be revealed on each * side of the Carousel (before and after the first and last item * in the Carousel's viewport.) * @default 0 * @type Number */ this.setAttributeConfig("revealAmount", { method : this._setRevealAmount, validator : this._validateRevealAmount, value : attrs.revealAmount || 0 }); /** * @attribute isCircular * @description Set this to true to wrap scrolling of the contents * in the Carousel. * @default false * @type Boolean */ this.setAttributeConfig("isCircular", { validator : JS.isBoolean, value : attrs.isCircular || false }); /** * @attribute isVertical * @description True if the orientation of the Carousel is vertical * @default false * @type Boolean */ this.setAttributeConfig("isVertical", { method : this._setOrientation, validator : JS.isBoolean, value : attrs.isVertical || false }); /** * @attribute navigation * @description The set of navigation controls for Carousel * @default <br> * { prev: null, // the previous navigation element<br> * next: null } // the next navigation element * @type Object */ this.setAttributeConfig("navigation", { method : this._setNavigation, validator : this._validateNavigation, value : attrs.navigation || { prev: null, next: null, page: null } }); /** * @attribute animation * @description The optional animation attributes for the Carousel. * @default <br> * { speed: 0, // the animation speed (in seconds)<br> * effect: null } // the animation effect (like * YAHOO.util.Easing.easeOut) * @type Object */ this.setAttributeConfig("animation", { validator : this._validateAnimation, value : attrs.animation || { speed: 0, effect: null } }); /** * @attribute autoPlay * @description Set this to time in milli-seconds to have the * Carousel automatically scroll the contents. * @type Number */ this.setAttributeConfig("autoPlay", { validator : JS.isNumber, value : attrs.autoPlay || 0 }); }, /** * Initialize and bind the event handlers. * * @method initEvents * @public */ initEvents: function () { var cssClass = this.CLASSES; this.on("keydown", this._keyboardEventHandler); this.subscribe(afterScrollEvent, syncNavigation); this.on(afterScrollEvent, this.focus); this.subscribe(itemAddedEvent, syncUI); this.subscribe(itemAddedEvent, syncNavigation); this.subscribe(itemRemovedEvent, syncUI); this.subscribe(itemRemovedEvent, syncNavigation); this.on(itemSelectedEvent, this.focus); this.subscribe(loadItemsEvent, syncUI); this.subscribe(pageChangeEvent, this._syncPagerUI); this.subscribe(renderEvent, syncNavigation); this.subscribe(renderEvent, this._syncPagerUI); this.on("selectedItemChange", function (ev) { setItemSelection.call(this, ev.newValue, ev.prevValue); this._updateTabIndex(this.getElementForItem(ev.newValue)); this.fireEvent(itemSelectedEvent, ev.newValue); }); this.on("firstVisibleChange", function (ev) { if (!this.get("selectOnScroll")) { this._updateTabIndex(this.getElementForItem(ev.newValue)); } }); // Handle item selection on mouse click this.on("click", this._itemClickHandler); // Handle page navigation this.on("click", this._pagerClickHandler); // Restore the focus on the navigation buttons Event.onFocus(this.get("element"), function (ev, obj) { obj._updateNavButtons(Event.getTarget(ev), true); }, this); Event.onBlur(this.get("element"), function (ev, obj) { obj._updateNavButtons(Event.getTarget(ev), false); }, this); }, /** * Return the ITEM_TAG_NAME at index or null if the index is not found. * * @method getElementForItem * @param index {Number} The index of the item to be returned * @return {Element} Return the item at index or null if not found * @public */ getElementForItem: function (index) { if (index < 0 || index >= this.get("numItems")) { return null; } // TODO: may be cache the item if (this._itemsTable.numItems > index) { if (!JS.isUndefined(this._itemsTable.items[index])) { return Dom.get(this._itemsTable.items[index].id); } } return null; }, /** * Return the ITEM_TAG_NAME for all items in the Carousel. * * @method getElementForItems * @return {Array} Return all the items * @public */ getElementForItems: function () { var els = [], i; for (i = 0; i < this._itemsTable.numItems; i++) { els.push(this.getElementForItem(i)); } return els; }, /** * Return the item at index or null if the index is not found.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?