满足1.对于页面上的一幅图片,鼠标悬停在图片上时切换成另一幅图,鼠标离开时,还原成原来的图片 2. 实现在页面上的一幅图片,能够跟随鼠标移动。 3. 实现在页面上的一幅图片,可以通过上下左右键控制图片在页面上移动。 4.在页面上建立一个自定义菜单,菜单内容如图所示,当单击菜单选项时,可以改变页面上文字相应设置。 5.在页面上有两个div块,实现内部div块可以在其外部容器div块中随意拖动,并实现磁吸效果。 6.页面上有一张图片和一个自定义滚动条,实现如下效果:拖动滚动条,可以改变图片大小。的html文件
时间: 2024-02-21 11:02:14 浏览: 82
九宫格图片鼠标悬停上下滑动图片切换显示代码.zip
这里是一个简单的 HTML 文件,包含了实现上述功能的代码。
```html
<!DOCTYPE html>
<html>
<head>
<title>Image Manipulation</title>
<style>
#container {
position: relative;
width: 600px;
height: 400px;
border: 1px solid black;
margin: 20px auto;
}
#image {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: all 0.5s ease-in-out;
}
#menu {
display: flex;
justify-content: space-between;
align-items: center;
width: 600px;
margin: 20px auto;
}
.menu-item {
padding: 10px;
border: 1px solid black;
cursor: pointer;
}
#text {
font-size: 24px;
text-align: center;
margin: 20px auto;
}
#outer {
position: relative;
width: 600px;
height: 400px;
border: 1px solid black;
margin: 20px auto;
}
#inner {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
border: 1px solid black;
cursor: move;
}
#scrollbar {
width: 20px;
height: 300px;
background-color: gray;
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}
#thumb {
width: 20px;
height: 50px;
background-color: white;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
</style>
</head>
<body>
<div id="container">
<img id="image" src="image1.jpg" width="300" height="200">
</div>
<div id="menu">
<div class="menu-item" onclick="changeText('Option 1')">Option 1</div>
<div class="menu-item" onclick="changeText('Option 2')">Option 2</div>
<div class="menu-item" onclick="changeText('Option 3')">Option 3</div>
</div>
<div id="text">Default Text</div>
<div id="outer">
<div id="inner"></div>
<div id="scrollbar">
<div id="thumb"></div>
</div>
</div>
<script>
// Image manipulation
var image = document.getElementById('image');
image.addEventListener('mouseenter', function() {
this.src = 'image2.jpg';
});
image.addEventListener('mouseleave', function() {
this.src = 'image1.jpg';
});
image.addEventListener('mousemove', function(e) {
this.style.left = e.clientX - this.width / 2 + 'px';
this.style.top = e.clientY - this.height / 2 + 'px';
});
document.addEventListener('keydown', function(e) {
var key = e.key;
switch (key) {
case 'ArrowUp':
image.style.top = parseInt(image.style.top) - 10 + 'px';
break;
case 'ArrowDown':
image.style.top = parseInt(image.style.top) + 10 + 'px';
break;
case 'ArrowLeft':
image.style.left = parseInt(image.style.left) - 10 + 'px';
break;
case 'ArrowRight':
image.style.left = parseInt(image.style.left) + 10 + 'px';
break;
}
});
// Menu manipulation
function changeText(text) {
var textDiv = document.getElementById('text');
textDiv.innerHTML = text;
}
// Div dragging
var outer = document.getElementById('outer');
var inner = document.getElementById('inner');
var thumb = document.getElementById('thumb');
var isDragging = false;
var lastX, lastY;
inner.addEventListener('mousedown', function(e) {
isDragging = true;
lastX = e.clientX;
lastY = e.clientY;
});
document.addEventListener('mousemove', function(e) {
if (isDragging) {
var deltaX = e.clientX - lastX;
var deltaY = e.clientY - lastY;
inner.style.left = parseInt(inner.style.left) + deltaX + 'px';
inner.style.top = parseInt(inner.style.top) + deltaY + 'px';
lastX = e.clientX;
lastY = e.clientY;
var outerRect = outer.getBoundingClientRect();
var innerRect = inner.getBoundingClientRect();
if (innerRect.left < outerRect.left) {
inner.style.left = outerRect.left + 'px';
}
if (innerRect.right > outerRect.right) {
inner.style.left = outerRect.right - inner.offsetWidth + 'px';
}
if (innerRect.top < outerRect.top) {
inner.style.top = outerRect.top + 'px';
}
if (innerRect.bottom > outerRect.bottom) {
inner.style.top = outerRect.bottom - inner.offsetHeight + 'px';
}
}
});
document.addEventListener('mouseup', function() {
isDragging = false;
});
// Scrollbar manipulation
var scrollbar = document.getElementById('scrollbar');
var thumb = document.getElementById('thumb');
var image = document.getElementById('image');
var minHeight = 100;
var maxHeight = 400;
thumb.addEventListener('mousedown', function(e) {
e.preventDefault();
var startY = e.clientY;
var startHeight = thumb.offsetHeight;
document.addEventListener('mousemove', resizeImage);
document.addEventListener('mouseup', removeListeners);
function resizeImage(e) {
var deltaY = e.clientY - startY;
var newHeight = startHeight + deltaY;
if (newHeight < minHeight) {
newHeight = minHeight;
} else if (newHeight > maxHeight) {
newHeight = maxHeight;
}
image.style.height = newHeight + 'px';
var thumbHeight = newHeight / maxHeight * scrollbar.offsetHeight;
thumb.style.height = thumbHeight + 'px';
var thumbTop = newHeight / maxHeight * (scrollbar.offsetHeight - thumbHeight);
thumb.style.top = thumbTop + 'px';
}
function removeListeners() {
document.removeEventListener('mousemove', resizeImage);
document.removeEventListener('mouseup', removeListeners);
}
});
</script>
</body>
</html>
```
阅读全文