javascript - Show moreShow less text - Stack Overflow

I'm new to React I want to make show moreshow less button. When the text is not expanded the brow

I'm new to React I want to make show more/show less button. When the text is not expanded the browser should show only 3 lines. I found a solution with array (ReactJS How to add a show more/show less button), but I want to do it with text. My question is how to show exactly 3 lines of text and expand it on button click.

I'm new to React I want to make show more/show less button. When the text is not expanded the browser should show only 3 lines. I found a solution with array (ReactJS How to add a show more/show less button), but I want to do it with text. My question is how to show exactly 3 lines of text and expand it on button click.

Share Improve this question asked Mar 6, 2020 at 9:07 Bob SmithBob Smith 131 silver badge7 bronze badges 2
  • 1 if someone will have same problem, use npmjs./package/react-lines-ellipsis – Bob Smith Commented Mar 6, 2020 at 9:35
  • Well. Instead of number of lines it should be character limit as device lines may change based on device. If you want to set it based on character limit, check out my answer here. stackoverflow./a/67073016/5782438 – Krishna Nigalye Commented Apr 14, 2021 at 5:27
Add a ment  | 

4 Answers 4

Reset to default 3

You can use react-lines-ellipsis for the same by using npm install --save react-lines-ellipsis

  <LinesEllipsis
    text='long long text'
    maxLine='3'
    ellipsis='...'
    trimRight
    basedOn='letters'
  />

for more information

@IncrediblePony

I am using React. You don't need javascript etc. You can use in React add remove class. You can use like this.

.hide{
  width: 100%;
  line-height: 1.2em;
  height: 3.6em; 
  background-color: #363636;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

class App extends React.Component {
    state = {
        shown: true,
    };

    render() {      
        return (
            <div><h2 className={this.state.shown ? '' : 'hide'}>
It will ing long text here</h2><button onClick={() => this.setState({ shown: !this.state.shown })}>Show more</button></div>
        )
    }
}

const mountNode = document.getElementById('app');
React.render(<App />, mountNode);

You can use like this.

div {
  width: 100%;
  line-height: 1.2em;
  height: 3.6em;
  background-color: #363636;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

I found a simple example here: https://codepen.io/joshbivens/pen/LNLKor?editors=1010

I edited this to behave in such a way that the button toggles a long piece of text like so:

class App extends React.Component {
    state = {
        shown: true,
    };

    render() {      
        return (
            <div>
                <h2>{
                        this.state.shown ? 
                        "Bacon ipsum dolor amet brisket pancetta leberkas ..." : 
                        "Bacon ipsum dolor amet brisket pancetta leberkas, landjaeger shank tail capicola tri-tip meatball. Shankle pig jowl tail doner corned beef ham hock biltong pork belly burgdoggen pancetta. Cupim pork loin tongue pastrami. Ball tip corned beef strip steak salami, porchetta chicken pork chop shoulder capicola. Ball tip swine strip steak, ground round tail rump porchetta beef biltong pork chop sausage meatloaf drumstick. Short loin corned beef short ribs buffalo chislic sausage."
                    }</h2>
                <button onClick={() => this.setState({ shown: !this.state.shown })}>Show more</button>
            </div>
        )
    }
}

const mountNode = document.getElementById('app');
React.render(<App />, mountNode);

This is the basic principle behind it. You can modify your code to suit your needs with this I hope.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575777a4633954.html

相关推荐

  • javascript - Show moreShow less text - Stack Overflow

    I'm new to React I want to make show moreshow less button. When the text is not expanded the brow

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信