使用bootstrap显示越来越少的选项

问题描述 投票:-4回答:1

我有一个paragrap,其中如果行超过3然后它应该显示“显示更多选项”,如果我点击它将扩展段落和“显示更少选项应显示。”如果我点击显示较少然后将崩溃这段落。我必须使用bootstrap,只有没有jquery和javascript。

html css bootstrap-4
1个回答
1
投票

你可以使用javascriptjquery执行效果你需要做这样的事情; jsfiddle

   

 $("document").ready(function(){
        // find elements
    var banner = $("#banner-message")
    var button = $("button")
    // when first load the window
    if($('.option').length>3){
    
    $('.option').each(function(index){
    var option=$(this);
    if(index>2){
    option.toggleClass('hidden');
    }
    })
    }
    
    // handle click and add class
    button.on("click", function(){
     if($('.option').length>3){
    
    $('.option').each(function(index){
    var option=$(this);
    if(index>2){
    option.toggleClass('hidden');
    }
    });
    }
    });
    });
body {
    background: #20262E;
    padding: 20px;
    font-family: Helvetica;
  }
  
  #banner-message {
    background: #fff;
    border-radius: 4px;
    padding: 20px;
    font-size: 25px;
    text-align: center;
    transition: all 0.2s;
    margin: 0 auto;
    width: 300px;
  }
  
  button {
    background: #0084ff;
    border: none;
    border-radius: 5px;
    padding: 8px 14px;
    font-size: 15px;
    color: #fff;
  }
  
  #banner-message.alt {
    background: #0084ff;
    color: #fff;
    margin-top: 40px;
    width: 200px;
  }
  
  #banner-message.alt button {
    background: #fff;
    color: #000;
  }
  
  .hidden {
    display: none;
  }

body {
    background: #20262E;
    padding: 20px;
    font-family: Helvetica;
  }
  
  #banner-message {
    background: #fff;
    border-radius: 4px;
    padding: 20px;
    font-size: 25px;
    text-align: center;
    transition: all 0.2s;
    margin: 0 auto;
    width: 300px;
  }
  
  button {
    background: #0084ff;
    border: none;
    border-radius: 5px;
    padding: 8px 14px;
    font-size: 15px;
    color: #fff;
  }
  
  #banner-message.alt {
    background: #0084ff;
    color: #fff;
    margin-top: 40px;
    width: 200px;
  }
  
  #banner-message.alt button {
    background: #fff;
    color: #000;
  }
  
  .hidden {
    display: none;
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div id="banner-message">
        <div class='option'> option1</div>
         <div class='option'> option2</div>
          <div class='option'> option3</div>
           <div class='option'> option4</div>
      
        <button>Change color</button>
      </div>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.