HTML页面中有左右两个div,分别实现滚动条效果
时间: 2024-03-17 20:39:37 浏览: 144
可以使用CSS的overflow属性来实现滚动条效果。具体实现方法如下:
HTML代码:
```html
<div class="container">
<div class="left">左边的内容</div>
<div class="right">右边的内容</div>
</div>
```
CSS代码:
```css
.container {
display: flex;
}
.left {
overflow-y: scroll; /* 竖直方向滚动条 */
width: 50%;
}
.right {
overflow-y: scroll; /* 竖直方向滚动条 */
width: 50%;
}
```
这样,左右两个div就会出现滚动条,当内容超出div的高度时就可以用滚动条滚动查看。
相关问题
HTML页面中有左右两个div,左边div实现滚动效果
要实现左边div的滚动效果,可以使用CSS属性overflow: scroll或overflow: auto来设置左边div的滚动条。具体实现步骤如下:
1.在HTML文件中添加两个div:
```html
<div class="left"></div>
<div class="right"></div>
```
2.在CSS文件中设置左右两个div的样式,并为左边div设置滚动条:
```css
.left {
width: 50%;
height: 500px;
overflow: scroll; /* 或者使用overflow: auto; */
}
.right {
width: 50%;
height: 500px;
}
```
3.在左边div中添加内容,超出div高度时就会出现滚动条了:
```html
<div class="left">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam condimentum, turpis quis suscipit lobortis, odio metus eleifend velit, eget laoreet nulla velit ac nunc. Aliquam non enim lectus. Donec euismod libero id arcu bibendum, eget bibendum ipsum varius. Sed vulputate, nibh non varius mollis, quam dolor maximus diam, non lobortis neque magna ut sem. Etiam in arcu vitae lorem convallis tristique. Etiam aliquet, sem a accumsan iaculis, neque risus tincidunt ipsum, sit amet posuere enim nulla auctor libero. Donec tempus consectetur ante, vel fringilla sapien. Curabitur eget enim nec neque ultrices eleifend sed vel tellus. Ut at elit vel tortor accumsan bibendum.</p>
... (此处省略部分内容)
</div>
<div class="right"></div>
```
这样,左边的div就会出现滚动条,右边的div则不会。
满足1.对于页面上的一幅图片,鼠标悬停在图片上时切换成另一幅图,鼠标离开时,还原成原来的图片 2. 实现在页面上的一幅图片,能够跟随鼠标移动。 3. 实现在页面上的一幅图片,可以通过上下左右键控制图片在页面上移动。 4.在页面上建立一个自定义菜单,菜单内容如图所示,当单击菜单选项时,可以改变页面上文字相应设置。 5.在页面上有两个div块,实现内部div块可以在其外部容器div块中随意拖动,并实现磁吸效果。 6.页面上有一张图片和一个自定义滚动条,实现如下效果:拖动滚动条,可以改变图片大小。的html文件
这里是一个简单的 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>
```
阅读全文