⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabpane.html

📁 动态增加表格和横向导航栏
💻 HTML
📖 第 1 页 / 共 3 页
字号:


</div>
<!-- end api tab -->








<!-- begin implementation tab -->
<div class="tab-page" id="implementation-page">
<h2 class="tab">Implementation</h2>

<script type="text/javascript">
tabPane.addTabPage( document.getElementById( "implementation-page" ) );
</script>

<h3>Check for support</h3>

<p>The way to check the browser whether it support a certain feature in the
DOM is to use the method <code>document.implementation.hasFeature</code>.
However since IE5.5 supports all the features that this script needs but it
does not support this way of checking for support we have to add a separate
check for IE55.</p>

<pre>
function hasSupport() {

   if (typeof hasSupport.support != "undefined")
      return hasSupport.support;

   var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );

   hasSupport.support = ( typeof document.implementation != "undefined" &amp;&amp;
         document.implementation.hasFeature( "html", "1.0" ) || ie55 )

   // IE55 has a serious DOM1 bug... Patch it!
   if ( ie55 ) {
      document._getElementsByTagName = document.getElementsByTagName;
      document.getElementsByTagName = function ( sTagName ) {
         if ( sTagName == "*" )
            return document.all;
         else
            return document._getElementsByTagName( sTagName );
      };
   }

   return hasSupport.support;
}
</pre>

<p>As you can see in the code above IE55 has a bug an therefore we also
patch that. Too many people are still using IE55 to just ignore it.</p>

<h3>WebFXTabPane</h3>

<p>The constructor for the tab pane creates the <code>tabRow</code> div
that is used to place all the actual tabs in. It also checks the cookie
state so that the selected tab can be persisted. Besides from this it
sets up some properties needed to keep track of the states. Last but not
least it checks the <code>childNodes</code> of the element and adds
the found tab pages.</p>

<pre>
function WebFXTabPane( el, bUseCookie ) {
   if ( !hasSupport() || el == null ) return;

   this.element = el;
   this.element.tabPane = this;
   this.pages = [];
   this.selectedIndex = null;
   this.useCookie = bUseCookie != null ? bUseCookie : true;

   // add class name tag to class name
   this.element.className = this.classNameTag + " " + this.element.className;

   // add tab row
   this.tabRow = document.createElement( "div" );
   this.tabRow.className = "tab-row";
   el.insertBefore( this.tabRow, el.firstChild );

   var tabIndex = 0;
   if ( this.useCookie ) {
      tabIndex = Number( WebFXTabPane.getCookie( "webfxtab_" + this.element.id ) );
      if ( isNaN( tabIndex ) )
         tabIndex = 0;
   }
   this.selectedIndex = tabIndex;

   // loop through child nodes and add them
   var cs = el.childNodes;
   var n;
   for (var i = 0; i &lt; cs.length; i++) {
      if (cs[i].nodeType == 1 &amp;&amp; cs[i].className == "tab-page") {
         this.addTabPage( cs[i] );
      }
   }
}
</pre>

<p>There are a few methods added to the <code>WebFXTabPane</code> class and one of the
more important ones is the method <code>addTabPage</code>. This method takes the element
that represents the tab page and uses that to create a <code>WebFXTabPage</code>
object that is added to the <code>pages</code> array. Once the tab page has been
added it also checks if this page is the selected one and if it is it shows it.</p>

<pre>
WebFXTabPane.prototype = {

   ...

   addTabPage:   function ( oElement ) {
      if ( !hasSupport() ) return;

      if ( oElement.tabPage == this )   // already added
         return oElement.tabPage;

      var n = this.pages.length;
      var tp = this.pages[n] = new WebFXTabPage( oElement, this, n );
      tp.tabPane = this;

      // move the tab out of the box
      this.tabRow.appendChild( tp.tab );

      if ( n == this.selectedIndex )
         tp.show();
      else
         tp.hide();

      return tp;
   }
};
</pre>

<h3>WebFXTabPage</h3>

<p>This class is used to keep track of the actual tab page. Once created it moves
the tab element to the <code>tabRow</code> of the tab pane. It also adds
an anchor around the text so that the user can use the keyboard to activate the
tabs.</p>

<pre>
function WebFXTabPage( el, tabPane, nIndex ) {
   if ( !hasSupport() || el == null ) return;

   this.element = el;
   this.element.tabPage = this;
   this.index = nIndex;

   var cs = el.childNodes;
   for (var i = 0; i &lt; cs.length; i++) {
      if (cs[i].nodeType == 1 &amp;&amp; cs[i].className == "tab") {
         this.tab = cs[i];
         break;
      }
   }

   // insert a tag around content to support keyboard navigation
   var a = document.createElement( "A" );
   a.href = "javascript:void 0;";
   while ( this.tab.hasChildNodes() )
      a.appendChild( this.tab.firstChild );
   this.tab.appendChild( a );

   // hook up events, using DOM0
   var oThis = this;
   this.tab.onclick = function () { oThis.select(); };
   this.tab.onmouseover = function () { WebFXTabPage.tabOver( oThis ); };
   this.tab.onmouseout = function () { WebFXTabPage.tabOut( oThis ); };
}
</pre>

<h3>Initialization</h3>

<p>The initialization uses the global function <code>setupAllTabs</code> that
goes through all elements and checks their class names and if the class names
match the classes used by the tab pane controls it checks whether this element
belongs to an uninitialized control and in that case it initializes it now.</p>

<pre>
function setupAllTabs() {
   if ( !hasSupport() ) return;

   var all = document.getElementsByTagName( "*" );
   var l = all.length;
   var tabPaneRe = /tab\-pane/;
   var tabPageRe = /tab\-page/;
   var cn, el;
   var parentTabPane;

   for ( var i = 0; i &lt; l; i++ ) {
      el = all[i]
      cn = el.className;

      // no className
      if ( cn == "" ) continue;

      // uninitiated tab pane
      if ( tabPaneRe.test( cn ) &amp;&amp; !el.tabPane )
         new WebFXTabPane( el );

      // unitiated tab page wit a valid tab pane parent
      else if ( tabPageRe.test( cn ) &amp;&amp; !el.tabPage &amp;&amp;
               tabPaneRe.test( el.parentNode.className ) ) {
         el.parentNode.tabPane.addTabPage( el );
      }
   }
}
</pre>

<p>This function can be called manually at any time but the script makes hooks
to the <code>load</code> event for the window. This is done using DOM level 2
events if available. If not we test if it supports the IE5 way of attaching events
and last we fall back on classic way of setting events.</p>

<pre>
// DOM2
if ( typeof window.addEventListener != "undefined" )
   window.addEventListener( "load", setupAllTabs, false );

// IE
else if ( typeof window.attachEvent != "undefined" )
   window.attachEvent( "onload", setupAllTabs );

else {
   if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
         oldOnload( e );
         setupAllTabs();
      };
   }
   else
      window.onload = setupAllTabs;
}
</pre>

</div>
<!-- end implementation tab -->




<!-- begin look and feel tab -->
<div class="tab-page" id="look-page">
<h2 class="tab">Look &amp; Feel</h2>

<script type="text/javascript">
tabPane.addTabPage( document.getElementById( "look-page" ) );
</script>


<h3>The structure</h3>

<p>To be able to change the look and feel one needs to understand the structure
of the tab pane. When the original XHTML source tree is transformed into the
tab pane the class name of the element representing the tab pane is tagged with
the property <code>classNameTag</code>. The default tag is
<code>dynamic-tab-pane-control</code> and therefore all your css rules should
take this into account. If you want different look on different tab panes in
the same document this tag can be changed to make the css rules easier to set
up.</p>

<pre>
&lt;div class="dynamic-tab-pane-control tab-pane" id="tab-pane-1"&gt;
   &lt;div class="tab-row"&gt;
      &lt;h2 class="tab selected"&gt;&lt;a ... &gt;General&lt;/a&gt;&lt;/h2&gt;
      &lt;h2 class="tab hover"&gt;&lt;a ... &gt;Privacy&lt;/a&gt;&lt;/h2&gt;
   &lt;/div&gt;
   &lt;div class="tab-page"&gt;

      This is text of tab 1. This is text of tab 1.
      This is text of tab 1. This is text of tab 1.

   &lt;/div&gt;

   &lt;div class="tab-page"&gt;

      This is text of tab 2. This is text of tab 2.
      This is text of tab 2. This is text of tab 2.

   &lt;/div&gt;
&lt;/div&gt;
</pre>

<p>The selected tab will have the class name <code>tab selected</code> and the
tab that the mouse hovers over will have the class name <code>tab hover</code>. If the selected
tab is hovered it will have the class name <code>tab selected hover</code>. These
rules allow you to differentiate the look of tabs between the different
states.</p>

<h3>The CSS Rules</h3>

<p>Here we will walk through the <a href="css/tab.winclassic.css">Windows Classic
css file</a>. First we set the width and position of the tab pane to prevent a few
rendering bugs in IE6.</p>

<pre>
.dynamic-tab-pane-control.tab-pane {
   position:        relative;
   width:           100%;
}

.dynamic-tab-pane-control .tab-row {
   z-index:         1;
   white-space:     nowrap;
}
</pre>

<p>Then we setup the css for the tab. Notice how the position is set to
relative to allow the top position to be slightly changed and to allow the
z-index property to be changed to position the tabs below the tab pages.</p>

<pre>
.dynamic-tab-pane-control .tab-row .tab {
   font:            Menu;
   cursor:          Default;
   display:         inline;
   margin:          1px -2px 1px 2px;
   float:           left;
   padding:         2px 5px 3px 5px;
   background:      ThreeDFace;
   border:          1px solid;
   border-color:    ThreeDHighlight ThreeDDarkShadow
                    ThreeDDarkShadow ThreeDHighlight;
   border-bottom:   0;
   z-index:         1;
   position:        relative;
   top:             0;
}
</pre>

<p>For the selected tab we set the z-index to 3 to put it above the
tab pages. We also move it a little and change some other properties to make it look
more like the classic window tab control.</p>

<pre>
.dynamic-tab-pane-control .tab-row .tab.selected {
   border-bottom:   0;
   z-index:         3;
   padding:         2px 6px 5px 7px;
   margin:          1px -3px -2px 0px;
   top:             -2px;
}
</pre>

<p>Then we override the text properties on the tabs as well
as for the <code>.hover</code> rule.</p>

<pre>
.dynamic-tab-pane-control .tab-row .tab a {
   font:            Menu;
   color:           WindowText;
   text-decoration: none;
   cursor:          default;
}

.dynamic-tab-pane-control .tab-row .hover a {
   color:           blue;
}
</pre>

<p>Then we set the z-index for the tab pages to 2 so that it will be
shown above tabs but below the selected tab. We also set the borders and
and a few other properties.</p>

<pre>
.dynamic-tab-pane-control .tab-page {
   clear:           both;
   border:          1px solid;
   border-color:    ThreeDHighlight ThreeDDarkShadow
                    ThreeDDarkShadow ThreeDHighlight;
   background:      ThreeDFace;
   z-index:         2;
   position:        relative;
   top:             -2px;
   color:           WindowText;
   font:            MessageBox;
   font:            Message-Box;
   padding:         10px;
}
</pre>


</div>
<!-- end look and feel tab -->


</div>
<!-- end tab pane -->


<p>
<a href="javascript:showArticleTab( 'main' )">Tab Pane</a><br />
<a href="javascript:showArticleTab( 'usage' )">Usage</a><br />
<a href="javascript:showArticleTab( 'api' )">API</a><br />
<a href="javascript:showArticleTab( 'implementation' )">Implementation</a><br />
<a href="javascript:showArticleTab( 'looknfeel' )">Look &amp; Feel</a><br />
<a href="demo.html">Demo</a><br />
<a href="http://webfx.eae.net/download/tabpane102.zip">Download</a>
</p>

<p class="author">Author: Erik Arvidsson</p>

<!-- end webfx-main-body -->
</div>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -