jquery實(shí)現(xiàn)隱藏與顯示動(dòng)畫(huà)效果/輸入框字符動(dòng)態(tài)遞減/導(dǎo)航按鈕切換
路是一步一步走出來(lái)的,知識(shí)是一點(diǎn)一滴積累的,記錄是筆財(cái)富,來(lái)吧,一起學(xué)著總結(jié)做筆記。
這幾天在寫(xiě)后臺(tái)文章的一些頁(yè)面,為了能得到更好的交互性,需要做一些效果,js無(wú)疑使不二之選,但由于瀏覽器的兼容性一直差強(qiáng)人意,所以選用jquery框架,通過(guò)css樣式、dom節(jié)點(diǎn)以及自身所帶函數(shù)就可以實(shí)現(xiàn)比較好的用戶(hù)體驗(yàn),此案例有三個(gè)功能點(diǎn)。分別為:
1.利用jquery自身的toggle()函數(shù)實(shí)現(xiàn)層的隱藏與顯示動(dòng)畫(huà);
2.仿新浪、騰訊微博輸入框字符動(dòng)態(tài)遞減效果(可作為單獨(dú)的js,引入即可通用);
3.實(shí)現(xiàn)幾個(gè)導(dǎo)航按鈕切換不同的內(nèi)容,類(lèi)似tab;
以下為所有代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用jquery實(shí)現(xiàn)兩個(gè)table的顯示與隱藏</title>
<script type="text/javascript" src="jquery-1.2.6.min.js" language="javascript"> </script>
<style>
/*整體table樣式*/
.mainbox {margin:5px 10px;overflow:hidden;zoom:1;_margin:5px;}
.mainnav_title {line-height:40px;height:40px;border-bottom:1px solid #eee;color:#ddd;}
.mainnav_title a {color:#004499;margin:0 5px;padding:4px 7px;background:#EFEFEF;}
.mainnav_title a:hover ,.mainnav_title a.on{background:#498CD0;color:#FFF;}
.table_form td{padding-left:12px}
.table_form th span{color:#FF0000}
.table_form th{font-weight:normal; text-align:right;padding-right:10px; color:#777}
.table_form td label{ vertical-align:middle}
.table_form td , .table_form th{padding:8px 0 5px 8px;line-height:22px;}
.table_form tbody td,.table_form tbody th{border-bottom:1px solid #eee; }
.colorpanel tbody td,.colorpanel tbody th{ padding:0;border-bottom: none;}
/*控制文章字?jǐn)?shù)輸入樣式*/
.textAfter{font-weight: 700;font-size: 22px;font-style: italic;color:#FF0000;font-family: Constantia, Georgia;}
.textCount{font-weight: 700;font-size: 22px;font-style: italic;font-family: Constantia, Georgia;}
/*文章列表頁(yè)面樣式*/
.article_search{border:1px solid #FFCC33; background-color:#FFFFCC;height:46px;margin:10px 0px 10px 0px;line-height:46px;padding:0px 15px 0px 15px;}
.advsetup{background-color:red; height:57px;line-height:57px;}
.search_table a:hover ,.search_table a.on{background:#498CD0;color:#FFF;}
.search_table a{margin:5px;padding:5px;height:15px;line-height:15px;}
.search a{color:#004499;margin:0 5px;padding:4px 7px;background:#EFEFEF;}
</style>
<script>
/*控制文章字?jǐn)?shù)輸入函數(shù)*/
$(function(){
$("td span").addClass('textCount');//頁(yè)面加載時(shí)為所有span標(biāo)簽添加新浪字體樣式
})
/*
words_deal函數(shù)是一個(gè)可以通用的關(guān)于仿新浪字符輸入的函數(shù),用在網(wǎng)站的文章編輯上可以提高用戶(hù)的交互性
dom:當(dāng)前要操作的對(duì)象
num:限制字符數(shù)量
id:通過(guò)傳入id值動(dòng)態(tài)添加需要操作的span
*/
function words_deal(dom,num,id)
{
var curLength=$(dom).val().length; //獲取文本框中輸入的文字總數(shù)量
if(curLength>num)//判斷是否大于限制字符數(shù)量
{ //如果大于限制級(jí)字符數(shù)量,獲得從0到該限制數(shù)量的所有字符串
var totalNum=$(dom).val().substr(0,num);
$(dom).val(totalNum); //將這些字符重新載入文本框,并彈框提示
alert("超過(guò)字?jǐn)?shù)限制,多出的字將被截?cái)啵? );
}
else
{
if(curLength>num-10)
{//如果輸入字符為倒數(shù)10個(gè)字符時(shí)改變樣式將字體變紅
$('.textCount_'+id).addClass("textAfter");
}
else
{//否則移除樣式
$('.textCount_'+id).removeClass("textAfter");
}
$(".textCount_"+id).text(num-$(dom).val().length); //如小于限制級(jí)字符數(shù)量,進(jìn)行累加計(jì)數(shù)顯示
}
}
//文章列表菜單欄效果控制函數(shù)
function fun_search(dom,id){
//控制搜索層顯示或隱藏
if(id!=1){
$(".article_search").toggle("fast",function(){
});
}
//控制切換樣式
var className = $(dom).attr("class");
if(className != 'on'){
$('.search_table a').removeClass('on');
$(dom).addClass('on');
}
}
</script>
</head>
<body>
<!--包含層start-->
<div class="mainbox">
<!--導(dǎo)航欄strat-->
<div id="nav" class="mainnav_title">
<ul>
<a href="javascript:;" onclick="toOpen(this,'1');" class="on">添加文章</a>
<a href="javascript:;" onclick="toOpen(this,'2');">高級(jí)設(shè)置</a>
<a href="javascript:;" onclick="fun_search(this,2);">搜索</a>
</ul>
</div>
<!--導(dǎo)航欄end-->
<!--搜索層start-->
<div class="article_search" style="display:none;">
<form id="searchForm" action="admin/user/0" method="post">
添加時(shí)間:
<input type="text" class="input-text" name="dateMin" id="dateMin" readonly="readonly"/> -
<input type="text" class="input-text" name="dateMax" id="dateMax" readonly="readonly"/>
<select name="channel_id2" id="channel_id2">
<option value="" >--- 全部欄目 ---</option>
<c:forEach items="${list}" var="list">
<option value="${list.id}">--- ${list.name} ---</option>
</c:forEach>
</select>
<select name="choose">
<option value="" >--- 查詢(xún)條件 ---</option>
<option value="" >--- ID ---</option>
<option value="" >--- 標(biāo)題 ---</option>
<option value="" >--- 簡(jiǎn)介 ---</option>
<option value="" >--- 發(fā)布人 ---</option>
</select>
<input type="text" class="input-text" name="txtSearch" size="30"></input>
<input type="submit" class="button" value="搜索"></input>
</form>
</div>
<!--搜索層end-->
<!--第一個(gè)div層start-->
<table id="table_1" cellpadding=0 cellspacing=0 width="100%" class="table_form" >
<tr>
<th width="140"><span>*</span> 欄目</th>
<td>
<select name="channel" id="channel">
<option value="" >--- 全部欄目 ---</option>
<c:forEach items="${list}" var="list">
<option value=""></option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<th width="140"><span>*</span> 標(biāo)題</th>
<td>
<input name="title" id="title" class="input-text"" type="text" size="90" onkeyup="words_deal(this,40,1);"></input>剩余<span class="textCount_1">40</span>個(gè)字<br />
</td>
</tr>
<tr>
<th width="140">縮略圖:</th>
<td>
<table>
<td>
<input name="txtSmallPic" type="text" id="text" class="input-text" size="45"/>
<input name="btnUpdown" type="submit" value="本地上傳" class="button"/>
<input name="btnChoose" type="submit" value="站內(nèi)選擇" class="button"/>
<input name="btnCut" type="submit" value="裁切" class="button"/>
</td>
<td><img src="thumbnail.ico" style="width:128px; height:128px;" /></td>
</table>
</td>
</tr>
<tr>
<th width="140">自定義屬性 </th>
<td>
<input id="chkDiyAtrr" type="checkbox" /> 首頁(yè)頭條推薦
<input id="chkDiyAtrr" type="checkbox" /> 首頁(yè)焦點(diǎn)圖推薦
<input id="chkDiyAtrr" type="checkbox" /> 視頻首頁(yè)每日熱點(diǎn)
<input id="chkDiyAtrr" type="checkbox" /> 視頻首頁(yè)頭條推薦
<input id="chkDiyAtrr" type="checkbox" /> 視頻首頁(yè)焦點(diǎn)圖
<input id="chkDiyAtrr" type="checkbox" /> 首頁(yè)圖片推薦<br></br>
<input id="chkDiyAtrr" type="checkbox" /> 欄目首頁(yè)推薦
<input id="chkDiyAtrr" type="checkbox" /> 視頻欄目精彩推薦
<input id="chkDiyAtrr" type="checkbox" /> 網(wǎng)站頂部推薦
</td>
</tr>
<tr>
<th width="140">TAG標(biāo)簽</th>
<td>
<input id="txtTag" class="input-text" type="text" size=""/> (','號(hào)分開(kāi),單個(gè)標(biāo)簽小于12字節(jié))
</td>
</tr>
</table>
<!--第一個(gè)div層end-->
<!--第二個(gè)div層start-->
<table id="table_2" style="display:none;" cellpadding=0 cellspacing=0 width="100%" class="table_form">
<tr>
<th width="140">附加選項(xiàng)</th>
<td>
<input id="chkDiyAtrr" type="checkbox" /> 提取第一個(gè)圖片為縮略圖
<input id="chkWatermark" type="checkbox" /> 圖片是否加水印
</td>
</tr>
<tr><th width="140">分頁(yè)選項(xiàng)</th>
<td>
<input id="rdoManual" type="radio" class="input-text" /> 手動(dòng) (分頁(yè)符為: #p#分頁(yè)標(biāo)題#e# )
<input id="rdoAutomatic" type="radio" class="input-text" /> 自動(dòng) 大小:
<input id="txtPage" type="text" style=" width:20px;" />K
</td>
</tr>
<tr>
<th width="140"> 評(píng)論選項(xiàng)</th>
<td>
<input id="rdoAllow" type="radio" class="input-text"/> 允許評(píng)論
<input id="rdoBan" type="radio" class="input-text" /> 禁止評(píng)論
</td>
</tr>
<tr>
<th width="140"><span>*</span> 標(biāo)題</th>
<td>
<input name="title" class="input-text"" type="text" size="90" id="TextArea" onkeyup="words_deal(this,20,2);"/>
剩余<span class="textCount_2">20</span>個(gè)字<br />
</td>
</tr>
<tr>
<th width="140"> 文章排序 </th>
<td colspan="2">
<select id="u108" class="u108">
<option selected="" value="默認(rèn)排序">默認(rèn)排序</option>
<option value="置頂一周">置頂一周</option>
<option value="置頂一月">置頂一月</option>
<option value="置頂一年">置頂一年</option>
</select>
</td>
</tr>
</table>
<!--第二個(gè)div層end-->
</div>
<!--包含層start-->
</body>
<script>
//切換界面
function toOpen(dom,id){
var className = $(dom).attr("class");
if(className != 'on'){
$('table[id^=table_]').hide();
$('.mainnav_title ul a').removeClass('on');
$('#table_'+id).show();
$(dom).addClass('on');
}
}
//文章列表菜單欄效果控制函數(shù)
function fun_search(dom,id){
//控制搜索層顯示或隱藏
if(id!=1){
$(".article_search").toggle("fast",function(){
});
}
//控制切換樣式
var className = $(dom).attr("class");
if(className != 'on'){
$('.search_table a').removeClass('on');
$(dom).addClass('on');
}
}
</script>
</html>
以下是運(yùn)行的效果圖:

ps:代碼規(guī)范很重要,養(yǎng)成加注釋的好習(xí)慣。
- jquery實(shí)現(xiàn)點(diǎn)擊左右按鈕切換圖片
- 使用jQuery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊左右按鈕滑動(dòng)切換
- 基于jquery實(shí)現(xiàn)左右按鈕點(diǎn)擊的圖片切換效果
- jquery帶有索引按鈕且自動(dòng)輪播切換特效代碼分享
- jQuery幻燈片特效代碼分享 鼠標(biāo)滑過(guò)按鈕時(shí)切換(2)
- Jquery幻燈片特效代碼分享?鼠標(biāo)點(diǎn)擊按鈕時(shí)切換(1)
- 基于jquery插件制作左右按鈕與標(biāo)題文字圖片切換效果
- jquery仿QQ商城帶左右按鈕控制焦點(diǎn)圖片切換滾動(dòng)效果
- jQuery實(shí)現(xiàn)簡(jiǎn)單的按鈕顏色變化
相關(guān)文章
jQuery的animate函數(shù)學(xué)習(xí)記錄
jQuery.animate的每種動(dòng)畫(huà)過(guò)渡效果都是通過(guò)easing函數(shù)實(shí)現(xiàn)的,下面是自己研究之后對(duì)其的基本認(rèn)識(shí)2014-08-08純JAVASCRIPT圖表動(dòng)畫(huà)插件Highcharts Examples
官方提供免費(fèi)的下載包以及詳細(xì)的文檔說(shuō)明,非常的值得收藏。2011-04-04asp.net中oracle 存儲(chǔ)過(guò)程(圖文)
存儲(chǔ)過(guò)程是在大型數(shù)據(jù)庫(kù)系統(tǒng)中,一組為了完成特定功能的sql語(yǔ)句集,經(jīng)過(guò)編譯存儲(chǔ)在數(shù)據(jù)庫(kù)中,用戶(hù)通過(guò)指定存儲(chǔ)過(guò)程的名字并給出參數(shù)(如果該存儲(chǔ)過(guò)程帶有參數(shù))來(lái)執(zhí)行它,下面小編給大家介紹asp.net中oracle存儲(chǔ)過(guò)程,需要的朋友可以參考下2015-08-08一款Jquery 分頁(yè)插件的改造方法(服務(wù)器端分頁(yè))
分頁(yè)幾乎是每個(gè)外部程序必不可少的東西,在webform時(shí)代很多人都用過(guò)AspNetPager這個(gè)用戶(hù)控件吧,用的人之多其實(shí)就在于它的優(yōu)點(diǎn)2011-07-07通過(guò)jquery還原含有rowspan、colspan的table的實(shí)現(xiàn)方法
通過(guò)jquery還原含有rowspan、colspan的table的實(shí)現(xiàn)方法,學(xué)習(xí)jquery的朋友可以參考下2012-02-02jquery實(shí)現(xiàn)下拉菜單的手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)下拉菜單的手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07jQuery.get、jQuery.getJSON、jQuery.post無(wú)法返回JSON問(wèn)題的解決方法
在WEB項(xiàng)目中,經(jīng)常會(huì)使用到j(luò)Query進(jìn)行AJAX請(qǐng)求,但是自從使用了.net 3.5以后,以往寫(xiě)的請(qǐng)求語(yǔ)句就有些小問(wèn)題了,就是返回的始終是xml,而并不是JSON2011-07-07