<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<style>
.box-content {
position: fixed;
bottom: 200px;
right: 80px;
border-radius: 35px;
background-color: #000;
cursor: move;
}
.box-content .btn {
width: 90px;
line-height: 70px;
color: #fff;
text-align: center;
font-size: 24px;
}
.box-content .top {
width: 180px;
color: #fff;
line-height: 70px;
font-size: 20px;
justify-content: space-evenly;
display: none;
}
.box-content .top.active {
display: flex;
}
</style>
<body>
<div class="box-content">
<div class="btn">操作</div>
<div class="top">
<div class="one">按钮1</div>
<div class="two">按钮2</div>
</div>
</div>
</body>
<script>
$(document).ready(function () {
let canClick = true;
$('.box-content').bind('mousedown', function (e) {
let startX = e.pageX - $(this).offset().left;
let startY = e.pageY - $(this).offset().top;
let clientW = document.documentElement.clientWidth;
let clientH = document.documentElement.clientHeight;
let _this = $(this);
e.preventDefault();
e.stopPropagation();
$(document).bind('mousemove', function (e) {
canClick = false;
let offsetW = (e.pageX - startX) + _this.width();
let offsetH = (e.pageY - startY) + _this.height();
console.log(offsetH);
if (offsetW >= clientW||offsetH>=clientH||offsetW<=_this.width()||offsetH<=_this.height()) return false;
_this.css({
top: e.pageY - startY + 'px',
left: e.pageX - startX + 'px',
right: 'auto',
bottom: 'auto'
})
e.preventDefault();
e.stopPropagation();
}).bind('mouseup', function (e) {
$(document).unbind('mousemove');
e.preventDefault();
e.stopPropagation();
})
})
$('.box-content .btn').click(function () {
if (!canClick) {
canClick = true;
return false;
}
$(this).hide();
$('.box-content .top').addClass('active');
})
$('.box-content .top').click(function () {
if (!canClick) {
canClick = true;
return false;
}
$(this).removeClass('active');
$('.box-content .btn').show();
})
})
</script>
</html>