<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下拉菜单</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
width: 100%;
height: 100%;
}
html{
font-size: 10px;
}
.nav{
width: 100%;
height: 6rem;
background-color: bisque;
display: flex;
justify-content: center;
}
.content{
width: 40rem;
height: 5rem;
background-color: bisque;
display: flex;
justify-content: start;
padding: 5px;
}
.content .item{
width: 10rem;
height: 5rem;
background-color: bisque;
padding: 0 1rem;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.content .item:hover{
background-color: rgb(210, 120, 233);
color: yellow;
cursor: pointer;
}
.content .item ul{
position: absolute;
top: 100%;
left: 0;
width: 10rem;
background-color: rgb(146, 14, 223);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
}
.fu{
background-color: rgb(146, 14, 223);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="nav">
<ul class="content">
<li class="item">首页</li>
<li class="item">文章
<ul>
<li class="fu">我的文章</li>
<li class="fu">发表文章</li>
</ul>
</li>
<li class="item">相册
<ul>
<li class="fu">我的相册</li>
<li class="fu">发表相册</li>
</ul>
</li>
<li class="item">消息
<ul>
<li class="fu">我的私信</li>
<li class="fu">发表消息</li>
</ul>
</li>
</ul>
</div>
<script>
let menu1 = document.getElementsByClassName("item");
for (let i = 0; i < menu1.length; i++) {
menu1[i].onmouseenter = function () {
let subMenu = this.getElementsByTagName("ul")[0];
if (subMenu) {
subMenu.style.display = "flex";
}
};
menu1[i].onmouseleave = function () {
let subMenu = this.getElementsByTagName("ul")[0];
if (subMenu) {
subMenu.style.display = "none";
}
};
}
</script>
</body>
</html>

屏幕截图 2025-03-31 155548.png
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>鼠标拖动</title>
<style>
#box{
position: absolute;
width: 400px;
height: 300px;
background: #a008db;
}
</style>
</head>
<body>
<div id="box">
<img src="img/IMG_3531.JPG" width="400px" height="300px">
</div>
<script>
let box = document.getElementById("box")
box.onmousedown = function(e) {
let offsetX = e.offsetX
let offsetY = e.offsetY
console.log(offsetX, "offsetX")
console.log(offsetY, "offsetY")
document.onmousemove = function(e2) {
let clientX = e2.clientX
let clientY = e2.clientY
console.log(clientX, "clientX")
console.log(clientY, "clientY")
_left = clientX - offsetX
_top = clientY - offsetY
console.log(_left, "left")
console.log(_top, "top")
if(_left < 0) {
_left = 0;
}
if(_top < 0) {
_top = 0
}
if(_left > document.documentElement.clientWidth - box.offsetWidth) {
_left = document.documentElement.clientWidth - box.offsetWidth
}
box.style.left = _left + "px"
box.style.top = _top + "px"
}
}
document.onmouseup = function() {
document.onmousemove = null
}
</script>
</body>
</html>

屏幕截图 2025-03-31 155804.png