前端实践:如何实现一个弹窗

前端开发中,我们经常会遇到需要设计弹窗的情形,今天就来看看如何用原生js写一个弹窗

功能说明

​ 我们要实现的功能是点击按钮弹出弹窗,然后点击右上角的关闭按钮或者点击其他非弹窗的区域弹窗消失。

HTML

​ 首先先写HTML布局:

    <button id="button">点击打开弹窗</button>
    <!-- 弹窗背景 -->
    <div id="modal">
        <!-- 弹窗 -->
      <div id="modal-content">
        这是一个弹窗
        <span id="close">&times;</span>
      </div>
    </div>

 效果:

CSS
  <style>
      * {
        padding: 0px;
        margin: 0px;
      }
      /* 弹窗背景 */
      #modal {
        width: 100%;
        height: 100%;
        /* 默认隐藏 */
        /* display: none; */
        /* 固定定位 */
        position: fixed;
        /* 设置在顶层 */
        z-index: 1000;
        /* 设置位置 */
        left: 0;
        top: 0;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
      }
      /* 弹窗内容 */
      #modal-content {
        position: relative;
        background-color: #fff;
        margin: 10% auto;
        border: 1px solid #888;
        width: 1000px;
        height: 580px;
        z-index: 1001;
      }
      /* 关闭按钮 */
      #close {
        position: absolute;
        right: 20px;
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
      }
/* 设置关闭按钮的鼠标指针 */
      #close:hover,
      #close:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
      }
    </style> 

效果:

然后我们要让弹窗先隐藏起来,使用display:none;或者visibility:hidden;

效果:

JavaScript

通过设置监听函数后来改变元素的样式display

   <script>
        // 点击按钮
      var button = document.getElementById("button");
      var modal = document.getElementById("modal")
      button.onclick = function () {
        modal.style.display = "block";
      };
    //   关闭按钮
      document.getElementById("close").onclick = function () {
            modal.style.display = "none";
      };
    //   点击其他领域(即弹窗背景)
    window.onclick = function(event){
        if(event.target ==modal){
                modal.style.display = "none"
        }
    }
    </script>

效果:

完善

最后我们根据需要,可以使弹窗的内容和图片无法被选中和复制

  /* 设置页面不可选择,复制 */
  -moz-user-select: none;
  -o-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;

设置图片无法被拖拽:

<body  ondragstart="window.event.returnValue=false;return false;" oncontextmenu="window.event.returnValue=false;return false;" onselectstart="event.returnValue=false;return false;">

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现前端得到后端的数据并在弹窗窗口中显示,可以使用以下步骤: 1. 在前端页面中定义一个弹窗窗口,比如使用Bootstrap的Modal组件。 2. 在前端页面中使用ajax向后端发送请求,获取数据。 3. 在ajax的回调函数中将后端返回的数据填充到弹窗窗口中。 下面是一个简单的示例代码: 前端页面: ```html <!-- 弹窗窗口 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title" id="myModalLabel">弹窗标题</h4> </div> <div class="modal-body"> <p id="modal-content"></p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> </div> </div> </div> </div> <!-- 发送ajax请求 --> <button id="btn-show-modal">显示弹窗</button> <script> $(function() { $('#btn-show-modal').click(function() { $.ajax({ url: '/data', type: 'GET', success: function(data) { $('#modal-content').text(data); $('#myModal').modal('show'); }, error: function() { alert('请求失败!'); } }); }); }); </script> ``` 后端代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/data') def get_data(): data = '后端返回的数据' return jsonify(data) if __name__ == '__main__': app.run() ``` 这里使用jQuery的ajax方法发送GET请求,获取后端返回的数据,并将其填充到弹窗窗口中。注意需要引入jQuery和Bootstrap的相关库。后端使用Flask框架,定义一个路由返回数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值