写一个前端的代码,可以用鼠标点击后滑动的窗口
时间: 2024-02-19 11:58:15 浏览: 54
鼠标点击滑动门代码
5星 · 资源好评率100%
好的,以下是一个简单的前端代码,实现了点击后可以拖动滑动窗口的效果:
HTML部分:
```html
<div class="window">
<div class="title">窗口标题</div>
<div class="content">窗口内容</div>
</div>
```
CSS部分:
```css
.window {
position: absolute;
top: 50px;
left: 50px;
width: 300px;
height: 200px;
border: 1px solid #333;
background-color: #fff;
}
.title {
height: 30px;
line-height: 30px;
padding: 0 10px;
background-color: #333;
color: #fff;
cursor: move;
}
.content {
height: 170px;
padding: 10px;
overflow-y: auto;
}
```
JavaScript部分:
```javascript
var windowEl = document.querySelector('.window');
var titleEl = windowEl.querySelector('.title');
var isDragging = false;
var mouseX = 0;
var mouseY = 0;
var windowX = 0;
var windowY = 0;
titleEl.addEventListener('mousedown', function (event) {
isDragging = true;
mouseX = event.clientX;
mouseY = event.clientY;
windowX = parseInt(window.getComputedStyle(windowEl).left);
windowY = parseInt(window.getComputedStyle(windowEl).top);
});
document.addEventListener('mousemove', function (event) {
if (isDragging) {
var deltaX = event.clientX - mouseX;
var deltaY = event.clientY - mouseY;
windowEl.style.left = (windowX + deltaX) + 'px';
windowEl.style.top = (windowY + deltaY) + 'px';
}
});
document.addEventListener('mouseup', function (event) {
isDragging = false;
});
```
这个代码实现了一个简单的可拖动的窗口效果,点击标题后可以拖动整个窗口。
阅读全文