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 MySQL database Event scheduled execution tasks

1. Background As the project's business conti...

Detailed explanation of MySQL batch SQL insert performance optimization

For some systems with large amounts of data, the ...

How to make a centos base image

Preface Now the operating system used by my compa...

Write a dynamic clock on a web page in HTML

Use HTML to write a dynamic web clock. The code i...

How to achieve 3D dynamic text effect with three.js

Preface Hello everyone, this is the CSS wizard - ...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

Master the CSS property display:flow-root declaration in one article

byzhangxinxu from https://www.zhangxinxu.com/word...

Difference between MySQL btree index and hash index

In MySQL, most indexes (such as PRIMARY KEY, UNIQ...

Solve the matching problem in CSS

Problem Description As we all know, when writing ...

What are mysql dirty pages?

Table of contents Dirty pages (memory pages) Why ...

Summary of Linux command methods to view used commands

There are many commands used in the system, so ho...

A brief discussion on how to use slots in Vue

How to define and use: Use the slot tag definitio...

Detailed steps for Linux account file control management

In the Linux system, in addition to various accou...