点滴生活

jQuery怎么实现点击按钮弹出可拖拽模态对话框

2024-03-27

这篇文章主要讲解了“jQuery怎么实现点击按钮弹出可拖拽模态对话框”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“jQuery怎么实现点击按钮弹出可拖拽模态对话框”吧!

css部分:

.dialog {
  display: none; /* 初始隐藏 */
  position: absolute;
  width: 300px;
  height: 200px;
  top: 50px;
  left: 50px;
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0,0,0,.2);
}
.dialog-header {
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  font-size: 16px;
  font-weight: bold;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ccc;
  cursor: move; /* 允许拖拽 */
}
.dialog-body {
  padding: 10px;
}

/* 遮罩层样式 */
.mask {
  display: none; /* 初始隐藏 */
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgba(0,0,0,.3);
}

html部分:

<!-- 点击按钮弹出对话框的按钮 -->

<button class="dialog-trigger">点击弹出对话框</button>

<!-- 对话框 -->

<div class="dialog" id="dialog">

  <div class="dialog-header">对话框</div>

  <div class="dialog-body">这里是对话框内容</div>

</div>

js部分:

$(function() {
  var $dialog = $('.dialog'); // 对话框
  var $mask = $('.mask'); // 遮罩层
  var isDragging = false; // 是否拖拽中

  // 点击弹出对话框
  $('.dialog-trigger').click(function() {
    $dialog.show(); // 显示对话框
    $mask.show(); // 显示遮罩层
  });

  // 拖拽对话框
  $dialog.find('.dialog-header').mousedown(function(e) {
    isDragging = true; // 开始拖拽
    var startX = e.pageX; // 鼠标按下时的X坐标
    var startY = e.pageY; // 鼠标按下时的Y坐标
    var left = $dialog.offset().left; // 对话框初始的left值
    var top = $dialog.offset().top; // 对话框初始的top值

    // 拖拽事件
    $(document).mousemove(function(e) {
      if (isDragging) {
        var moveX = e.pageX - startX; // 鼠标移动的X距离
        var moveY = e.pageY - startY; // 鼠标移动的Y距离
        $dialog.css({
          left: left + moveX + 'px',
          top: top + moveY + 'px'
        });
      }
    });

    // 停止拖拽事件
    $(document).mouseup(function() {
      isDragging = false;
    });
  });

  // 点击遮罩层或对话框的关闭按钮,隐藏对话框和遮罩层
  $mask.click(function() {
    $dialog.hide();
    $mask.hide();
  });
  $dialog.find('.dialog-close').click(function() {
    $dialog.hide();
    $mask.hide();
  });
});

完整实例如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery点击弹出模态对话框</title>
<style>

.dialog {
  display: none; /* 初始隐藏 */
  position: absolute;
  width: 300px;
  height: 200px;
  top: 50px;
  left: 50px;
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0,0,0,.2);
}
.dialog-header {
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  font-size: 16px;
  font-weight: bold;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ccc;
  cursor: move; /* 允许拖拽 */
}
.dialog-body {
  padding: 10px;
}

/* 遮罩层样式 */
.mask {
  display: none; /* 初始隐藏 */
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgba(0,0,0,.3);
}

</style>

</head>

<body>

<!-- 点击按钮弹出对话框的按钮 -->

<button class="dialog-trigger">点击弹出对话框</button>

<!-- 对话框 -->

<div class="dialog" id="dialog">

  <div class="dialog-header">对话框</div>

  <div class="dialog-body">这里是对话框内容</div>

</div>

<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>

<script>

$(function() {
  var $dialog = $('.dialog'); // 对话框
  var $mask = $('.mask'); // 遮罩层
  var isDragging = false; // 是否拖拽中

  // 点击弹出对话框
  $('.dialog-trigger').click(function() {
    $dialog.show(); // 显示对话框
    $mask.show(); // 显示遮罩层
  });

  // 拖拽对话框
  $dialog.find('.dialog-header').mousedown(function(e) {
    isDragging = true; // 开始拖拽
    var startX = e.pageX; // 鼠标按下时的X坐标
    var startY = e.pageY; // 鼠标按下时的Y坐标
    var left = $dialog.offset().left; // 对话框初始的left值
    var top = $dialog.offset().top; // 对话框初始的top值

    // 拖拽事件
    $(document).mousemove(function(e) {
      if (isDragging) {
        var moveX = e.pageX - startX; // 鼠标移动的X距离
        var moveY = e.pageY - startY; // 鼠标移动的Y距离
        $dialog.css({
          left: left + moveX + 'px',
          top: top + moveY + 'px'
        });
      }
    });

    // 停止拖拽事件
    $(document).mouseup(function() {
      isDragging = false;
    });
  });

  // 点击遮罩层或对话框的关闭按钮,隐藏对话框和遮罩层
  $mask.click(function() {
    $dialog.hide();
    $mask.hide();
  });
  $dialog.find('.dialog-close').click(function() {
    $dialog.hide();
    $mask.hide();
  });
});

</script>

</body>

</html>

感谢各位的阅读,以上就是“jQuery怎么实现点击按钮弹出可拖拽模态对话框”的内容了,经过本文的学习后,相信大家对jQuery怎么实现点击按钮弹出可拖拽模态对话框这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

jquery