jQuery實現(xiàn)文本展開收縮特效
在網(wǎng)頁上只有一個小區(qū)域,但是說明性的文字又很長,下面這段腳本實現(xiàn)的是長文字的部分顯示。
當(dāng)用戶點擊展開時,文字展開,收縮時文字收縮。
本來用jQuery自帶的toggle()就可以寫,但是我做的時候 toggle一直不work,所以就用了click + 標(biāo)志位來做的
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
var cur_status = "less";
$.extend({
show_more_init:function(){
//alert("show_more_init!");
var charNumbers=$(".content").html().length;//總字數(shù)
var limit=100;//顯示字數(shù)
if(charNumbers>limit)
{
var orgText=$(".content").html();//原始文本
var orgHeight=$(".content").height();//原始高度
var showText=orgText.substring(0,limit);//最終顯示的文本
$(".content").html(showText);
var contentHeight=$(".content").height();//截取內(nèi)容后的高度
$(".switch").click(
function() {
if(cur_status == "less"){
$(".content").height(contentHeight).html(orgText).animate({ height:orgHeight}, { duration: "slow" });
$(this).html("收縮");
cur_status = "more";
}else{
$(".content").height(orgHeight).html(showText).animate({ height:contentHeight}, { duration: "fast" });
$(this).html("展開");
cur_status = "less";
}
}
);
}
else
{
$(".switch").hide();
}
}
});
$(document).ready(function(){
$.show_more_init();
});
</script>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style>
#limittext{
width:640px;
height:auto;
position:relative;
background-color:#ccc;
color:black;
}
.switch{
font-size:12px;
text-align:center;
cursor:pointer;
font-family:Verdana;
font-weight:800;
position:absolute;
bottom:0;
width:100%;
/*background:url(more-bg.png) repeat-x bottom;*/
height:40px;
line-height:80px;
}
</style>
</head>
<body>
<div id="limittext" >
<div class="content" style="padding-bottom:15px;">
這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字,這是很長的一段文字
</div>
<div class="switch">展開</div>
</div>
</body>
</html>
方法二:
<!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>
<title>jQuery實現(xiàn)DIV層的收縮展開效果</title>
<script type="text/javascript" src="/images/jquery.js"></script>
<style>
/* 收縮展開效果 */
.text{line-height:22px;padding:0 6px;color:#666;}
.box h1{padding-left:10px;height:22px;line-height:22px;background:#f1f1f1;font-weight:bold;}
.box{position:relative;border:1px solid #e7e7e7;}
</style>
</head>
<body>
<script type="text/javascript">
// 收縮展開效果
$(document).ready(function(){
$(".box h1").toggle(function(){
$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow");
},function(){
$(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow");
});
});
</script>
<!-- 收縮展開效果 -->
<div class="box">
<h1>收縮展開效果</h1>
<div class="text">
1<br />
2<br />
3<br />
4<br />
5<br />
</div>
</div>
<br />
<div class="box">
<h1>收縮展開效果</h1>
<div class="text">
1<br />
2<br />
</div>
</div>
<br>
<font color=red>第一次運行請刷新一下頁面。</font>
</body>
</html>
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- jQuery實現(xiàn)DIV響應(yīng)鼠標(biāo)滑過由下向上展開效果示例【測試可用】
- jquery控制頁面的展開和隱藏實現(xiàn)方法(推薦)
- 基于jQuery實現(xiàn)下拉收縮(展開與折疊)特效
- jQuery實現(xiàn)DIV層收縮展開的方法
- jquery實現(xiàn)可點擊伸縮與展開的菜單效果代碼
- jquery實現(xiàn)點擊查看更多內(nèi)容控制段落文字展開折疊效果
- jQuery實現(xiàn)表格展開與折疊的方法
- jquery實現(xiàn)先淡出再折疊收起的動畫效果
- jQuery使用slideUp方法實現(xiàn)控制元素緩慢收起
- jQuery實現(xiàn)的頁面詳情展開收起功能示例
相關(guān)文章
jQuery 獲取頁面li數(shù)組并刪除不在數(shù)組中的key
這篇文章主要介紹了jQuery 獲取頁面li數(shù)組并刪除不在數(shù)組中的key 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
IE10中flexigrid無法顯示數(shù)據(jù)的解決方法
這篇文章主要介紹了IE10中flexigrid無法顯示數(shù)據(jù)的解決方法的相關(guān)資料,需要的朋友可以參考下2015-07-07
基于jquery的用dl模擬實現(xiàn)可自定義樣式的SELECT下拉列表(已封裝)
通過dl模擬實現(xiàn)SELECT下拉列表. 其實這是項目中要常用到的一個效果, 于是, 在之前寫的基礎(chǔ)上封裝成了一個插件. 可自定義樣式, 可防止用戶本意劃過時觸發(fā)事件.2010-11-11
WEB前端開發(fā)都應(yīng)知道的jquery小技巧及jquery三個簡寫
一個簡單技巧的集合,幫你提升 jQuery 技能,下面腳本之家小編給大家收集整理了web前端開發(fā)都應(yīng)知道的jquery小技巧,對jquery小技巧感興趣的朋友一起學(xué)習(xí)吧2015-11-11
jquery簡單實現(xiàn)網(wǎng)頁層的展開與收縮效果
這篇文章主要介紹了jquery簡單實現(xiàn)網(wǎng)頁層的展開與收縮效果的方法,涉及jquery中toggle結(jié)合animate方法操作頁面元素屬性的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-08-08
基于jquery實現(xiàn)的鼠標(biāo)懸停提示案例
本文主要介紹了基于jquery實現(xiàn)的鼠標(biāo)懸停提示的詳細案例。代碼全面,功能實用,需要的朋友可以參考借鑒2016-12-12

