鱼C论坛

 找回密码
 立即注册
查看: 2776|回复: 2

[已解决]初学前端js 这两段代码有些疑问 大家帮我看下

[复制链接]
发表于 2020-10-6 23:42:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
两段代码一样,第一段代码除了最外层用的是获取的title元素设置监听,里面的都是用document来设置监听
第二段代码则全是用获取的title元素来设置监听 运行的效果是一样的 大佬们帮我看下 他们有什么区别吗

title.addEventListener('mousedown', function(e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            document.addEventListener('mousemove', move)

            function move(e) {
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
            }
            document.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move)
            })
        })


title.addEventListener('mousedown', function(e) {
                var x = e.pageX - login_box.offsetLeft;
                var y = e.pageY - login_box.offsetTop;

                title.addEventListener('mousemove', move)

                function move(e) {
                    login_box.style.left = e.pageX - x + 'px';
                    login_box.style.top = e.pageY - y + 'px';
                }
                title.addEventListener('mouseup', function() {
                    title.removeEventListener('mousemove', move)
                })
            })
最佳答案
2020-10-7 01:49:25
本帖最后由 kogawananari 于 2020-10-7 01:56 编辑

title属于document
title的mouseup触发后 document的mouseup也会触发
这个叫做冒泡
在你这个拖拽的特例中 事件的响应可能跟不上鼠标 鼠标甩到title之外的时候可能才判定鼠标抬起 所以mouseup事件绑定在document上更好
再提醒一下mousemove触发后的几毫秒内默认会不让mouseup触发 导致鼠标抬起后依然在拖拽的情况  所以这个事件你需要return false来取消默认行为

难一点的例子我发一个
  1. <style>
  2.     #imgbox {
  3.         position: relative;
  4.         width: 500px;
  5.         height: 500px;
  6.         margin: 50px auto;
  7.         background-color: palevioletred;
  8.     }

  9.     #imgsorech {
  10.         position: absolute;
  11.         width: 200px;
  12.         height: 200px;
  13.         border: 1px dashed white;
  14.         background-color: transparent;
  15.     }

  16.     .muki {
  17.         position: absolute;
  18.         width: 5px;
  19.         height: 5px;
  20.         border-radius: 50%;
  21.         background-color: white;
  22.     }

  23.     .muki:hover {
  24.         cursor: move;
  25.     }

  26.     #migishita {
  27.         right: -3px;
  28.         bottom: -3px;
  29.     }

  30.     #hibarikami {
  31.         top: -3px;
  32.         left: -3px;
  33.     }

  34.     #migikami {
  35.         right: -3px;
  36.         top: -3px;
  37.     }

  38.     #hibarishita {
  39.         left: -3px;
  40.         bottom: -3px;
  41.     }

  42.     #ennSeiyaku {
  43.         box-sizing: border-box;
  44.         height: 100%;
  45.         width: 100%;
  46.         border: 1px solid white;
  47.         border-radius: 50%;
  48.     }
  49. </style>
  50. <script>
  51.     window.onload = function () {
  52.         let minX = 0
  53.         let minY = 0
  54.         let minwidth = 50
  55.         let minheight = 50

  56.         imgsorech.onmousedown = function (eve) {  //拖动
  57.             eve = eve || window.event
  58.             let imaX = eve.clientX
  59.             let imaY = eve.clientY
  60.             let imawidth = imgsorech.offsetWidth
  61.             let imaheight = imgsorech.offsetHeight
  62.             let schX = imgsorech.offsetLeft
  63.             let schY = imgsorech.offsetTop
  64.             let maxX = imgbox.clientWidth + minX - imawidth
  65.             let maxY = imgbox.clientHeight + minY - imaheight
  66.             imgsorech.style.bottom = ''
  67.             imgsorech.style.right = ''
  68.             imgsorech.style.left = schX + 'px'
  69.             imgsorech.style.top = schY + 'px'
  70.             document.onmousemove = function (eve) {
  71.                 eve = eve || window.event
  72.                 let miraiX = eve.clientX
  73.                 let miraiY = eve.clientY
  74.                 let schleft = schX + (miraiX - imaX)
  75.                 let schtop = schY + (miraiY - imaY)
  76.                 schleft = Math.max(schleft, minX)
  77.                 schleft = Math.min(schleft, maxX)
  78.                 schtop = Math.max(schtop, minY)
  79.                 schtop = Math.min(schtop, maxY)
  80.                 imgsorech.style.left = schleft + 'px'
  81.                 imgsorech.style.top = schtop + 'px'
  82.                 return false
  83.             }
  84.         }

  85.         document.onmouseup = function () {
  86.             document.onmousemove = null
  87.         }

  88.         migishita.onmousedown = function (eve) {  //右下角
  89.             eve = eve || window.event
  90.             if (eve.stopPropagation) {
  91.                 eve.stopPropagation()
  92.             } else {
  93.                 eve.cancelBubble = true
  94.             }
  95.             let imaX = eve.clientX
  96.             let imaY = eve.clientY
  97.             let imawidth = imgsorech.offsetWidth
  98.             let imaheight = imgsorech.offsetHeight
  99.             let schX = imgsorech.offsetLeft
  100.             let schY = imgsorech.offsetTop
  101.             let maxwidth = imgbox.clientWidth - schX - 2
  102.             let maxheight = imgbox.clientHeight - schY - 2
  103.             imgsorech.style.bottom = ''
  104.             imgsorech.style.right = ''
  105.             imgsorech.style.left = schX + 'px'
  106.             imgsorech.style.top = schY + 'px'
  107.             document.onmousemove = function (eve) {
  108.                 eve = eve || window.event
  109.                 let miraiX = eve.clientX
  110.                 let miraiY = eve.clientY
  111.                 let schwidth = imawidth + (miraiX - imaX)
  112.                 let schheight = imaheight + (miraiY - imaY)
  113.                 schwidth = Math.min(schwidth, maxwidth)
  114.                 schwidth = Math.max(schwidth, minwidth)
  115.                 schheight = Math.min(schheight, maxheight)
  116.                 schheight = Math.max(schheight, minheight)
  117.                 let seihoukeiSize = Math.min(schwidth, schheight)
  118.                 imgsorech.style.width = seihoukeiSize + 'px'
  119.                 imgsorech.style.height = seihoukeiSize + 'px'
  120.                 return false
  121.             }
  122.         }

  123.         hibarikami.onmousedown = function (eve) {  //左上角
  124.             eve = eve || window.event
  125.             if (eve.stopPropagation) {
  126.                 eve.stopPropagation()
  127.             } else {
  128.                 eve.cancelBubble = true
  129.             }
  130.             let imaX = eve.clientX
  131.             let imaY = eve.clientY
  132.             let imawidth = imgsorech.offsetWidth
  133.             let imaheight = imgsorech.offsetHeight
  134.             let schX = imgbox.clientWidth - imawidth - imgsorech.offsetLeft
  135.             let schY = imgbox.clientHeight - imaheight - imgsorech.offsetTop
  136.             let maxwidth = imgbox.clientWidth - schX - 2
  137.             let maxheight = imgbox.clientHeight - schY - 2
  138.             imgsorech.style.left = ''
  139.             imgsorech.style.top = ''
  140.             imgsorech.style.right = schX + 'px'
  141.             imgsorech.style.bottom = schY + 'px'
  142.             document.onmousemove = function (eve) {
  143.                 let miraiX = eve.clientX
  144.                 let miraiY = eve.clientY
  145.                 let schwidth = imawidth - (miraiX - imaX)
  146.                 let schheight = imaheight - (miraiY - imaY)
  147.                 schwidth = Math.min(schwidth, maxwidth)
  148.                 schwidth = Math.max(schwidth, minwidth)
  149.                 schheight = Math.min(schheight, maxheight)
  150.                 schheight = Math.max(schheight, minheight)
  151.                 let seihoukeiSize = Math.min(schwidth, schheight)
  152.                 imgsorech.style.width = seihoukeiSize + 'px'
  153.                 imgsorech.style.height = seihoukeiSize + 'px'
  154.                 return false
  155.             }
  156.         }

  157.         migikami.onmousedown = function (eve) { //右上角
  158.             eve = eve || window.event
  159.             if (eve.stopPropagation) {
  160.                 eve.stopPropagation()
  161.             } else {
  162.                 eve.cancelBubble = true
  163.             }
  164.             let imaX = eve.clientX
  165.             let imaY = eve.clientY
  166.             let imawidth = imgsorech.offsetWidth
  167.             let imaheight = imgsorech.offsetHeight
  168.             let schX = imgsorech.offsetLeft
  169.             let schY = imgbox.clientHeight - imaheight - imgsorech.offsetTop
  170.             let maxwidth = imgbox.clientWidth - schX - 2
  171.             let maxheight = imgbox.clientHeight - schY - 2
  172.             imgsorech.style.right = ''
  173.             imgsorech.style.top = ''
  174.             imgsorech.style.left = schX + 'px'
  175.             imgsorech.style.bottom = schY + 'px'
  176.             document.onmousemove = function (eve) {
  177.                 let miraiX = eve.clientX
  178.                 let miraiY = eve.clientY
  179.                 let schwidth = imawidth + (miraiX - imaX)
  180.                 let schheight = imaheight - (miraiY - imaY)
  181.                 schwidth = Math.min(schwidth, maxwidth)
  182.                 schwidth = Math.max(schwidth, minwidth)
  183.                 schheight = Math.min(schheight, maxheight)
  184.                 schheight = Math.max(schheight, minheight)
  185.                 let seihoukeiSize = Math.min(schwidth, schheight)
  186.                 imgsorech.style.width = seihoukeiSize + 'px'
  187.                 imgsorech.style.height = seihoukeiSize + 'px'
  188.                 return false
  189.             }
  190.         }

  191.         hibarishita.onmousedown = function (eve) { //左下角
  192.             eve = eve || window.event
  193.             if (eve.stopPropagation) {
  194.                 eve.stopPropagation()
  195.             } else {
  196.                 eve.cancelBubble = true
  197.             }
  198.             let imaX = eve.clientX
  199.             let imaY = eve.clientY
  200.             let imawidth = imgsorech.offsetWidth
  201.             let imaheight = imgsorech.offsetHeight
  202.             let schX = imgbox.clientWidth - imawidth - imgsorech.offsetLeft
  203.             let schY = imgsorech.offsetTop
  204.             let maxwidth = imgbox.clientWidth - schX - 2
  205.             let maxheight = imgbox.clientHeight - schY - 2
  206.             imgsorech.style.left = ''
  207.             imgsorech.style.bottom = ''
  208.             imgsorech.style.right = schX + 'px'
  209.             imgsorech.style.top = schY + 'px'
  210.             document.onmousemove = function (eve) {
  211.                 let miraiX = eve.clientX
  212.                 let miraiY = eve.clientY
  213.                 let schwidth = imawidth - (miraiX - imaX)
  214.                 let schheight = imaheight + (miraiY - imaY)
  215.                 schwidth = Math.min(schwidth, maxwidth)
  216.                 schwidth = Math.max(schwidth, minwidth)
  217.                 schheight = Math.min(schheight, maxheight)
  218.                 schheight = Math.max(schheight, minheight)
  219.                 let seihoukeiSize = Math.min(schwidth, schheight)
  220.                 imgsorech.style.width = seihoukeiSize + 'px'
  221.                 imgsorech.style.height = seihoukeiSize + 'px'
  222.                 return false
  223.             }
  224.         }
  225.     }
  226. </script>
  227. <div id="imgbox">
  228.     <div id="imgsorech">
  229.         <div id="ennSeiyaku">
  230.         </div>
  231.         <div id="migishita" class="muki">
  232.         </div>
  233.         <div id="hibarikami" class="muki">
  234.         </div>
  235.         <div id="migikami" class="muki">
  236.         </div>
  237.         <div id="hibarishita" class="muki">
  238.         </div>
  239.     </div>
  240. </div>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-7 01:49:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 kogawananari 于 2020-10-7 01:56 编辑

title属于document
title的mouseup触发后 document的mouseup也会触发
这个叫做冒泡
在你这个拖拽的特例中 事件的响应可能跟不上鼠标 鼠标甩到title之外的时候可能才判定鼠标抬起 所以mouseup事件绑定在document上更好
再提醒一下mousemove触发后的几毫秒内默认会不让mouseup触发 导致鼠标抬起后依然在拖拽的情况  所以这个事件你需要return false来取消默认行为

难一点的例子我发一个
  1. <style>
  2.     #imgbox {
  3.         position: relative;
  4.         width: 500px;
  5.         height: 500px;
  6.         margin: 50px auto;
  7.         background-color: palevioletred;
  8.     }

  9.     #imgsorech {
  10.         position: absolute;
  11.         width: 200px;
  12.         height: 200px;
  13.         border: 1px dashed white;
  14.         background-color: transparent;
  15.     }

  16.     .muki {
  17.         position: absolute;
  18.         width: 5px;
  19.         height: 5px;
  20.         border-radius: 50%;
  21.         background-color: white;
  22.     }

  23.     .muki:hover {
  24.         cursor: move;
  25.     }

  26.     #migishita {
  27.         right: -3px;
  28.         bottom: -3px;
  29.     }

  30.     #hibarikami {
  31.         top: -3px;
  32.         left: -3px;
  33.     }

  34.     #migikami {
  35.         right: -3px;
  36.         top: -3px;
  37.     }

  38.     #hibarishita {
  39.         left: -3px;
  40.         bottom: -3px;
  41.     }

  42.     #ennSeiyaku {
  43.         box-sizing: border-box;
  44.         height: 100%;
  45.         width: 100%;
  46.         border: 1px solid white;
  47.         border-radius: 50%;
  48.     }
  49. </style>
  50. <script>
  51.     window.onload = function () {
  52.         let minX = 0
  53.         let minY = 0
  54.         let minwidth = 50
  55.         let minheight = 50

  56.         imgsorech.onmousedown = function (eve) {  //拖动
  57.             eve = eve || window.event
  58.             let imaX = eve.clientX
  59.             let imaY = eve.clientY
  60.             let imawidth = imgsorech.offsetWidth
  61.             let imaheight = imgsorech.offsetHeight
  62.             let schX = imgsorech.offsetLeft
  63.             let schY = imgsorech.offsetTop
  64.             let maxX = imgbox.clientWidth + minX - imawidth
  65.             let maxY = imgbox.clientHeight + minY - imaheight
  66.             imgsorech.style.bottom = ''
  67.             imgsorech.style.right = ''
  68.             imgsorech.style.left = schX + 'px'
  69.             imgsorech.style.top = schY + 'px'
  70.             document.onmousemove = function (eve) {
  71.                 eve = eve || window.event
  72.                 let miraiX = eve.clientX
  73.                 let miraiY = eve.clientY
  74.                 let schleft = schX + (miraiX - imaX)
  75.                 let schtop = schY + (miraiY - imaY)
  76.                 schleft = Math.max(schleft, minX)
  77.                 schleft = Math.min(schleft, maxX)
  78.                 schtop = Math.max(schtop, minY)
  79.                 schtop = Math.min(schtop, maxY)
  80.                 imgsorech.style.left = schleft + 'px'
  81.                 imgsorech.style.top = schtop + 'px'
  82.                 return false
  83.             }
  84.         }

  85.         document.onmouseup = function () {
  86.             document.onmousemove = null
  87.         }

  88.         migishita.onmousedown = function (eve) {  //右下角
  89.             eve = eve || window.event
  90.             if (eve.stopPropagation) {
  91.                 eve.stopPropagation()
  92.             } else {
  93.                 eve.cancelBubble = true
  94.             }
  95.             let imaX = eve.clientX
  96.             let imaY = eve.clientY
  97.             let imawidth = imgsorech.offsetWidth
  98.             let imaheight = imgsorech.offsetHeight
  99.             let schX = imgsorech.offsetLeft
  100.             let schY = imgsorech.offsetTop
  101.             let maxwidth = imgbox.clientWidth - schX - 2
  102.             let maxheight = imgbox.clientHeight - schY - 2
  103.             imgsorech.style.bottom = ''
  104.             imgsorech.style.right = ''
  105.             imgsorech.style.left = schX + 'px'
  106.             imgsorech.style.top = schY + 'px'
  107.             document.onmousemove = function (eve) {
  108.                 eve = eve || window.event
  109.                 let miraiX = eve.clientX
  110.                 let miraiY = eve.clientY
  111.                 let schwidth = imawidth + (miraiX - imaX)
  112.                 let schheight = imaheight + (miraiY - imaY)
  113.                 schwidth = Math.min(schwidth, maxwidth)
  114.                 schwidth = Math.max(schwidth, minwidth)
  115.                 schheight = Math.min(schheight, maxheight)
  116.                 schheight = Math.max(schheight, minheight)
  117.                 let seihoukeiSize = Math.min(schwidth, schheight)
  118.                 imgsorech.style.width = seihoukeiSize + 'px'
  119.                 imgsorech.style.height = seihoukeiSize + 'px'
  120.                 return false
  121.             }
  122.         }

  123.         hibarikami.onmousedown = function (eve) {  //左上角
  124.             eve = eve || window.event
  125.             if (eve.stopPropagation) {
  126.                 eve.stopPropagation()
  127.             } else {
  128.                 eve.cancelBubble = true
  129.             }
  130.             let imaX = eve.clientX
  131.             let imaY = eve.clientY
  132.             let imawidth = imgsorech.offsetWidth
  133.             let imaheight = imgsorech.offsetHeight
  134.             let schX = imgbox.clientWidth - imawidth - imgsorech.offsetLeft
  135.             let schY = imgbox.clientHeight - imaheight - imgsorech.offsetTop
  136.             let maxwidth = imgbox.clientWidth - schX - 2
  137.             let maxheight = imgbox.clientHeight - schY - 2
  138.             imgsorech.style.left = ''
  139.             imgsorech.style.top = ''
  140.             imgsorech.style.right = schX + 'px'
  141.             imgsorech.style.bottom = schY + 'px'
  142.             document.onmousemove = function (eve) {
  143.                 let miraiX = eve.clientX
  144.                 let miraiY = eve.clientY
  145.                 let schwidth = imawidth - (miraiX - imaX)
  146.                 let schheight = imaheight - (miraiY - imaY)
  147.                 schwidth = Math.min(schwidth, maxwidth)
  148.                 schwidth = Math.max(schwidth, minwidth)
  149.                 schheight = Math.min(schheight, maxheight)
  150.                 schheight = Math.max(schheight, minheight)
  151.                 let seihoukeiSize = Math.min(schwidth, schheight)
  152.                 imgsorech.style.width = seihoukeiSize + 'px'
  153.                 imgsorech.style.height = seihoukeiSize + 'px'
  154.                 return false
  155.             }
  156.         }

  157.         migikami.onmousedown = function (eve) { //右上角
  158.             eve = eve || window.event
  159.             if (eve.stopPropagation) {
  160.                 eve.stopPropagation()
  161.             } else {
  162.                 eve.cancelBubble = true
  163.             }
  164.             let imaX = eve.clientX
  165.             let imaY = eve.clientY
  166.             let imawidth = imgsorech.offsetWidth
  167.             let imaheight = imgsorech.offsetHeight
  168.             let schX = imgsorech.offsetLeft
  169.             let schY = imgbox.clientHeight - imaheight - imgsorech.offsetTop
  170.             let maxwidth = imgbox.clientWidth - schX - 2
  171.             let maxheight = imgbox.clientHeight - schY - 2
  172.             imgsorech.style.right = ''
  173.             imgsorech.style.top = ''
  174.             imgsorech.style.left = schX + 'px'
  175.             imgsorech.style.bottom = schY + 'px'
  176.             document.onmousemove = function (eve) {
  177.                 let miraiX = eve.clientX
  178.                 let miraiY = eve.clientY
  179.                 let schwidth = imawidth + (miraiX - imaX)
  180.                 let schheight = imaheight - (miraiY - imaY)
  181.                 schwidth = Math.min(schwidth, maxwidth)
  182.                 schwidth = Math.max(schwidth, minwidth)
  183.                 schheight = Math.min(schheight, maxheight)
  184.                 schheight = Math.max(schheight, minheight)
  185.                 let seihoukeiSize = Math.min(schwidth, schheight)
  186.                 imgsorech.style.width = seihoukeiSize + 'px'
  187.                 imgsorech.style.height = seihoukeiSize + 'px'
  188.                 return false
  189.             }
  190.         }

  191.         hibarishita.onmousedown = function (eve) { //左下角
  192.             eve = eve || window.event
  193.             if (eve.stopPropagation) {
  194.                 eve.stopPropagation()
  195.             } else {
  196.                 eve.cancelBubble = true
  197.             }
  198.             let imaX = eve.clientX
  199.             let imaY = eve.clientY
  200.             let imawidth = imgsorech.offsetWidth
  201.             let imaheight = imgsorech.offsetHeight
  202.             let schX = imgbox.clientWidth - imawidth - imgsorech.offsetLeft
  203.             let schY = imgsorech.offsetTop
  204.             let maxwidth = imgbox.clientWidth - schX - 2
  205.             let maxheight = imgbox.clientHeight - schY - 2
  206.             imgsorech.style.left = ''
  207.             imgsorech.style.bottom = ''
  208.             imgsorech.style.right = schX + 'px'
  209.             imgsorech.style.top = schY + 'px'
  210.             document.onmousemove = function (eve) {
  211.                 let miraiX = eve.clientX
  212.                 let miraiY = eve.clientY
  213.                 let schwidth = imawidth - (miraiX - imaX)
  214.                 let schheight = imaheight + (miraiY - imaY)
  215.                 schwidth = Math.min(schwidth, maxwidth)
  216.                 schwidth = Math.max(schwidth, minwidth)
  217.                 schheight = Math.min(schheight, maxheight)
  218.                 schheight = Math.max(schheight, minheight)
  219.                 let seihoukeiSize = Math.min(schwidth, schheight)
  220.                 imgsorech.style.width = seihoukeiSize + 'px'
  221.                 imgsorech.style.height = seihoukeiSize + 'px'
  222.                 return false
  223.             }
  224.         }
  225.     }
  226. </script>
  227. <div id="imgbox">
  228.     <div id="imgsorech">
  229.         <div id="ennSeiyaku">
  230.         </div>
  231.         <div id="migishita" class="muki">
  232.         </div>
  233.         <div id="hibarikami" class="muki">
  234.         </div>
  235.         <div id="migikami" class="muki">
  236.         </div>
  237.         <div id="hibarishita" class="muki">
  238.         </div>
  239.     </div>
  240. </div>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-7 09:27:23 | 显示全部楼层
kogawananari 发表于 2020-10-7 01:49
title属于document
title的mouseup触发后 document的mouseup也会触发
这个叫做冒泡

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 15:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表