国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

通過javascript實現段落的收縮與展開

2024-05-06 16:52:48
字體:
來源:轉載
供稿:網友

主要是使用-webkit-line-clamp這個屬性進行限制顯示行數,通過計算文字在標簽內的顯示高度來計算當前文字行數,再與需要限制的行數進行對比,來確定是否顯示

   代碼如下:

<!--Created by lmj on 2017/8/10.--><!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>段落的收起與展開</title><script src="js/jquery.js"></script><style type="text/css">.info-shrink-text {display: -webkit-box;overflow: hidden;text-overflow: ellipsis;-webkit-box-orient: vertical;}#info-manager-content {text-indent: 2em;font-size: 12px;color: #404040;font-family: 微軟雅黑;}.more-text {display: -webkit-box;width: 100%;-webkit-box-sizing: border-box;-webkit-box-pack: end;padding-right: 10px;color: #00a5e0;font-size: 14px;}</style></head><body><div class="ui-tab"><P id="info-manager-content" class="info-shrink-text">測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落的展開與收起.測試段落</P><div class="more-text">查看更多</div></div><script type="text/javascript">var isHide = true;var textContainer;function initView() {textContainer = $("#info-manager-content");var single=document.createElement("div");// 設置文字樣式single.style.cssText = "padding:0;visibility:hidden;font-familly:微軟雅黑;font-size:12px";single.innerHTML = "單";document.body.appendChild(single);//獲取該樣式下的單個文字的高度var singleHeight = single.offsetHeight;document.body.removeChild(single);//獲取整個段落的高度var paragraphHeight = textContainer.innerHeight();//設置你要限制的高度var limitHeight = 50;//當前文本行數var currentLine = (paragraphHeight/singleHeight).toFixed(0);//轉化為行數var lineCount = (limitHeight / singleHeight).toFixed(0);// alert(singleHeight+"----"+paragraphHeight+"---"+lineCount+"---"+currentLine);// 修改段落限制行數textContainer.attr("style", "-webkit-line-clamp:" + lineCount);// 設置按鈕的顯示或隱藏if (currentLine >= lineCount) {$(".more-text").show();isHide = true;} else {$(".more-text").hide();}}initView();window.onresize = function () {initView();};//添加點擊事件$(".more-text").on("click", function () {if (isHide) {textContainer.removeClass("info-shrink-text");$(this).text("收起");isHide = false;} else {textContainer.addClass("info-shrink-text");$(this).text("查看更多");isHide = true;}});</script></body></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤阳县| 科技| 休宁县| 江西省| 库车县| 新泰市| 柘荣县| 芦山县| 维西| 林口县| 武威市| 荔浦县| 榆林市| 木里| 美姑县| 大姚县| 翁源县| 郓城县| 肥乡县| 普定县| 莆田市| 库尔勒市| 揭西县| 芦溪县| 新建县| 英山县| 陇西县| 花垣县| 新营市| 丰县| 沅陵县| 石阡县| 荆门市| 东台市| 花莲市| 沙洋县| 阳山县| 南澳县| 马山县| 青神县| 马山县|