主要是使用-webkit-line-clamp這個屬性進行限制顯示行數(shù),通過計算文字在標(biāo)簽內(nèi)的顯示高度來計算當(dāng)前文字行數(shù),再與需要限制的行數(shù)進行對比,來確定是否顯示
成都創(chuàng)新互聯(lián)公司擁有網(wǎng)站維護技術(shù)和項目管理團隊,建立的售前、實施和售后服務(wù)體系,為客戶提供定制化的成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站維護、服務(wù)器托管解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護服務(wù)覆蓋集團企業(yè)、上市公司、外企網(wǎng)站、商城系統(tǒng)網(wǎng)站開發(fā)、政府網(wǎng)站等各類型客戶群體,為全球1000+企業(yè)提供全方位網(wǎng)站維護、服務(wù)器維護解決方案。
代碼如下:
<!--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"); // 設(shè)置文字樣式 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(); //設(shè)置你要限制的高度 var limitHeight = 50; //當(dāng)前文本行數(shù) var currentLine = (paragraphHeight/singleHeight).toFixed(0); //轉(zhuǎn)化為行數(shù) var lineCount = (limitHeight / singleHeight).toFixed(0); // alert(singleHeight+"----"+paragraphHeight+"---"+lineCount+"---"+currentLine); // 修改段落限制行數(shù) textContainer.attr("style", "-webkit-line-clamp:" + lineCount); // 設(shè)置按鈕的顯示或隱藏 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前名稱:通過javascript實現(xiàn)段落的收縮與展開
URL標(biāo)題:http://muchs.cn/article28/gesijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)