Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make the width of and element change when a touch and drag is detected on a touch device?
    primarykey
    data
    text
    <p>I have a slider that I built that changes the width of it to slide the element into view. On a desktop there are buttons to do this. I want to be able to make the slider work with a touch and drag event and for it to be smooth like iosslider. I have found a way that works but it is choppy and does not always respond. </p> <p>My code... </p> <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" &gt;&lt;/script&gt; &lt;script type="text/javascript"&gt;$(document).bind("mobileinit", function(){$.extend($.mobile , {autoInitializePage: false})});&lt;/script&gt; &lt;script src="scripts/jquery.mobile-1.3.2.min.js"&gt;&lt;/script&gt; &lt;script&gt; function clickNDrag(){ var mousePosition = event.pageX; $(this).bind('vmousemove',function(){ var mouseCurrentPosition = event.pageX; var positionNumber = mouseCurrentPosition - mousePosition; if(positionNumber &gt; 0){ var widthAppend = 20; } else { var widthAppend = -20; } $(this).css({width: '+=' + widthAppend}); }); $(this).vmouseup(function(){ $(this).unbind('mousemove'); }); $(this).vmouseleave(function(){ $(this).unbind('mousemove'); }); } $(document).ready(function(){ $('.slider').bind('vmousedown',clickNDrag); }); &lt;/script&gt; </code></pre> <p>What I have done is I loaded jQuery Mobile and only loaded the touch events of it. </p> <p>The script checks to see where the virtual mouse is and then when it moves checks to see if it moved right or left and then adds 20px or subtracts 20px from the slider.</p> <p>How would I do this in a more natural feeling and smooth way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload