麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

JavaScript使用面向對象實現的拖拽功能詳解

2019-11-19 11:21:20
字體:
來源:轉載
供稿:網友

本文實例講述了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.VeVB.COm/code/HtmlJsRun測試一下運行效果。

更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript頁面元素操作技巧總結》、《JavaScript操作DOM技巧總結》、《JavaScript切換特效與技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》及《JavaScript數學運算用法總結

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91精品国产综合久久久欧美 | 欧美成人一区二区三区 | 国产精选久久 | 久久精品一区二区三区不卡牛牛 | 午夜视频免费在线观看 | 一区二区三区无码高清视频 | 久久精品一区二区三区不卡牛牛 | 色淫网站免费视频 | 欧美亚洲综合网 | 欧美日韩高清不卡 | 玖玖视频精品 | av手机在线电影 | 亚洲国产成人久久成人52 | h色网站免费观看 | 午夜视频在线观看免费视频 | 国产精品久久久久久久久久大牛 | 黄色va视频| 欧美日韩亚洲一区二区三区 | 久久久无码精品亚洲日韩按摩 | 久久精品观看 | 国产精品a一 | 国内精品久久久久久久久久 | 中文字幕伦乱 | 午夜视频在线免费播放 | 天海翼无删减av三级在线观看 | 99在线在线视频免费视频观看 | 久久久一区二区三区精品 | 亚洲小视频在线 | 小视频在线看 | 欧美综合在线观看 | 午夜精品网| 国产精品视频专区 | 小情侣嗯啊哦视频www | 黄污视频在线看 | 4p嗯啊巨肉寝室调教男男视频 | 91精品观看91久久久久久国产 | 欧美一级黄色录像片 | 国产一级aa大片毛片 | 午夜国产在线观看 | 欧美精品18 | 黄色aaa视频 |