本文实例讲述了JavaScript使用面向对象实现的拖拽功能。分享给大家供大家参考,具体如下:

面向对象有个前提:

  • 前提:所有东西都必须包含在onload里
  • 改写:不能有函数嵌套,可以有全局变量
  • 过程,如下
    • onload改成构造函数,
    • 全局变量改成属性(通过this)
    • 函数改写成方法
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向对象的继承-1</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
window.onload = function() {
  var oDiv = document.getElementById('div1');
  oDiv.onmousedown = function(ev) {
    var ev = ev || event;
    var disX = ev.clientX - this.offsetLeft;
    var disY = ev.clientY - this.offsetTop;
    document.onmousemove = function(ev) {
      var ev = ev || event;
      oDiv.style.left = ev.clientX - disX + 'px';
      oDiv.style.top = ev.clientY - disY + 'px';
    }
    document.onmouseup = function() {
      document.onmousemove = document.onmouseup = null;
    }
  }
}
</script>
</head>
<body>
  <div id="div1"></div>
</body>
</html>

把局部变量改成全局变量

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向对象的继承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
</style>
<script>
var oDiv=null;
var disX=0;
var disY=0;
window.onload = function() {
  oDiv = document.getElementById('div1');
  oDiv.onmousedown = fnDown;
}
function fnMove(ev) {
  var ev = ev || event;
  oDiv.style.left = ev.clientX - disX + 'px';
  oDiv.style.top = ev.clientY - disY + 'px';
}
function fnUp() {
  document.onmousemove = document.onmouseup = null;
}
function fnDown(ev) {
  var ev = ev || event;
  disX = ev.clientX - this.offsetLeft;
  disY = ev.clientY - this.offsetTop;
  document.onmousemove = fnMove;
  document.onmouseup =fnUp;
}
</script>
</head>
<body>
  <div id="div1"></div>
</body>
</html>

引用块内容

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向对象的继承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
#div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
</style>
<script>
window.onload=function(){
  new Drag('div1');
  new Drag('div2');
}
function Drag(id) {
  var _this=this;
  this.disX=0;
  this.disY=0;
  this.oDiv = document.getElementById(id);
  this.oDiv.onmousedown = function(){
    _this.fnDown()
  };
}
Drag.prototype.fnDown=function (ev) {
  var ev = ev || event;
  var _this=this;
  this.disX = ev.clientX - this.oDiv.offsetLeft;
  this.disY = ev.clientY - this.oDiv.offsetTop;
  document.onmousemove = function(){
    _this.fnMove();
  };
  document.onmouseup =function(){
    _this.fnUp();
  };
}
Drag.prototype.fnMove=function(ev) {
  var ev = ev || event;
  this.oDiv.style.left = ev.clientX - this.disX + 'px';
  this.oDiv.style.top = ev.clientY - this.disY + 'px';
}
Drag.prototype.fnUp=function () {
  document.onmousemove = null;
  document.onmouseup = null
}
</script>
</head>
<body>
  <div id="div1"></div>
  <div id="div2"></div>
</body>
</html>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>面向对象的继承-2</title>
<style>
#div1 {width: 100px; height: 100px; background: red; position: absolute;}
#div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
</style>
<script>
window.onload=function(){
  new Drag('div1');
  new Drag('div2');
}
function Drag(id) {
  var _this=this;
  this.disX=0;
  this.disY=0;
  this.oDiv = document.getElementById(id);
  this.oDiv.onmousedown = function(){
    _this.fnDown()
  };
}
Drag.prototype.fnDown=function (ev) {
  var ev = ev || event;
  var _this=this;
  this.disX = ev.clientX - this.oDiv.offsetLeft;
  this.disY = ev.clientY - this.oDiv.offsetTop;
  document.onmousemove = function(){
    _this.fnMove();
  };
  document.onmouseup =function(){
    _this.fnUp();
  };
}
Drag.prototype.fnMove=function(ev) {
  var ev = ev || event;
  this.oDiv.style.left = ev.clientX - this.disX + 'px';
  this.oDiv.style.top = ev.clientY - this.disY + 'px';
}
Drag.prototype.fnUp=function () {
  document.onmousemove = null;
  document.onmouseup = null
}
</script>
</head>
<body>
  <div id="div1"></div>
  <div id="div2"></div>
</body>
</html>

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试一下运行效果。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript页面元素操作技巧总结》、《JavaScript操作DOM技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

标签:
JavaScript,面向对象,拖拽

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。