找到的html demo,只有pc端
<!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>
</head>
<style>
body,
html {
width: 100%;
height: 100%;
margin: 10;
}
#drag {
width: 200px;
height: 200px;
padding: 15px;
position: absolute;
left: 100px;
top: 100px;
border: #00cdcd 2px solid;
box-sizing: border-box;
}
</style>
<script type="text/javascript">
setTimeout(()=>{
let c = document.getElementById('drag')
console.log(c,document.body);
document.body.addEventListener('mousemove', move)
c.addEventListener('mousedown', down)
document.body.addEventListener('mouseup', up)
let issize = false
let cX, cY
let minW = 8,
minH = 8
let dir = ''
function up() {
issize = false
}
function down(e) {
let d = getDir(e)
console.log('d',d);
if (d !== '') {
issize = true
dir = d
cX = e.clientX
cY = e.clientY
}
}
function move(e) {
let d = getDir(e)
let cursor
if (d === '') cursor = 'default';
else cursor = d + '-resize';
c.style.cursor = cursor;
if (issize) {
if (dir.indexOf('e') !== -1) {
c.style.width = Math.max(minW, c.offsetWidth + (e.clientX - cX)) + 'px'
cX = e.clientX
}
if (dir.indexOf('n') !== -1) {
c.style.height = Math.max(minH, c.offsetHeight + (cY - e.clientY)) + 'px'
cY = e.clientY
}
if (dir.indexOf('s') !== -1) {
c.style.height = Math.max(minH, c.offsetHeight + (e.clientY - cY)) + 'px'
cY = e.clientY
}
if (dir.indexOf('w') !== -1) {
c.style.width = Math.max(minW, c.offsetWidth + (cX - e.clientX)) + 'px'
cX = e.clientX
}
}
}
function getDir(ev) {
let x, y, o, d;
d = '';
x = ev.offsetX;
y = ev.offsetY;
o = 10;
if (y < o) d += 'n';
else if (y > c.offsetHeight - o) d += 's';
if (x < o) d += 'w';
else if (x > c.offsetWidth - o) d += 'e';
return d;
}
},1000)
</script>
<body>
<div id="drag"></div>
</body>
</html>
改造后实现视频窗口上下拖拽的需求
async dragVideo() {
if (!this.isDragEnv) return
document.body.removeEventListener('mousemove', move)
document.body.removeEventListener('mouseup', up)
document.body.removeEventListener('touchmove', move)
document.body.removeEventListener('touchend', up)
await this.sleep(500)
let allowVideoDrag = false
await window.zlCourseService
.getScreenScale({})
.then(res => {
allowVideoDrag = res && res.button
})
.catch(() => {
allowVideoDrag = false
})
this.allowVideoDrag = allowVideoDrag
if (!allowVideoDrag) return
let clientWidth = document.body.clientWidth
let clientHeight = document.body.clientHeight
if (clientWidth < 600) return
let c = document.querySelector('.xxx')
if (!c) return
let video = document.querySelector('.xxx video')
video = video && video.parentElement
let headerBox = document.querySelector('.header-box')
let outContainer = document.querySelector('.out-container')
let footer = document.querySelector('.play-video-bottombox')
video.style.height = video.offsetHeight - 4 + 'px'
document.body.addEventListener('mousemove', move)
document.body.addEventListener('mouseup', up)
document.body.addEventListener('touchmove', move)
document.body.addEventListener('touchend', up)
let issize = false
let cY
let minH = clientHeight * 0.3,
maxH = clientHeight * 0.8
let dir = ''
function up() {
issize = false
}
function down(e) {
let d = getDir(e)
if (d === 's') {
issize = true
dir = d
cY =
e.clientY ||
(e.targetTouches[0] ? e.targetTouches[0].clientY : 0)
}
}
const _this = this
async function move(e) {
clientWidth = document.body.clientWidth
clientHeight = document.body.clientHeight
if (clientWidth < 600) return
c = document.querySelector('.xxx')
if (!c) return
video = document.querySelector('.xxx video')
video = video && video.parentElement
c.removeEventListener('mousedown', down)
c.removeEventListener('touchstart', down)
await _this.sleep(0)
c.addEventListener('mousedown', down)
c.addEventListener('touchstart', down)
let d = getDir(e)
let cursor
if (d === '') cursor = 'default'
else cursor = d + '-resize'
c.style.cursor = cursor
if (_this.dragTimer !== null) {
clearTimeout(_this.dragTimer)
_this.dragTimer = null
}
_this.dragTimer = setTimeout(() => {
let clientY = 0
if (issize && dir.indexOf('s') !== -1) {
if (e.clientY !== undefined) {
clientY = e.clientY
} else if (e.targetTouches[0]) {
clientY = e.targetTouches[0].clientY
}
clientY = clientY || 0
let finallyH = Math.max(minH, c.offsetHeight + (clientY - cY))
finallyH = Math.min(finallyH, maxH)
c.style.height = finallyH + 'px'
cY = clientY
if (video) {
video.style.width = clientWidth + 'px'
video.style.height = finallyH - 4 + 'px'
}
let outContainerH =
clientHeight -
finallyH -
((headerBox && headerBox.offsetHeight) || 0) -
((footer && footer.offsetHeight) || 0)
outContainer.style.height = outContainerH + 'px'
_this.dragHeightInfo = {
finallyH,
clientWidth,
outContainerH
}
}
}, 0)
}
function getDir(ev) {
let y, o, d
d = ''
let offsetY = 0
if (ev.offsetY !== undefined) {
offsetY = ev.offsetY
} else if (ev.touches[0]) {
offsetY = ev.touches[0].clientY
}
y = offsetY || 0
o = 10
if (y < o) d += 'n'
else if (y > c.offsetHeight - o) d += 's'
return d
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)