如何在Bootstrap 4中设置水平排列的手风琴

如何在Bootstrap 4中设置水平排列的手风琴

垂直手风琴在网页开发中相当流行,但在少数情况下需要水平手风琴,在这篇文章中我们将使用指针事件。我们将采用书写模式来改变手风琴的标题视图。并使用转换功能来旋转整个手风琴。

下面的程序说明了上述方法。

代码:

<!DOCTYPE html>
<html>
 
<head>
    <title>Horizontal Accordion</title>
    <link href=
"https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
          rel="stylesheet" />
    <link href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
          rel="stylesheet" />
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js">
    </script>
    <script src=
"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js">
    </script>
    <style>
        .accordion {
            text-align: center;
        }
         
        .accordion.width {
            display: flex;
            width: 200px;
        }
         
        .accordion.width .card {
            flex-direction: row;
            min-width: min-content;
        }
         
        .accordion.width .card .card-header {
            cursor: pointer;
            transform: rotate(180deg);
            writing-mode: vertical-rl;
            background-color: #0F9D58;
            border: 2px solid black;
        }
         
        .card-body {
            width: 200px;
            background-color: ;
        }
         
        .collapsing.width {
            transition: width 0.5s ease;
            height: auto;
            width: 10px;
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <center>
 
        <!-- Header and tags -->
        <h1>GeeksforGeeks</h1>
        <b>A Computer Science Portal for Geeks</b>
         
 
<p>A Horizontal Accordion</p>
 
 
        <div class="accordion width"
             id="accordionHorizontalExample">
 
            <!-- Accordion group creating-->
            <div class="card">
                <div class="card-header"
                     data-toggle="collapse"
                     data-target="#collapseOne">
                    Accordion Slide 1
                </div>
                <div id="collapseOne"
                     class="collapse show width"
                     data-parent="#accordionHorizontalExample">
                    <div class="card-body">
                        If you like GeeksforGeeks and would like
                      to contribute you can also write an article
                      using write.geeksforgeeks.org
                    </div>
                </div>
            </div>
            <div class="card">
                <div class="card-header"
                     data-toggle="collapse"
                     data-target="#collapseTwo">
                    Accordion Slide 2
                </div>
                <div id="collapseTwo"
                     class="collapse width"
                     data-parent="#accordionHorizontalExample">
                    <div class="card-body">
                        If you like GeeksforGeeks and would like
                      to contribute you can also write an article
                      using write.geeksforgeeks.org
                    </div>
                </div>
                <div class="card">
                    <div class="card-header"
                         data-toggle="collapse"
                         data-target="#collapseThree">
                        Accordion Slide 3
                    </div>
                    <div id="collapseThree"
                         class="collapse width"
                         data-parent="#accordionHorizontalExample">
                        <div class="card-body">
                            If you like GeeksforGeeks and would like
                          to contribute you can also write an article
                          using write.geeksforgeeks.org
                        </div>
                    </div>
                </div>
                <script>
                    var horizontalAccordions = (".accordion.width");
 
                    horizontalAccordions.each(function() {
                        var accordion =(this);
                        var collapse = accordion.find(".collapse");
                        var bodies = collapse.find("> *");
                        accordion.height(accordion.height());
                        bodies.width(bodies.eq(0).width());
                        collapse.not(".show").each(function() {
                            $(this).parent().find("[data-toggle='collapse']")
                                .addClass("collapsed");
                        });
                    });
                </script>
          </div>
      </div>
    </center>
</body>
 
</html> 

输出:

  • 在点击Accordion之前。

如何在Bootstrap 4中设置水平排列的手风琴?

  • 点击Accordion后。

如何在Bootstrap 4中设置水平排列的手风琴?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Bootstrap教程