欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)文字展開和收起效果

 更新時(shí)間:2021年09月22日 16:39:11   作者:~小仙女~  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)文字展開和收起效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

列表式的文字的展開和收起的實(shí)現(xiàn),供大家參考,具體內(nèi)容如下

需求:

1、當(dāng)文字超出目標(biāo)值,則截取目標(biāo)值,其他隱藏,同時(shí)顯示“展開”二字和下拉箭頭;
2、點(diǎn)擊“展開”顯示所有文字,同時(shí)改為“收起”和上拉箭頭
3、如果文字本身就沒有超過目標(biāo)值,則顯示所有文字即可

之前想過使用css設(shè)置超出多少行隱藏,或者給Li標(biāo)簽設(shè)置高度隱藏,但都無法滿足以上第三條,所以想到了下邊一種方法將就可以使用

思路:

1、初始遍歷需要展開和收起的元素,超出目標(biāo)值隱藏,然后把所有標(biāo)簽中的內(nèi)容存起來(后邊顯示全部的時(shí)候會(huì)用到)
2、點(diǎn)擊展開和收起的時(shí)候,根據(jù)當(dāng)前的內(nèi)容去存儲(chǔ)的值中匹配,匹配到之后做相應(yīng)的處理,展示出來

HTML

<ul class="outList">
        <li>
            <div>5-14號(hào)</div>
            <ul class="innerList">
                <li class="wordsContent">111111111111111111111111</li>
                <li class="wordsContent">222222222222222222222222</li>
                <li class="wordsContent">333333333333333333333333</li>
            </ul>
        </li>
        <li>
            <div>5-15號(hào)</div>
            <ul class="innerList">
                <li class="wordsContent">4444</li>
                <li class="wordsContent">5555555555555555555555555</li>
                <li class="wordsContent">6666666666666666666666666</li>
            </ul>
        </li>
</ul>

CSS

ul,li {
   list-style: none;
 }
.innerList>li {
     margin-bottom: 0.2rem;
     border-bottom: 0.01rem solid green;
     box-sizing: border-box;
     padding: 0.2rem 5% 0.7rem 3%;
     position: relative;
     margin-bottom: 0.3rem;
 }
 .open {
     font-size: 0.22rem;
     color: #12309E;
     position: absolute;
     right: 0.2rem;
     bottom: 0.1rem;
     font-weight: bold;
 }
 .close {
     font-size: 0.22rem;
     color: #12309E;
     position: absolute;
     right: 0.2rem;
     bottom: 0.1rem;
     font-weight: bold;
 }

JS

//新聞的展開收起部分
var objList = $(".wordsContent");   //需要展開收起的li標(biāo)簽元素
var maxNum = 5;                     //目標(biāo)值的長(zhǎng)度
var arr = [];                       //需要展開收起的所有文字匯總
objList.delegate(".open", "click", function () {
    openClose(true, this)
})
objList.delegate(".close", "click", function () {
    openClose(false, this)
})
//初始化封裝,初始化是為了1:存儲(chǔ)原本的Li標(biāo)簽中的內(nèi)容;2:超出目標(biāo)值的文字隱藏
function init(objList, maxNum) {
    objList.each(function (index, item) {
        arr.push($(item_).text())
        if ($(item).text().length > maxNum) {
            $(item).html($(item).text().substr(0, maxNum) + "<span class='open'>展開<img src='./image/down^.png'/></span>")
        }
    })
}
init(objList, maxNum)

//展開和收起的封裝
function openClose(boo, clickObj) {
    var final = '';
    arr.map(function (item, index) {
        if (item.match($(clickObj).parents(".wordsContent").text().substring(0, $(clickObj).parents(".wordsContent").text().length - 2))) {
            final = item
        }
    })
    if (boo) {
        $(clickObj).parents(".wordsContent").html(final + "<span class='close'>收起<img src='./image/up^.png'/></span>")
    } else {
        $(clickObj).parents(".wordsContent").html(final.substr(0, maxNum) + "<span class='open'>展開<img src='./image/down^.png'/></span>")
    }
}

效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論