移动与pc端的touch,mouse,click事件触发过程

测试代码以下:测试

   <div id="parent">
            parent
            <div id="child">点击我</div>
        </div>
        
      <script>
          document.getElementById("parent").addEventListener("touchstart",function(){
              console.log("parent touchstart");
          })
          document.getElementById("parent").addEventListener("touchmove",function(){
              console.log("parent touchmove");
          })
          document.getElementById("parent").addEventListener("touchend",function(){
              console.log("parent touchend");
          })
          document.getElementById("parent").addEventListener("click",function(){
              console.log("parent click");
          })
          document.getElementById("child").addEventListener("touchstart",function(e){
              console.log("child touchstart");     
              e.preventDefault();      
          })
          document.getElementById("child").addEventListener("touchmove",function(){
              console.log("child touchmove");
          })
          document.getElementById("child").addEventListener("touchend",function(){
              console.log("child touchend");
          })
          document.getElementById("child").addEventListener("click",function(){
              console.log("child click");
          })

          document.getElementById("parent").addEventListener("mousedown",function(){
              console.log("parent mousedown");
          })
          document.getElementById("parent").addEventListener("mousemove",function(){
              console.log("parent mousemove");
          })
          document.getElementById("parent").addEventListener("mouseup",function(){
              console.log("parent mouseup");
          })
          document.getElementById("child").addEventListener("mousedown",function(){
              console.log("child mousedown");
          })
          document.getElementById("child").addEventListener("mousemove",function(){
              console.log("child mousemove");
          })
          document.getElementById("child").addEventListener("mouseup",function(){
              console.log("child mouseup");
          })

 

在PC端:spa

1.pc端无touch相关事件,因此touchstart,touchmove,touchend事件无响应。code

2.点击子元素,由于须要先移动到元素上因此触发了mousemove事件并冒泡到父元素上,而后点击,依次出发mousedown并冒泡,触发mouseup并冒泡,触发click并冒泡。(注意会先执行冒泡事件而后在执行下一个触发事件)blog

打印以下:事件

 

3.在元素上拖动时,会在mousedown后执行mousemove事件并执行一次冒泡一次,最后依然会执行click事件。ip

4.不管点击仍是拖动,子元素的click事件都会执行并冒泡。e.stopPropogation()和e.preventDefault()都不可阻止click事件的触发。可是click事件的触发必须是mousedown和mouseup在同一个元素上执行才能够,若是元素滑动到了元素外,则不会触发click事件。get

在移动端:io

1.在移动端mouse相关事件只在点击的时候有效,若是没有touch事件,只有mouse事件,点击子元素时会依次触发mousemove,mousedown,mouseup,click。若是有touch事件,点击子元素时会依次触发touchstart,touchend,mousemove,mousedown,mouseup,click事件。而且每个事件都会触发父元素的相关事件。若是在子元素的touchstart或touchend事件中添加console

e.preventDefault(),则点击子元素后touchend事件后面的事件都再也不触发。测试中发现

若是在父元素的touchstart或touchend事件中添加event

e.preventDefault(),则点击子元素后touchend事件后面的事件也都再也不触发。
 
注意:滑动元素时在touch事件中添加e.preventDefault()后会阻止onscroll事件,会致使页面不滚动。

2.滑动子元素时,mouse相关事件和click事件不会触发,只会依次触发touchstart,touchmove,touchend并冒泡到父元素上。

相关文章
相关标签/搜索