首页 >> js开发 >> JavaScriptjs实现限定范围拖拽的示例
JavaScriptjs实现限定范围拖拽的示例
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
限定范围拖拽限定范围拖拽目录
代码实例
与简易拖拽的差异
下载源码链接
代码实例与简易拖拽的差异下载源码链接
代码实例
* {
padding: 0;
margin: 0;
}
#box1 {
width: 500px;
height: 500px;
background: #999;
position: relative;
left: 100px;
top: 100px;
}
#box {
width: 100px;
height: 100px;
background: #334;
position: absolute;
cursor: move;
}
(function () {
var dragging = false
var boxX, boxY, mouseX, mouseY, offsetX, offsetY
var box = document.getElementById('box')
var box1 = document.getElementById('box1')
// 鼠标按下的动作
box.onmousedown = down
// 鼠标的移动动作
document.onmousemove = move
// 释放鼠标的动作
document.onmouseup = up
// 鼠标按下后的函数,e为事件对象
function down(e) {
dragging = true
// 获取元素所在的坐标
boxX = box.offsetLeft
boxY = box.offsetTop
// 获取鼠标所在的坐标
mouseX = parseInt(getMouseXY(e).x)
mouseY = parseInt(getMouseXY(e).y)
// 鼠标相对元素左和上边缘的坐标
offsetX = mouseX - boxX
offsetY = mouseY - boxY
}
// 鼠标移动调用的函数
function move(e){
if (dragging) {
// 获取移动后的元素的坐标
var x = getMouseXY(e).x - offsetX
var y = getMouseXY(e).y - offsetY
// 计算可移动位置的大小, 保证元素不会超过可移动范围
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// min方法保证不会超过右边界,max保证不会超过左边界
x = Math.min(Math.max(0, x), width)
y = Math.min(Math.max(0, y), height)
// 给元素及时定位
box.style.left = x + 'px'
box.style.top = y + 'px'
}
}
// 释放鼠标的函数
function up(e){
dragging = false
}
// 函数用于获取鼠标的位置
function getMouseXY(e){
var x = 0, y = 0
e = e || window.event
if (e.pageX) {
x = e.pageX
y = e.pageY
} else {
x = e.clientX + document.body.scrollLeft - document.body.clientLeft
y = e.clientY + document.body.scrollTop - document.body.clientTop
}
return {
x: x,
y: y
}
}
})()
* {
padding: 0;
margin: 0;
}
#box1 {
width: 500px;
height: 500px;
background: #999;
position: relative;
left: 100px;
top: 100px;
}
#box {
width: 100px;
height: 100px;
background: #334;
position: absolute;
cursor: move;
}
(function () {
var dragging = false
var boxX, boxY, mouseX, mouseY, offsetX, offsetY
var box = document.getElementById('box')
var box1 = document.getElementById('box1')
// 鼠标按下的动作
box.onmousedown = down
// 鼠标的移动动作
document.onmousemove = move
// 释放鼠标的动作
document.onmouseup = up
// 鼠标按下后的函数,e为事件对象
function down(e) {
dragging = true
// 获取元素所在的坐标
boxX = box.offsetLeft
boxY = box.offsetTop
// 获取鼠标所在的坐标
mouseX = parseInt(getMouseXY(e).x)
mouseY = parseInt(getMouseXY(e).y)
// 鼠标相对元素左和上边缘的坐标
offsetX = mouseX - boxX
offsetY = mouseY - boxY
}
// 鼠标移动调用的函数
function move(e){
if (dragging) {
// 获取移动后的元素的坐标
var x = getMouseXY(e).x - offsetX
var y = getMouseXY(e).y - offsetY
// 计算可移动位置的大小, 保证元素不会超过可移动范围
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// min方法保证不会超过右边界,max保证不会超过左边界
x = Math.min(Math.max(0, x), width)
y = Math.min(Math.max(0, y), height)
// 给元素及时定位
box.style.left = x + 'px'
box.style.top = y + 'px'
}
}
// 释放鼠标的函数
function up(e){
dragging = false
}
// 函数用于获取鼠标的位置
function getMouseXY(e){
var x = 0, y = 0
e = e || window.event
if (e.pageX) {
x = e.pageX
y = e.pageY
} else {
x = e.clientX + document.body.scrollLeft - document.body.clientLeft
y = e.clientY + document.body.scrollTop - document.body.clientTop
}
return {
x: x,
y: y
}
}
})()与简易拖拽的差异与简易拖拽的差异简易拖拽的链接简易拖拽的链接可移动位置的改变可移动位置的改变
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight下载源码链接下载源码链接星辉的Github 星辉的Github 以上就是js实现限定范围拖拽的示例的详细内容,关于js实现限定范围拖拽的资料请关注其它相关文章!
代码实例
与简易拖拽的差异
下载源码链接
代码实例与简易拖拽的差异下载源码链接
代码实例
* {
padding: 0;
margin: 0;
}
#box1 {
width: 500px;
height: 500px;
background: #999;
position: relative;
left: 100px;
top: 100px;
}
#box {
width: 100px;
height: 100px;
background: #334;
position: absolute;
cursor: move;
}
(function () {
var dragging = false
var boxX, boxY, mouseX, mouseY, offsetX, offsetY
var box = document.getElementById('box')
var box1 = document.getElementById('box1')
// 鼠标按下的动作
box.onmousedown = down
// 鼠标的移动动作
document.onmousemove = move
// 释放鼠标的动作
document.onmouseup = up
// 鼠标按下后的函数,e为事件对象
function down(e) {
dragging = true
// 获取元素所在的坐标
boxX = box.offsetLeft
boxY = box.offsetTop
// 获取鼠标所在的坐标
mouseX = parseInt(getMouseXY(e).x)
mouseY = parseInt(getMouseXY(e).y)
// 鼠标相对元素左和上边缘的坐标
offsetX = mouseX - boxX
offsetY = mouseY - boxY
}
// 鼠标移动调用的函数
function move(e){
if (dragging) {
// 获取移动后的元素的坐标
var x = getMouseXY(e).x - offsetX
var y = getMouseXY(e).y - offsetY
// 计算可移动位置的大小, 保证元素不会超过可移动范围
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// min方法保证不会超过右边界,max保证不会超过左边界
x = Math.min(Math.max(0, x), width)
y = Math.min(Math.max(0, y), height)
// 给元素及时定位
box.style.left = x + 'px'
box.style.top = y + 'px'
}
}
// 释放鼠标的函数
function up(e){
dragging = false
}
// 函数用于获取鼠标的位置
function getMouseXY(e){
var x = 0, y = 0
e = e || window.event
if (e.pageX) {
x = e.pageX
y = e.pageY
} else {
x = e.clientX + document.body.scrollLeft - document.body.clientLeft
y = e.clientY + document.body.scrollTop - document.body.clientTop
}
return {
x: x,
y: y
}
}
})()
* {
padding: 0;
margin: 0;
}
#box1 {
width: 500px;
height: 500px;
background: #999;
position: relative;
left: 100px;
top: 100px;
}
#box {
width: 100px;
height: 100px;
background: #334;
position: absolute;
cursor: move;
}
(function () {
var dragging = false
var boxX, boxY, mouseX, mouseY, offsetX, offsetY
var box = document.getElementById('box')
var box1 = document.getElementById('box1')
// 鼠标按下的动作
box.onmousedown = down
// 鼠标的移动动作
document.onmousemove = move
// 释放鼠标的动作
document.onmouseup = up
// 鼠标按下后的函数,e为事件对象
function down(e) {
dragging = true
// 获取元素所在的坐标
boxX = box.offsetLeft
boxY = box.offsetTop
// 获取鼠标所在的坐标
mouseX = parseInt(getMouseXY(e).x)
mouseY = parseInt(getMouseXY(e).y)
// 鼠标相对元素左和上边缘的坐标
offsetX = mouseX - boxX
offsetY = mouseY - boxY
}
// 鼠标移动调用的函数
function move(e){
if (dragging) {
// 获取移动后的元素的坐标
var x = getMouseXY(e).x - offsetX
var y = getMouseXY(e).y - offsetY
// 计算可移动位置的大小, 保证元素不会超过可移动范围
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// min方法保证不会超过右边界,max保证不会超过左边界
x = Math.min(Math.max(0, x), width)
y = Math.min(Math.max(0, y), height)
// 给元素及时定位
box.style.left = x + 'px'
box.style.top = y + 'px'
}
}
// 释放鼠标的函数
function up(e){
dragging = false
}
// 函数用于获取鼠标的位置
function getMouseXY(e){
var x = 0, y = 0
e = e || window.event
if (e.pageX) {
x = e.pageX
y = e.pageY
} else {
x = e.clientX + document.body.scrollLeft - document.body.clientLeft
y = e.clientY + document.body.scrollTop - document.body.clientTop
}
return {
x: x,
y: y
}
}
})()与简易拖拽的差异与简易拖拽的差异简易拖拽的链接简易拖拽的链接可移动位置的改变可移动位置的改变
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight
// 此处就是父元素的宽度减去子元素宽度
var width = box1.clientWidth - box.offsetWidth
var height = box1.clientHeight - box.offsetHeight下载源码链接下载源码链接星辉的Github 星辉的Github 以上就是js实现限定范围拖拽的示例的详细内容,关于js实现限定范围拖拽的资料请关注其它相关文章!
相关文章:
- js如何在Express4.x中愉快地使用async的方法js大全
- js微信小程序实现多行文字滚动js大全
- js解决vue项目中出现Invalid Host header的问题js大全
- jsSpringBoot在yml配置文件中配置druid的操作js大全
- js快速解决vue2+vue-cli3项目ie兼容的问题js大全
- jsantd table按表格里的日期去排序操作js大全
- jsvue 表单输入框不支持focus及blur事件的解决方案js大全
- js小程序自定义圆形进度条js大全
- jsant design pro中可控的筛选和排序实例js大全
- jsAntd-vue Table组件添加Click事件,实现点击某行数据教程js大全