📄 ui.tabs.js
字号:
/* * jQuery UI Tabs * * Copyright (c) 2007, 2008 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * * http://docs.jquery.com/UI/Tabs * * Depends: * ui.core.js */(function($) {$.widget("ui.tabs", { init: function() { this.options.event += '.tabs'; // namespace event // create tabs this.tabify(true); }, setData: function(key, value) { if ((/^selected/).test(key)) this.select(value); else { this.options[key] = value; this.tabify(); } }, length: function() { return this.$tabs.length; }, tabId: function(a) { return a.title && a.title.replace(/\s/g, '_').replace(/[^A-Za-z0-9\-_:\.]/g, '') || this.options.idPrefix + $.data(a); }, ui: function(tab, panel) { return { options: this.options, tab: tab, panel: panel }; }, tabify: function(init) { this.$lis = $('li:has(a[href])', this.element); this.$tabs = this.$lis.map(function() { return $('a', this)[0]; }); this.$panels = $([]); var self = this, o = this.options; this.$tabs.each(function(i, a) { // inline tab if (a.hash && a.hash.replace('#', '')) // Safari 2 reports '#' for an empty hash self.$panels = self.$panels.add(a.hash); // remote tab else if ($(a).attr('href') != '#') { // prevent loading the page itself if href is just "#" $.data(a, 'href.tabs', a.href); // required for restore on destroy $.data(a, 'load.tabs', a.href); // mutable var id = self.tabId(a); a.href = '#' + id; var $panel = $('#' + id); if (!$panel.length) { $panel = $(o.panelTemplate).attr('id', id).addClass(o.panelClass) .insertAfter( self.$panels[i - 1] || self.element ); $panel.data('destroy.tabs', true); } self.$panels = self.$panels.add( $panel ); } // invalid tab href else o.disabled.push(i + 1); }); if (init) { // attach necessary classes for styling if not present this.element.addClass(o.navClass); this.$panels.each(function() { var $this = $(this); $this.addClass(o.panelClass); }); // Selected tab // use "selected" option or try to retrieve: // 1. from fragment identifier in url // 2. from cookie // 3. from selected class attribute on <li> if (o.selected === undefined) { if (location.hash) { this.$tabs.each(function(i, a) { if (a.hash == location.hash) { o.selected = i; // prevent page scroll to fragment if ($.browser.msie || $.browser.opera) { // && !o.remote var $toShow = $(location.hash), toShowId = $toShow.attr('id'); $toShow.attr('id', ''); setTimeout(function() { $toShow.attr('id', toShowId); // restore id }, 500); } scrollTo(0, 0); return false; // break } }); } else if (o.cookie) { var index = parseInt($.cookie('ui-tabs' + $.data(self.element)),10); if (index && self.$tabs[index]) o.selected = index; } else if (self.$lis.filter('.' + o.selectedClass).length) o.selected = self.$lis.index( self.$lis.filter('.' + o.selectedClass)[0] ); } o.selected = o.selected === null || o.selected !== undefined ? o.selected : 0; // first tab selected by default // Take disabling tabs via class attribute from HTML // into account and update option properly. // A selected tab cannot become disabled. o.disabled = $.unique(o.disabled.concat( $.map(this.$lis.filter('.' + o.disabledClass), function(n, i) { return self.$lis.index(n); } ) )).sort(); if ($.inArray(o.selected, o.disabled) != -1) o.disabled.splice($.inArray(o.selected, o.disabled), 1); // highlight selected tab this.$panels.addClass(o.hideClass); this.$lis.removeClass(o.selectedClass); if (o.selected !== null) { this.$panels.eq(o.selected).show().removeClass(o.hideClass); // use show and remove class to show in any case no matter how it has been hidden before this.$lis.eq(o.selected).addClass(o.selectedClass); // seems to be expected behavior that the show callback is fired var onShow = function() { $(self.element).triggerHandler('tabsshow', [null, self.ui(self.$tabs[o.selected], self.$panels[o.selected])], o.show); }; // load if remote tab if ($.data(this.$tabs[o.selected], 'load.tabs')) this.load(o.selected, onShow); // just trigger show event else onShow(); } // clean up to avoid memory leaks in certain versions of IE 6 $(window).bind('unload', function() { self.$tabs.unbind('.tabs'); self.$lis = self.$tabs = self.$panels = null; }); } // disable tabs for (var i = 0, li; li = this.$lis[i]; i++) $(li)[$.inArray(i, o.disabled) != -1 && !$(li).hasClass(o.selectedClass) ? 'addClass' : 'removeClass'](o.disabledClass); // reset cache if switching from cached to not cached if (o.cache === false) this.$tabs.removeData('cache.tabs'); // set up animations var hideFx, showFx, baseFx = { 'min-width': 0, duration: 1 }, baseDuration = 'normal'; if (o.fx && o.fx.constructor == Array) hideFx = o.fx[0] || baseFx, showFx = o.fx[1] || baseFx; else hideFx = showFx = o.fx || baseFx; // reset some styles to maintain print style sheets etc. var resetCSS = { display: '', overflow: '', height: '' }; if (!$.browser.msie) // not in IE to prevent ClearType font issue resetCSS.opacity = ''; // Hide a tab, animation prevents browser scrolling to fragment, // $show is optional. function hideTab(clicked, $hide, $show) { $hide.animate(hideFx, hideFx.duration || baseDuration, function() { // $hide.addClass(o.hideClass).css(resetCSS); // maintain flexible height and accessibility in print etc. if ($.browser.msie && hideFx.opacity) $hide[0].style.filter = ''; if ($show) showTab(clicked, $show, $hide); }); } // Show a tab, animation prevents browser scrolling to fragment, // $hide is optional. function showTab(clicked, $show, $hide) { if (showFx === baseFx) $show.css('display', 'block'); // prevent occasionally occuring flicker in Firefox cause by gap between showing and hiding the tab panels $show.animate(showFx, showFx.duration || baseDuration, function() { $show.removeClass(o.hideClass).css(resetCSS); // maintain flexible height and accessibility in print etc. if ($.browser.msie && showFx.opacity) $show[0].style.filter = ''; // callback $(self.element).triggerHandler('tabsshow', [null, self.ui(clicked, $show[0])], o.show); }); } // switch a tab function switchTab(clicked, $li, $hide, $show) { /*if (o.bookmarkable && trueClick) { // add to history only if true click occured, not a triggered click $.ajaxHistory.update(clicked.hash); }*/ $li.addClass(o.selectedClass) .siblings().removeClass(o.selectedClass); hideTab(clicked, $hide, $show); } // attach tab event handler, unbind to avoid duplicates from former tabifying... this.$tabs.unbind('.tabs').bind(o.event, function() { //var trueClick = e.clientX; // add to history only if true click occured, not a triggered click var $li = $(this).parents('li:eq(0)'), $hide = self.$panels.filter(':visible'), $show = $(this.hash); // If tab is already selected and not unselectable or tab disabled or // or is already loading or click callback returns false stop here. // Check if click handler returns false last so that it is not executed // for a disabled or loading tab! if (($li.hasClass(o.selectedClass) && !o.unselect) || $li.hasClass(o.disabledClass) || $(this).hasClass(o.loadingClass) || $(self.element).triggerHandler('tabsselect', [null, self.ui(this, $show[0])], o.select) === false ) { this.blur(); return false; } self.options.selected = self.$tabs.index(this); // if tab may be closed if (o.unselect) { if ($li.hasClass(o.selectedClass)) { self.options.selected = null; $li.removeClass(o.selectedClass); self.$panels.stop(); hideTab(this, $hide); this.blur(); return false; } else if (!$hide.length) { self.$panels.stop(); var a = this; self.load(self.$tabs.index(this), function() { $li.addClass(o.selectedClass).addClass(o.unselectClass); showTab(a, $show); }); this.blur(); return false; } } if (o.cookie) $.cookie('ui-tabs' + $.data(self.element), self.options.selected, o.cookie); // stop possibly running animations self.$panels.stop(); // show new tab if ($show.length) { // prevent scrollbar scrolling to 0 and than back in IE7, happens only if bookmarking/history is enabled /*if ($.browser.msie && o.bookmarkable) { var showId = this.hash.replace('#', ''); $show.attr('id', ''); setTimeout(function() { $show.attr('id', showId); // restore id }, 0); }*/ var a = this; self.load(self.$tabs.index(this), $hide.length ? function() { switchTab(a, $li, $hide, $show); } : function() { $li.addClass(o.selectedClass); showTab(a, $show); } ); // Set scrollbar to saved position - need to use timeout with 0 to prevent browser scroll to target of hash /*var scrollX = window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft || 0; var scrollY = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0; setTimeout(function() { scrollTo(scrollX, scrollY); }, 0);*/ } else throw 'jQuery UI Tabs: Mismatching fragment identifier.';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -