JS实现DIV的拖动

JS实现DIV的拖动

第一种

使用JQuery和JQuery-UI组件

JQuery-UI:https://www.runoob.com/jqueryui/example-draggable.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
  <script src="https://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="https://apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
  <style>
  #draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
  });
  </script>
</head>
	<body>
		<div id="draggable" class="ui-widget-content">
		  <p>请拖动我!</p>
		</div>
	</body>
</html>

第二种

使用JavaScript和样式实现

拖动功能的实现

$(function(){
    dragPanelMove(".titleMain","#left_bottom_div_tips_win");
});

function dragPanelMove(titleDiv, mainWinDiv) {
    $(titleDiv).mousedown(function (e) {
        var isMove = true;
        var div_x = e.pageX - $(mainWinDiv).offset().left;
        var div_y = e.pageY - $(mainWinDiv).offset().top;
        $(document).mousemove(function (e) {
            if (isMove) {
                var obj = $(mainWinDiv);
                obj.css({"left": e.pageX - div_x, "top": e.pageY - div_y});
            }
        }).mouseup(
            function () {
                isMove = false;
            });
    });
}

div样式

<div id="left_bottom_div_tips_win" style="width: 200px;height: 200px;box-shadow: 1px 1px 5px 2px #999;
                                          background: #fff;position: absolute;bottom: 1%;right: 1%;">
    <div class="titleMain" style="height: 20px;background: #f2f2f2;cursor: move;">
        <span style="font-size: 14px;vertical-align: middle;"></span>
        <div id="closeDivWin" style="position: absolute;text-align: center;width: 18px;height: 18px;
                                     box-shadow: 0px 0px 0px 1px #d9d9d9;right: 1px;cursor: pointer"
                                     onclick="$('#left_bottom_div_tips_win').fadeOut(500);" 
                                     onmouseover="$(this).css('background','red');" 
                                     onmouseout="$(this).css('background','')">X
        </div>
    </div>
    <div style="position: relative;left: 30px;top: 10px;display: flex;align-items: center;">
        <img src="" style="width: 30px;height: 30px;box-shadow: 1px 1px 3px 1px #999;border-radius: 100%;">
        <span style="padding-left: 10px;font-size: 25px;">ML</span>
    </div>
    <div class="online-msg" style="margin: 15px 0px 0px 10px;">
        <div class="online-time" style="font-size: 12px;color: #999;">2022-05-25 11:06</div>
        <div class="online-content" style="padding: 5px 2px;font-size: 13px;max-height: 70px;
                                           max-width: 165px;overflow: hidden; text-overflow: ellipsis;
                                           line-height: 1;color: blue;">
            消息内容通知
        </div>
    </div>
</div>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值