征集網銳志網絡淘途找素材上91
創意在線首頁
平面設計交互設計CG動漫工業設計建筑環境數碼影像創意搜
 首頁大賽征集資訊教程畫廊訪談作品集創企同盟設計招聘人才專區競賽臺專題論吧 找素材看酷站設計網址庫
交互設計首頁
當前位置:首頁 > 交互設計 > 觀點 > 正文
CSS,JavaScript和MooTools制作斑馬紋表格
作者: 來源:Rainbow 發表時間:2010-3-25 16:50:17 瀏覽:

簡單的斑馬紋表格,如果頁面上有大量的表格數據時,隔行變色的斑馬紋會幫助我們快速閱讀,有利于用戶體驗。我們今天不討論在動態語言中的方法,只討論CSS,JavaScript,MooTools 是如何實現的,并有三種可行性方案。

我們的表格

The Html Code:

 

<table id="playlist" cellspacing="0">
	<tbody>
		<tr>
			<td>1</td>
			<td>Lost In The Plot</td>
			<td>The Dears</td>
		</tr>
		<tr>
			<td>2</td>
			<td>Poison</td>
			<td>The Constantines</td>
		</tr>
		<tr>
			<td>3</td>
			<td>Plea From A Cat Named Virtute</td>
			<td>The Weakerthans</td>
		</tr>
		<tr>
			<td>4</td>
			<td>Melissa Louise</td>
			<td>Chixdiggit!</td>
		</tr>
 
		<tr>
			<td>5</td>
			<td>Living Room</td>
			<td>Tegan And Sara</td>
 
		</tr>
		<tr>
			<td>6</td>
			<td>Speed</td>
			<td>Bran Van 3000</td>
		</tr>
		<tr>
			<td>7</td>
			<td>Fast Money Blessing</td>
			<td>King Cobb Steelie</td>
		</tr>
 
		<tr>
			<td>8</td>
			<td>Sore</td>
			<td>Buck 65</td>
		</tr>
		<tr>
			<td>9</td>
			<td>Love Travel</td>
			<td>Danko Jones</td>
		</tr>
		<tr>
			<td>10</td>
			<td>You Never Let Me Down</td>
			<td>Furnaceface</td>
		</tr>	
	</tbody>
 
</table>

 

我們上面所看到的表格,就是我們要美化的表格,要實現斑馬紋的表格。

斑馬紋的表格

方案一 :

在CSS3中有許多的偽類選擇器,其中的

 

E:nth-child(n){attribute}

 

它可以匹配父元素中的第n個子元素E。

The CSS3 Code

 

/*獲得奇偶數的子元素*/
tr:nth-child(odd)		{ background-color:#eee; }//所有奇數序子元素
tr:nth-child(even)		{ background-color:#fff; }//所有偶數序子元素
/*同上一樣的作用*/
tr:nth-child(2n){background-color:#eee;} //返回偶數序的子元素
tr:nth-child(2n+1){ background-color:#fff;} //返回奇數序的子元素

 

方案二 :

The JavaScript Code

 

// this function is need to work around
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }  
 
function stripe(id) {
 
    // the flag we’ll use to keep track of
    // whether the current row is odd or even
    var even = false;
 
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
 
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
 
    // by definition, tables can have more than one tbody
    // element, so we’ll have to get the list of child
    // &lt;tbody&gt;s
    var tbodies = table.getElementsByTagName("tbody");
 
    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
 
     // find all the &lt;tr&gt; elements...
      var trs = tbodies[h].getElementsByTagName("tr");
 
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {
 
    // avoid rows that have a class attribute
        // or backgroundColor style
    if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
 
         // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
 
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
 
            var mytd = tds[j];
 
            // avoid cells that have a class attribute
            // or backgroundColor style
        if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
 
      mytd.style.backgroundColor = even ? evenColor : oddColor;
 
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }
window.onload=function(){stripe(’playlist’, ’#fff’, ’#eee’);}

 

在以前MooTools1.1的老版本中是不支持CSS3選擇器的,那又如何實現那。

方案三 :

The CSS Code:

 

.odd{
	background:#fff;
	color: #666;
}
.even{
	background-color: #3d80df;
	color: #FFF;	
}

 

The MooTools JavaScript:

 

window.addEvent(’domready’, function() {
	var count = 0;
	$(’table.shade-table tr’).each(function(el) {
		el.addClass(count++ % 2 == 0 ? ’odd’ : ’even’);
	});
});

 

如果你已經使用了MooTools1.2+的版本,我們就可以用MooTools Selectors的偽類選擇器,它的語法是類似于CSS3的偽類選擇器的。

The MooTools JavaScript:

 

$(’table#playlist tr:nth-child(odd)’).addClass(’odd’);
 
$(’table#playlist tr:nth-child(even)’).addClass(’even’);
/*
$(’table#playlist tr:nth-child(2n+1)’).addClass(’odd’);
 
$(’table#playlist tr:nth-child(2n)’).addClass(’even’);*/

 

在鼠標經過時高亮表格行列

The CSS Code:

 

.over{
	background-color:#F00;
	color:#FFF;
}

 

The MooTools JavaScript:

 

$$("table#playlist tr").addEvent(’mouseover’,function() {this.addClass("over");}).addEvent(’mouseout’,function() {this.removeClass("over");});


在線編輯:xudf
本文鏈接:


推薦閱讀:
·2021第十五屆“創意中國”設計大獎 征稿章程
·2021第七屆“學院派獎”全國藝術與設計大賽征集公告
·青春唯美插畫培訓網絡遠程班正在招生
·第八屆中國高等院校設計作品大賽啟動
·第十二屆中國高校美術作品學年展獲獎名單公布
·2021第七屆“學院派獎”全國藝術與設計大賽征集公告
·第六屆“包豪斯獎”國際設計大賽獲獎名單揭曉
免責聲明:本站刊載此文不代表同意其說法或描述,僅為提供更多信息。對本文有異議,請聯絡本站!
轉載要求:文章作者及來源信息必需保留。轉載之圖片、文件請不要盜鏈本站地址,且不準打上各自站點的水!

近期推薦報道 返回首頁更多精彩等著你!

Illustrator| 用AI創建精細矢量插圖徽章Photoshop| PS經典合成張開血盆大嘴櫻桃
Photoshop| Photoshop自制個性的黑色翅膀攝影佳作| 德國Thomas Wuhrer長腿女郎性
Photoshop| Photoshop制作超酷的邊緣發光的熒CorelDraw| CorelDRAW制作編織效果詳細教程
Illustrator| illustrator制作潮流海報字體攝影佳作| 驚艷的Francesco Marconi
Photoshop| 利用畫筆及圖層樣式制作方格子藝術紋理背景Photoshop| Photoshop制作簡單時尚的彩色放射
關于本站 | 聯系方式 | 商業服務 | 合作伙伴 | 站點地圖 | 免責聲明 | 版權聲明 | 在線投稿  
 
97久久精品无码一区二区,精品久久久久久无码专区不卡,交换玩弄两个美妇教师视频,让男人爽到不行的床技