Pure CSS to achieve click to expand and read the full text function

Pure CSS to achieve click to expand and read the full text function

Note

When developing an article display list interface, you may want to display part of the article header content to provide a basic overview of the information, and display a button that says 【點擊展開閱讀全文】 to get detailed information.

The idea of ​​pure CSS in the article caibaojian.com/css-tonggle… was referred to. However, the content described in the article is applicable to the effect of a single article, and it is not friendly when using <li></li> tags to generate table data. Therefore, corresponding optimization was carried out on this basis. The specific code is as follows:

<div>
    <ul id="content-ul">
        <!-- This is the body of the LI tag that stores the article content-->
    </ul>
</div>
[id^="contTab"] {
    display: none;
}

.content-more {
    display: none;
}

[id^="contTab"]:checked ~ #content {
    max-height: 95px;
    overflow: hidden;
}

[id^="contTab"]:checked ~ .content-more {
    display: block;
    position: relative;
    text-align: center;
}

[id^="contTab"]:checked ~ .content-more .gradient {
    background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 0)), to(#fff));
    background-image: -webkit-linear-gradient(top, rgba(255, 255, 255, 0), #fff);
    background-image: linear-gradient(-180deg, rgba(255, 255, 255, 0), #fff);
    height: 80px;
    position: absolute;
    left: 0;
    top: -79px;
    width: 100%;
}

[id^="contTab"]:checked ~ .content-more .readmore {
    display: inline-block;
    background: #319a1717;
    color: #0014ff9e;
    width: 300px;
    height: 30px;
    border-radius: 32px;
    line-height: 32px;
    font-size: 14px;
    cursor: pointer;
    text-indent: 0;
}

Of course, here is a piece of JS code:

function inner(response) {
    for (var val of response.data) {
        document.getElementById('content-ul').innerHTML += '' +
            '<li>' +
            '<h2 class="title">' + val.title + '</h2>' +
            '<p class="update_author">' + val.author_name + ' / ' + layui.util.toDateString(val.update, "yyyy-MM-dd HH:mm:ss") + '</p>' +
            '<input type="checkbox" id="contTab_' + val.id + '" checked="checked" class="tabbed">' +
            '<div id="content">' + val.content + '</div>' +
            '<div class="content-more"><div class="gradient"></div> <label for="contTab_' + val.id + '" class="readmore">Click to read the full text</label></div>' +
            '</li>'
    }
}

illustrate

The improvement method is to dynamically generate and bind the bound tag ID attribute, and then use the CSS selector, using a fuzzy matching method that is not limited to a specific ID selector.

Source code

This code snippet is used in the M&OAS project. You can click here to view the relevant code information and get a more complete code.

PS: If you find that there is no relevant code block after entering, don't panic, maybe I haven't uploaded it to GITHU yet, please forgive me QAQ ~

The brain is a good thing, hahahaha~

This is the end of this article about how to implement the [click to expand and read full text] function with pure CSS. For more relevant CSS click to expand and read full text content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Build a severe weather real-time warning system with Node.JS

>>:  Solution to "No such file or directory" in locale after installing glibc-2.14 in CentOS6.5

Recommend

Detailed explanation of the use of grid properties in CSS

Grid layout Attributes added to the parent elemen...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution Recently,...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

Detailed examples of Docker-compose networks

Today I experimented with the network settings un...

JS+AJAX realizes the linkage of province, city and district drop-down lists

This article shares the specific code of JS+AJAX ...

Docker installation rocketMQ tutorial (most detailed)

RocketMQ is a distributed, queue-based messaging ...

CSS implements the bottom tapbar function

Now many mobile phones have the function of switc...

jQuery implements breathing carousel

This article shares the specific code of jQuery t...

Detailed explanation of Vue development website SEO optimization method

Because the data binding mechanism of Vue and oth...

HTML background color gradient achieved through CSS

Effect screenshots: Implementation code: Copy code...

Solution to the ineffective global style of the mini program custom component

Table of contents Too long to read Component styl...

Batch replace part of the data of a field in Mysql (recommended)

Batch replace part of the data of a field in MYSQ...