javascript - jQuery 'on mousemove' not slide when mouse out of the frame -
i want make slider jquery. when pulled out frame "sildercontainer", "sliders" stop moving , when returned through "slidercontainer", "slider" move again. want how "slider" still moving when pulled mouse out of frame.
here code:
<style> body{ padding:60px; height:800px; } .slidercontainer{ //position:relative; width:200px; background-color:#ccc; height:30px; margin-top:16px; cursor:pointer; } .slider{ position:absolute; width:50px; height:30px; background:#fa565a; float:right; } </style> </head> <body> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(document).on('ready', function(){ function moveslider(e){ e.preventdefault(); var pos = $(e.currenttarget).offset() , posx = e.pagex - pos.left; if(posx >= 0 && posx <= $(e.currenttarget).outerwidth()){ $('.slider').css('width', posx+'px'); } } $('.slidercontainer').on('mousedown', function(e){ moveslider(e); $(this).on('mousemove', function(e){ moveslider(e); }); }).on('mouseup', function(){ $(this).off('mousemove'); }); }); </script> <div class='slidercontainer'> <div class='slider'></div> </div> </body>
any appreciated. thank you.
it's because target element .slidercontainer. think - when you're on web page , hold down mouse on normal form button , move mouse in , out of it, focus lost on object.
http://www.w3schools.com/html/html_forms.asp
look 'submit button' section , test out.
what logic says while focus on container element , mouse move event registered, javascript can respond it. focus lost (eg. moving outside of region of element) can't respond anymore.
instead of attaching events container element itself, register them document or window, or object has larger space behind it, focus maintained.
here's example: losing mouseup event if releasing not on same element
Comments
Post a Comment