disx = oevent.clientX - box.offsetLeft;
box.style.left = oevent.clientX - disx + "px";
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>无标题文档</title>
<style>
body{margin:0; padding:0; height:1500px;}
#box{width:100px; height:100px; background:#06c; position:absolute;}
</style>
<script>
window.onload = function(){
var box = document.getElementById("box");
var disx =0;
var disy = 0;
box.onmousedown = function(evt){
var oevent = evt || window.event;
disx = oevent.clientX - box.offsetLeft;
disy = oevent.clientY - box.offsetTop;
document.onmousemove = function(evt){
var oevent = evt || window.event;
box.style.left = oevent.clientX - disx + "px";
box.style.top = oevent.clientY - disy + "px";
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
return false;
}
}
</script>
</head>
<body>
<h1>鼠标拖动</h1>
<div id="box"></div>
</body>
</html>
var boxl = oevent.clientX - disx;
if(boxl < 0){
boxl =0;
}else if(boxl > vieww - box.offsetWidth){
boxl = vieww - box.offsetWidth;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>无标题文档</title>
<style>
body{margin:0; padding:0;}
#box{width:100px;
height:100px;
background:#06c;
position:absolute;
}
</style>
<script>
window.onload = function(){
var box = document.getElementById("box");
var disx =0;
var disy = 0;
box.onmousedown = function(evt){
var oevent = evt || window.event;
disx = oevent.clientX - box.offsetLeft;
disy = oevent.clientY - box.offsetTop;
document.onmousemove = function(evt){
var oevent = evt || window.event;
var boxl = oevent.clientX - disx;
var boxt = oevent.clientY - disy
var vieww = document.documentElement.clientWidth || document.body.clientWidth;
var viewh = document.documentElement.clientHeight || document.body.clientHeight;
if(boxl < 0){
boxl =0;
}else if(boxl > vieww - box.offsetWidth){
boxl = vieww - box.offsetWidth;
}
if(boxt < 0){
boxt =0;
}else if(boxt > viewh - box.offsetHeight){
boxt = viewh- box.offsetHeight;
}
box.style.left = boxl + "px";
box.style.top = boxt + "px";
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
return false;
}
}
</script>
</head>
<body>
<h1>鼠标拖动</h1>
<div id="box"></div>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有