Bootstrap对话框使用实例讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Bootstrap对话框使用实例讲解

hakase   2020-05-15 我要评论

使用模态框的弹窗组件需要三层 div 容器元素

分别为 modal(模态声明层) dialog(窗口声明层) content(内容层)

在内容层里面,还有三层,分别为 header(头部)、 body(主体)、 footer(注脚) 

一个简单的对话框登陆/注册例子

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <link rel="stylesheet" href="./css/bootstrap.min.css">
 <script src="./js/jquery.min.js"></script>
 <script src="./js/bootstrap.min.js"></script>
 <style>
  .modal-dialog {
   width: 20%;
  }

  .modal-footer, .modal-header {
   text-align: center;
  }

  input {
   width: 80%;
  }

 </style>
</head>
<body>
 <!-- LOGIN MODULE -->
 <div id="loginModal" class="modal fade" tabindex="-1">
  <div class="modal-dialog">
   <div class="modal-content">
    <div class="modal-header">
     <button type="button" class="close" data-dismiss="modal">
      <span>×</span>
     </button>
     <h4 class="modal-title">会员登录</h4>
    </div>
    <div class="modal-body">
     <label for="log_uname">
      <span>帐号:</span>
      <input id="log_uname" name="log_uname" type="text" placeholder="input your account">
     </label>
     <br>
     <label for="log_passwd">
      <span>密码:</span>
      <input id="log_passwd" name="log_passwd" type="password" placeholder="input your password">
     </label>
    </div>
    <div class="modal-footer">
     <button type="button" class="btn btn-primary">登录</button>
     <button type="button" class="btn btn-warning" data-dismiss="modal">退出</button>
    </div>
   </div>
  </div>
 </div>

 <!-- LOGIN MODULE -->
 <div id="registerModal" class="modal fade" tabindex="-1">
  <div class="modal-dialog">
   <div class="modal-content">
    <div class="modal-header">
     <button type="button" class="close" data-dismiss="modal">
      <span>×</span>
     </button>
     <h4 class="modal-title">注册会员</h4>
    </div>
    <div class="modal-body">
     <label for="uname">
      <span>帐号:</span>
      <input id="reg_uname" name="reg_uname" type="text" placeholder="input your account">
     </label>
     <br>
     <label for="reg_passwd">
      <span>密码:</span>
      <input id="reg_passwd" name="reg_passwd" type="password" placeholder="input your password">
     </label>
     <label for="reg_confirm_passwd">
      <span>确认:</span>
      <input id="reg_confirm_passwd" name="reg_confirm_passwd" type="password" placeholder="confirm your password">
     </label>
    </div>
    <div class="modal-footer">
     <button type="button" class="btn btn-primary">注册</button>
     <button type="button" class="btn btn-warning" data-dismiss="modal">退出</button>
    </div>
   </div>
  </div>
 </div>

 <button class="btn btn-primary" data-toggle="modal" data-target="#loginModal">登陆</button>
 <button class="btn btn-warning" data-toggle="modal" data-target="#registerModal">注册</button>
</body>
</html>

对话框其他知识

jQuery方式声明对话框

$('#myModal').modal({
 show : true,
 backdrop : false,
 keyboard : false,
 remote : 'index.html',
});

jQuery方式显示对话框

$('#myBtn').on('click', function () {
 $('#myModal').modal('show');
});

对话框的事件

show.bs.modal  ==> 在show方法调用时立即触发

shown.bs.modal  ==> 在模态框完全显示出来并且CSS动画完成之后触发

hide.bs.modal ==> 在hide方法调用时 还未关闭隐藏时触发

hidden.bs.modal ==> 在模态框完全隐藏之后并且CSS动画完成之后触发

$('#myModal').on('show.bs.modal', function () {
 alert('show !');
});

边缘弹出框

<button class="btn btn-lg btn-danger" type="button" data-toggle="popover"
 title="弹出框" data-content="这是一个弹出框">点击弹出/隐藏弹出框</button>
<script>
 $('button').popover();
</script>

其他方法

$('button').popover('show'); //显示
$('button').popover('hide'); //隐藏
$('button').popover('toggle'); //反转显示和隐藏
$('button').popover('destroy'); //隐藏并销毁

事件

show.bs.popover ==> 在调用show方法时触发

shown.bs.popover ==> 在显示整个弹窗时触发

hide.bs.popover ===> 在调用hide方法时触发

hidden.bs.popover ==> 在完全关闭整个弹出时触发

如果大家还想深入学习,可以点击这里进行学习,再为大家附两个精彩的专题:Bootstrap学习教程 Bootstrap实战教程

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们