【问题标题】:Show more and less option with bootstrap使用引导程序显示更多和更少的选项
【发布时间】:2019-03-07 19:56:35
【问题描述】:

我有一个段落,如果行超过 3,那么它应该显示“显示更多选项”,如果我点击它,它将展开段落并“显示更少的选项应该显示。”如果我点击显示更少thenit 将折叠该段落。 我必须用引导程序来做,没有 jquery 和 javascript。

【问题讨论】:

  • 你能分享你到目前为止尝试过的代码吗?
  • 我很确定 Bootstrap 没有这样的内置控件……如果有的话,它会使用带有 jQ​​uery 库的 JavaScript 来构建它。
  • 我已经为 3 行设置了段落行高 1rem 和总高度 3 rem。我已将溢出属性设置为隐藏。如果我点击显示更多链接,那么它应该显示溢出文本。跨度>
  • 您可以使用js 在溢出属性的值之间切换

标签: html css bootstrap-4


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多