国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript使用面向?qū)ο髮崿F(xiàn)的拖拽功能詳解

2024-05-06 15:38:06
字體:
供稿:網(wǎng)友

本文實例講述了JavaScript使用面向?qū)ο髮崿F(xiàn)的拖拽功能。分享給大家供大家參考,具體如下:

面向?qū)ο笥袀€前提:

前提:所有東西都必須包含在onload里 改寫:不能有函數(shù)嵌套,可以有全局變量 過程,如下 onload改成構(gòu)造函數(shù), 全局變量改成屬性(通過this) 函數(shù)改寫成方法
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>面向?qū)ο蟮睦^承-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>面向?qū)ο蟮睦^承-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>

引用塊內(nèi)容

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>面向?qū)ο蟮睦^承-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>            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 乐平市| 濮阳市| 大邑县| 奎屯市| 大田县| 大关县| 利川市| 荃湾区| 明溪县| 宜城市| 玉门市| 保靖县| 富顺县| 西充县| 昌平区| 临城县| 汉寿县| 紫阳县| 麻城市| 十堰市| 二连浩特市| 大兴区| 丰顺县| 望都县| 霍山县| 营山县| 稷山县| 瓮安县| 建湖县| 南宁市| 靖远县| 六安市| 林口县| 广宁县| 邓州市| 嵊州市| 化州市| 怀安县| 泗洪县| 万宁市| 图木舒克市|