Home >Web Front-end >Front-end Q&A >Javascript implements expansion and collapse of a piece of text
JavaScript implements the expand and collapse function of a paragraph of text
As web design pays more and more attention to user experience, more and more page designs will need to expand or collapse a paragraph of text. This Sometimes we can use JavaScript code to achieve this function. Let's take a look at how to use JavaScript to expand and collapse a piece of text.
First, we need to prepare a piece of HTML code. This code can be the text that needs to be expanded and collapsed and the rest of the page elements. The sample code is as follows:
<div class="content"> <p>这是一段需要展开收起的文字,可以有很多很多的字,可能会占据很多的空间。因此,在默认情况下,我们只会显示部分文字,而把其余的部分折叠起来。如果需要查看全部内容,可以点击“查看更多”按钮,文字内容就会展开显示了。</p> <button class="btn-more">查看更多</button> </div>
In this code, we use a dc6dce4a544fdca2df29d5ac0ea9906b
tag to wrap the text and button elements that need to be expanded and collapsed. In the dc6dce4a544fdca2df29d5ac0ea9906b
tag, we use a e388a4556c0f65e1904146cc1a846bee
tag to display the text content that needs to be expanded and collapsed. Below the text content, use a < ;button>
tag to display the "See more" button.
We need to use CSS to set the style of the text that needs to be expanded and collapsed in the default and expanded states.
.content p { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; /* 设置文字溢出时显示省略号 */ } .content p.is-expanded { overflow: visible; white-space: normal; /* 文字溢出时不再显示省略号 */ } .btn-more { display: block; margin-top: 10px; cursor: pointer; /* 设置鼠标指针为手型 */ }
Next, we need to use JavaScript code to implement the click event of the "View More" button so that it can be expanded or collapsed when the button is clicked Text that needs to be expanded or collapsed.
First, we can create a variable to store whether it is currently in the expanded state. The initial state is false, which means it is in the collapsed state.
let isExpanded = false;
When the user clicks the "View More" button, we need to get the e388a4556c0f65e1904146cc1a846bee
element that needs to expand and collapse the text, and add or remove a # for this element. ##is-expanded's class. At the same time, change the text content of the button to display "Less" or "View More" to remind the user of the current text status.
const content = document.querySelector('.content'); const btnMore = document.querySelector('.btn-more'); btnMore.addEventListener('click', function() { const paragraph = content.querySelector('p'); isExpanded = !isExpanded; if (isExpanded) { paragraph.classList.add('is-expanded'); btnMore.innerText = '收起'; } else { paragraph.classList.remove('is-expanded'); btnMore.innerText = '查看更多'; } });In this code, we first use the
document.querySelector() method to obtain the
e388a4556c0f65e1904146cc1a846bee elements corresponding to the text that needs to be expanded and collapsed. The
bb9345e55eb71822850ff156dfde57c8 element corresponding to the "View More" button. Then, add a click event listener to the button element, which will fire when the user clicks the button.
isExpanded variable to determine whether the current text is expanded or collapsed. If it is in the expanded state, we will add a class
is-expanded to the
e388a4556c0f65e1904146cc1a846bee element and set the button's text to "Collapse". Instead, we will remove the
is-expanded class and set the button's text to "See more".
The above is the detailed content of Javascript implements expansion and collapse of a piece of text. For more information, please follow other related articles on the PHP Chinese website!