javascript - How to Detect Mousedown + Mousemove Left or Right Direction -


can please take @ demo , let me know how can detect whether mousemove toward left or right?

 $('.container').mousedown(function(){      $(this).mousemove(function(){        //if moves left { console.log("moving left"); }        //if moves right {  console.log("moving right"); }      });  });         $('.container').mouseup(function(){      $(this).unbind("mousemove");  });      
.container {      height:200px;      width:200px;      background-color:#1abc9c;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="container"></div>

definitely 1 off many solution:

$('.container').mousedown(function(e1){     var mx = e1.pagex;//register mouse down position      $(this).mousemove(function(e2){          if (e2.pagex > mx){ //right w.r.t mouse down position             console.log("moved right");         } else {             console.log("moved left")         }     }); });        $('.container').mouseup(function(){     $(this).unbind("mousemove"); });       

working code here


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -