[Bootstap] 9. Dropdown

Dropdown Arrow Class

In order to create a down arrow like this: , what class should we apply to the element?

Answer: caret

Opening Dropdowns

The Bootstrap Dropdown plugin will toggle a specific class on our element in order to make the dropdown visible. Will the following dropdown menu be visible when the page loads?

<div class=‘btn-group in‘>
  <a href=‘book.html‘ data-target=‘#‘ data-toggle=‘dropdown‘ class=‘btn btn-default‘>Boot Tickets Now <span class=‘caret‘></span></a>

  <ul class=‘dropdown-menu‘>
    <li><a href=‘book-teleporter.html‘>Teleporter</a></li>
    <li><a href=‘book-elevator.html‘>Orbital Elevator</a></li>
    <li><a href=‘book-rocketbus.html‘>RocketBus</a></li>
  </ul>
</div>
  • Yes, it will be visible when the page loads.
  • No, it will not be visible when the page loads.

If you want it open, add a ‘open‘ class to the ul.

Take a Tour Today!

We can use the Dropdown Plugin for more than just navigation components. One other place we could use it is on our "Take the Tour" button. Follow the tasks below to convert this button into a Dropdown.

Within our "Take the Tour" button, we‘ll want to show a little down arrow so our travelers are encouraged to click on it. Add this icon after the text.

<!DOCTYPE html>
<html>
  <head>
    <title>Blasting Off With Bootstrap</title>
    <link href=‘css/bootstrap.min.css‘ rel=‘stylesheet‘>
    <link href=‘css/main.css‘ rel=‘stylesheet‘>
  </head>
  <body>
    <div class=‘navbar navbar-default navbar-static-top‘>
      <div class=‘container‘>
        <a href=‘/‘ class=‘navbar-brand‘>Blasting Off With Bootstrap</a>

        <ul class=‘nav navbar-nav navbar-right‘>
          <li><a href=‘tickets.html‘>Tickets</a></li>
          <li><a href=‘stations.html‘>Stations</a></li>
          <li><a href=‘about.html‘>About</a></li>
        </ul>
      </div>
    </div>
    <div class=‘container‘>
      <div class=‘row well well-lg‘>
        <div class=‘col-md-6‘>
          <h2>The Fastest Way to Space</h2>
          <p class=‘lead‘>Make your way to space in the comfort of your own rocket, elevator or transporter.</p>

          <div class=‘btn-group‘>
            <button type=‘button‘ class=‘btn btn-default btn-lg‘>
              Take the Tour
            </button>
            <ul>
              <li><a href=‘#‘>Transporter</a></li>
              <li><a href=‘#‘>Orbital Elevator</a></li>
              <li><a href=‘#‘>RocketBus</a></li>
              <li><a href=‘#‘>Learn More</a></li>
            </ul>
          </div>

          <button type=‘button‘ class=‘btn btn-lg btn-primary‘>Book Tickets Now</button>
        </div>
        <div class=‘col-md-6 visible-md visible-lg‘>
          <img src=‘images/img-header.jpg‘ alt=‘Blast off with Bootstrap‘ />
        </div>
      </div>
      <div class=‘row text-center features‘>
        <div class=‘col-sm-4 col-xs-10 col-xs-offset-1 col-sm-offset-0‘>
          <i class=‘glyphicon glyphicon-briefcase‘></i>
          <h3>Book Today!</h3>
          <p>Even if you‘re traveling tomorrow, you can still get tickets today. We have a number of conveniently located ports around the globe to service everyone.</p>
        </div>

        <div class=‘col-sm-4 col-xs-6‘>
          <i class=‘glyphicon glyphicon-random‘></i>
          <h3>Go Anywhere</h3>
          <p>If you need to get to space today, why not try out a transporter? Despite the claims, there are have been no deaths in the last 6 weeks!</p>
        </div>

        <div class=‘col-sm-4 col-xs-6‘>
          <i class=‘glyphicon glyphicon-send‘></i>
          <h3>RocketBus&reg;</h3>
          <p>For cheapest fares, catch the next RocketBus&reg; to the stars. Cheaper on your wallet, and easiest way to make friends.</p>
        </div>
      </div>
    </div>
    <div class=‘quote‘>
      <div class=‘container‘>
        <blockquote>
          <p>Any sufficiently advanced technology is indistinguishable from magic.</p>
          <footer>Arthur C. Clarke in <cite title="Source Title">Profiles of the Future</cite></footer>
        </blockquote>
      </div>
    </div>
    <div class=‘container transport-systems‘>
      <div class=‘row‘>
        <div class=‘col-md-10 col-md-offset-1‘>
          <h2>Our Transport Systems</h2>
          <p>Learn more about our transport systems to find out which one is right for you. Pick out the mode of transport that works for your budget and risk level.</p>
        </div>
      </div>

      <div class=‘row text-center‘>
        <div class=‘transporter col-md-3 col-md-offset-1 well well-sm‘>
          <h3>Transporter</h3>
          <ul class=‘list-unstyled‘>
            <li>8 second travel time</li>
            <li>Chance of death only 1 in 7,593</li>
            <li>Low price of only $15.99!</li>
          </ul>
          <p><button class=‘btn btn-info‘><i class=‘glyphicon glyphicon-transfer‘></i> Beam Me Up!</button></p>
        </div>
        <div class=‘space-elevator col-md-3 col-md-offset-1 well well-sm‘>
          <h3>Space Elevator</h3>
          <ul class=‘list-unstyled‘>
            <li>8 hour scenic ride</li>
            <li>Only 1 horrific death per 12,456</li>
            <li>Only $45.99 if you book today!</li>
          </ul>
          <p><button class=‘btn btn-info‘><i class=‘glyphicon glyphicon-sort‘></i> Board the Elevator!</button></p>
        </div>
        <div class=‘rocketbus col-md-3 col-md-offset-1 well well-sm‘>
          <h3>RocketBus</h3>
          <ul class=‘list-unstyled‘>
            <li>8 minute scenic ride</li>
            <li>Plunging death rate of under 1/100k</li>
            <li>$74.99 lets you blast off today!</li>
          </ul>
          <p><button class=‘btn btn-info‘><i class=‘glyphicon glyphicon-plane‘></i> Blast Off!</button></p>
        </div>
      </div>
    </div>
    <div class=‘quote‘>
      <div class=‘container‘>
        <blockquote>
          <p>A dream that became a reality and spread throughout the stars.</p>
          <footer>Captain Kirk in <cite title="Source Title">Whom Gods Destroy</cite></footer>
        </blockquote>
      </div>
    </div>
    <div class=‘container‘>
      <div class=‘row‘>
        <div class=‘col-md-8‘>
          <h2>Plan Your Trip Today!</h2>

          <ul class=‘nav nav-tabs‘>
            <li class=‘active‘><a href=‘#time‘>Todays Times</a></li>
            <li><a href=‘#deals‘>Hot Deals</a></li>
            <li><a href=‘#forecast‘>Forecast</a></li>
          </ul>

          <div class=‘tab-content‘>
            <div class=‘tab-pane fade active‘ id=‘time‘>
              <p>Todays Time here</p>
            </div>
            <div class=‘tab-pane fade‘ id=‘deals‘>
              <p>Deals here</p>
            </div>
            <div class=‘tab-pane fade‘ id=‘forecast‘>
              <p>Forecast</p>
            </div>
          </div>
        </div>

        <div class=‘col-md-3 col-md-offset-1‘>
          <div class=‘panel-group‘>
            <div class=‘panel panel-success‘>
              <div class=‘panel-heading‘>
                <h4 class=‘panel-title‘>
                  <a data-toggle=‘collapse‘ data-parent=‘.panel-group‘ href=‘#systemsOperational‘>All Systems Operational</a>
                </h4>
              </div>
              <div id=‘systemsOperational‘ class=‘panel-collapse collapse in‘>
                <div class=‘panel-body‘>
                  <p>All systems are operational</p>
                  <ul class=‘list-unstyled‘>
                    <li><i class=‘glyphicon glyphicon-ok‘></i>  Transporters</li>
                    <li><i class=‘glyphicon glyphicon-ok‘></i>  Space Elevator</li>
                    <li><i class=‘glyphicon glyphicon-ok‘></i>  RocketBus</li>
                  </ul>
                </div>
              </div>
            </div>

            <div class=‘panel panel-warning‘>
              <div class=‘panel-heading‘>
                <h4 class=‘panel-title‘>
                  <a data-toggle=‘collapse‘ data-parent=‘.panel-group‘ href=‘#weatherAlert‘>Weather Alert</a>
                </h4>
              </div>
              <div id=‘weatherAlert‘ class=‘panel-collapse collapse‘>
                <div class=‘panel-body‘>
                  <p>The National Weather Service has issued a solar flare watch for the following time periods. Please plan accordingly:</p>
                  <ul class=‘list-unstyled‘>
                    <li><strong>February 9th, 21:43</strong></li>
                    <li><strong>May 18, 19:82</strong></li>
                    <li><strong>July 4, 08:42</strong></li>
                  </ul>
                </div>
              </div>
            </div>

            <div class=‘panel panel-danger‘>
              <div class=‘panel-heading‘>
                <h4 class=‘panel-title‘>
                  <a data-toggle=‘collapse‘ data-parent=‘.panel-group‘ href=‘#transportationWarning‘>Transportation Generals Warning: Transporters</a>
                </h4>
              </div>
              <div id=‘transportationWarning‘ class=‘panel-collapse collapse‘>
                <div class=‘panel-body‘>
                  <p>Technically, transporting means making a clone of yourself and killing off the other one. If you believe in a soul, you should be aware of this fact before attempting travel using a transporter.</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class=‘footer‘>
      <div class=‘container‘>
        <div class=‘row‘>
          <div class=‘col-md-3 col-sm-4 col-xs-6‘>
            <h4>Who We Are</h4>
            <p><i>Blasting Off With Bootstrap</i> is the fastest way to space. <a href=‘tickets.html‘>Book your ticket today</a>!</p>
            <p><a href=‘about.html‘>More About Us <i class=‘glyphicon glyphicon-arrow-right‘></i></a></p>
          </div>

          <div class=‘col-md-offset-1 col-sm-2 col-xs-6‘>
            <h4>Links</h4>
            <ul class=‘list-unstyled‘>
              <li><a href=‘/‘>Home</a></li>
              <li><a href=‘tickets.html‘>Tickets</a></li>
              <li><a href=‘stations.html‘>Stations</a></li>
            </ul>
          </div>

          <div class=‘clearfix visible-xs‘></div>

          <div class=‘col-sm-2 col-xs-6‘>
            <h4>Stay in Touch</h4>
            <ul class=‘list-unstyled‘>
              <li><a href=‘about.html‘>About</a></li>
              <li><a href=‘contact.html‘>Contact Us</a></li>
              <li><a href=‘/blog‘>Blog</a></li>
              <li><a href=‘http://twitter.com/codeschool‘>Twitter</a></li>
              <li><a href=‘http://facebook.com/codeschool‘>Facebook</a></li>
            </ul>
          </div>

          <div class=‘col-md-3 col-md-offset-1 col-sm-4 col-xs-6‘>
            <h4>Contact Us</h4>
            <ul class=‘list-unstyled‘>
              <li><i class=‘glyphicon glyphicon-globe‘></i> Orlando, FL</li>
              <li><i class=‘glyphicon glyphicon-phone‘></i> 1-555-blast-off</li>
              <li><i class=‘glyphicon glyphicon-envelope‘></i> <a href=‘mailto:[email protected]‘>[email protected]</a></li>
            </ul>
            <p>Blasting Off With Bootstrap &copy;2214.</p>
          </div>
        </div>
      </div>
    </div>

    <script src=‘https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js‘></script>
    <script src=‘js/bootstrap.min.js‘></script>
    <script>
      $(function() {
        $(‘.nav-tabs a‘).click(function (e) {
          e.preventDefault();
          $(this).tab(‘show‘);
        });
      });
    </script>
  </body>
</html>

Within our "Take the Tour" button, we‘ll want to show a little down arrow so our travelers are encouraged to click on it. Add this icon after the text.

            <button type=‘button‘ class=‘btn btn-default btn-lg‘>
              Take the Tour<span class="caret"></span>
            </button>

Our list of links for this Dropdown is currently unstyled. Give it a Bootstrap class to indicate it is a Dropdown Menu.

            <ul class="dropdown-menu">
              <li><a href=‘#‘>Transporter</a></li>
              <li><a href=‘#‘>Orbital Elevator</a></li>
              <li><a href=‘#‘>RocketBus</a></li>
              <li><a href=‘#‘>Learn More</a></li>
            </ul>

In order to tie in the structure and behavior, we‘ll need to add a specific attribute to our button to let Bootstrap know this is a Dropdown. Add that attribute and value.

            <button type=‘button‘ class=‘btn btn-default btn-lg‘ data-toggle="dropdown">
              Take the Tour<span class="caret"></span>
            </button>

Our Dropdown is working! But it seems a little weird we have our 3 transportation methods, and an unrelated link to "Learn More" in there. Go ahead and add a divider before the "Learn More" link.

              <li><a href=‘#‘>Transporter</a></li>
              <li><a href=‘#‘>Orbital Elevator</a></li>
              <li><a href=‘#‘>RocketBus</a></li>
              <li class="divider"></li>
              <li><a href=‘#‘>Learn More</a></li>

Lastly, we want to indicate that the "Take the Tour" button will popup a dropdown rather than taking us to a new page. We could do this by adding a Glyphicon, but in this case there‘s an easier way with a class provided by Bootstrap.

Checkout the Bootstrap documentation on Button Dropdowns and add the dropdown-toggle class to the appropriate place.

            <button type=‘button‘ class=‘btn btn-default btn-lg dropdown-toggle‘ data-toggle="dropdown">
              Take the Tour<span class="caret"></span>
            </button>
时间: 2024-11-18 07:44:08

[Bootstap] 9. Dropdown的相关文章

Bootstrap源码:dropdown.js

bootstrap的dropdown.js,封装了一个非常灵活易用的下拉组件,在各种下拉场景中稍加变换,都能实现目标效果,还能跟其他的组件良好地结合,比如前面的tab.js,搭配完成更强大的组件功能.这个组件除了js之外,html的结构和css的配合更是精妙,我从这个组件里面学到了不少有用的经验和技巧,下面是它的html结构: <div class="dropdown"> <a href="#" class="dropdown-toggl

Bootstrap 学习之js插件(下拉菜单(Dropdown)插件)

Bootstrap 下拉菜单(Dropdown)插件 Bootstrap 下拉菜单 这一章讲解了下拉菜单,但是没有涉及到交互部分,本章将具体讲解下拉菜单的交互.使用下拉菜单(Dropdown)插件,您可以向任何组件(比如导航栏.标签页.胶囊式导航菜单.按钮等)添加下拉菜单. 用法 您可以切换下拉菜单(Dropdown)插件的隐藏内容: 通过 data 属性:向链接或按钮添加 data-toggle="dropdown" 来切换下拉菜单,如下所示: <div class="

bootstrap 下拉菜单(dropdown)插件

使用下拉菜单的插件,您可以向任何组件(比如:导航栏,标签页,胶囊式导航,按钮)添加下拉菜单 用法 您可以切换下拉菜单(dropdown)插件隐藏内容 1.通过data属性,向链接或按钮添加data-toggle="dropdown"来切换下拉菜单. <div class="dropdown"> <a href="#" data-toggle="dropdown">下拉菜单触发器</a> &l

Unity学习笔记(2)——关于Dropdown的学习

今天组长让我写一个界面,其中用到了下拉条,我的印象还停留在以前的NGUI有现成的组件,但是uGUI没有的那阶段,组长跟我说uGUI现在也有了,研究了一下,发现real方便哦,今天就来简单说一下用法吧. Dropdow 用法也就是点开会有一个下拉列表,选中的可以显示.那么第一个问题来了 就是怎么在下拉列表里填上我们所需要的选项,显示我们需要的个数首先呢,遇到一个没有用过的组件不要紧张,可以先看看面板上都有些什么属性,一般来说Inspect界面上的都是可以获取的,有些事可以改的有些的不能改的,如上图

asp.net MVC5 中的捆绑和更改bootstap默认的样式

在MVC5的视图中使用@Scritps.Render(),@Styles.Render() 分别可以加载样式和脚本.捆绑的和实际的路径都可以. 并且可以利用 编程的方式灵活引用css文件和脚本文件. 一.Layout页面头部会使用 @Styles.Render("~/Content/css") 来加载css文件,非实际文件路径.  相当于 <link href="css.css" rel="stylesheet" type="te

Spring MVC-表单(Form)标签-下拉框(Dropdown)示例(转载实践)

以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_dropdown.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显示如何使用Spring Web MVC框架在表单中使用Dropdown.首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序: 步骤 描述 1 创建一个名为HelloWeb的项目,在一个包com.tutorialspo

按钮--bootStap

<!doctype html> <html> <head> <meta charset="utf-8"> <title>按钮</title> <link href="../../BootStrap/bootstrap-3.3.5-dist/css/bootstrap.css" rel="stylesheet" /> <meta name="view

jQuery Drop-Down 自定义下拉框

在线实例 实例演示 使用方法 <section class="main"> <div class="wrapper-demo"> <div id="dd" class="wrapper-dropdown-1" tabindex="1"> <span>手册网</span> <ul class="dropdown" tabin

Bootstrap源码分析之dropdown

源码分析: Dropdowns.scss:下拉框模块 Javascripts/bootstrap/dropdown.js:实现下拉框响应 实现功能及原理: 下拉选项卡,默认不能实现显示选中项的功能 原理: 1.利用dropdown类作为定位点,然后让子级的列表dropdown-menu绝对定位实现,还需要加一个单击点作为设置data-toggle=”dropdown”才能做关联.2. 需要js插件的支持 源码分析: 1.caret:实现向下的三角形,利用边框实现的    1.1.边框颜色默认是字