当前位置:Gxlcms > JavaScript > JavaScript使用面向对象实现的拖拽功能详解

JavaScript使用面向对象实现的拖拽功能详解

时间:2021-07-01 10:21:17 帮助过:6人阅读

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

面向对象有个前提:

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

把局部变量改成全局变量

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>面向对象的继承-2</title>
  6. <style>
  7. #div1 {width: 100px; height: 100px; background: red; position: absolute;}
  8. </style>
  9. <script>
  10. var oDiv=null;
  11. var disX=0;
  12. var disY=0;
  13. window.onload = function() {
  14. oDiv = document.getElementById('div1');
  15. oDiv.onmousedown = fnDown;
  16. }
  17. function fnMove(ev) {
  18. var ev = ev || event;
  19. oDiv.style.left = ev.clientX - disX + 'px';
  20. oDiv.style.top = ev.clientY - disY + 'px';
  21. }
  22. function fnUp() {
  23. document.onmousemove = document.onmouseup = null;
  24. }
  25. function fnDown(ev) {
  26. var ev = ev || event;
  27. disX = ev.clientX - this.offsetLeft;
  28. disY = ev.clientY - this.offsetTop;
  29. document.onmousemove = fnMove;
  30. document.onmouseup =fnUp;
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <div id="div1"></div>
  36. </body>
  37. </html>

引用块内容

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>面向对象的继承-2</title>
  6. <style>
  7. #div1 {width: 100px; height: 100px; background: red; position: absolute;}
  8. #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
  9. </style>
  10. <script>
  11. window.onload=function(){
  12. new Drag('div1');
  13. new Drag('div2');
  14. }
  15. function Drag(id) {
  16. var _this=this;
  17. this.disX=0;
  18. this.disY=0;
  19. this.oDiv = document.getElementById(id);
  20. this.oDiv.onmousedown = function(){
  21. _this.fnDown()
  22. };
  23. }
  24. Drag.prototype.fnDown=function (ev) {
  25. var ev = ev || event;
  26. var _this=this;
  27. this.disX = ev.clientX - this.oDiv.offsetLeft;
  28. this.disY = ev.clientY - this.oDiv.offsetTop;
  29. document.onmousemove = function(){
  30. _this.fnMove();
  31. };
  32. document.onmouseup =function(){
  33. _this.fnUp();
  34. };
  35. }
  36. Drag.prototype.fnMove=function(ev) {
  37. var ev = ev || event;
  38. this.oDiv.style.left = ev.clientX - this.disX + 'px';
  39. this.oDiv.style.top = ev.clientY - this.disY + 'px';
  40. }
  41. Drag.prototype.fnUp=function () {
  42. document.onmousemove = null;
  43. document.onmouseup = null
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. <div id="div1"></div>
  49. <div id="div2"></div>
  50. </body>
  51. </html>
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>面向对象的继承-2</title>
  6. <style>
  7. #div1 {width: 100px; height: 100px; background: red; position: absolute;}
  8. #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;}
  9. </style>
  10. <script>
  11. window.onload=function(){
  12. new Drag('div1');
  13. new Drag('div2');
  14. }
  15. function Drag(id) {
  16. var _this=this;
  17. this.disX=0;
  18. this.disY=0;
  19. this.oDiv = document.getElementById(id);
  20. this.oDiv.onmousedown = function(){
  21. _this.fnDown()
  22. };
  23. }
  24. Drag.prototype.fnDown=function (ev) {
  25. var ev = ev || event;
  26. var _this=this;
  27. this.disX = ev.clientX - this.oDiv.offsetLeft;
  28. this.disY = ev.clientY - this.oDiv.offsetTop;
  29. document.onmousemove = function(){
  30. _this.fnMove();
  31. };
  32. document.onmouseup =function(){
  33. _this.fnUp();
  34. };
  35. }
  36. Drag.prototype.fnMove=function(ev) {
  37. var ev = ev || event;
  38. this.oDiv.style.left = ev.clientX - this.disX + 'px';
  39. this.oDiv.style.top = ev.clientY - this.disY + 'px';
  40. }
  41. Drag.prototype.fnUp=function () {
  42. document.onmousemove = null;
  43. document.onmouseup = null
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. <div id="div1"></div>
  49. <div id="div2"></div>
  50. </body>
  51. </html>

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

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

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

人气教程排行