我有一個非常簡單的代碼,通過觸摸屏拖動/移動元素。代碼工作正常,唯一的問題是一個常見的問題,但無論我如何嘗試,我都無法修復從接觸點拖動的代碼。相反,無論初始觸摸位置如何,每個運動都從元素的邊緣開始。如何用touchmove找到偏移位置?
我看到了一些建議,但都失敗了。我希望較小的簡單代碼將吸引一個簡單的修復。
這裏是JavaScript:
<script>
$("#drag").on('touchmove',function(e){
// This is supposed to be the fix,
// but regardless of positioning
// (relative parent) of the divs,
// it is failing ---
var offset = $(this).offset();
var relX =(e.clientX - offset.left);
// By rights the above is the correct solution. But I
// cannot suss out why it is not working.
var touch = e.originalEvent.touches[0];
var x = touch.clientX
$(this).css({
"-webkit-transform": "translate3d("+ x +"px,"+ 0 +"px,0)"
});
});
</script>
<! --And here is the html -->
<body>
<div id="container" class="container">
<div id='drag' class='drag'>
<!---contents--->
</div>
</div>
CSS.
#container{
position:relative;
top: 0%;
left: 0px;
width: 500px;
height: 500px;
background: #ccc;
}
#drag {
position:absolute;
top: 20%;
left: 10px;
width: 150px;
height: 10%;
background: #0a0;
}
不是重複的方式,這是一個完全不同的代碼。考慮到我還沒有達到我所需要的信息,我會堅持下去。 – russbaker2016
這裏是一個jfiddle http://jsfiddle.net/xbmyazb2/22/ – russbaker2016
非常接近的呼叫,但仍然沒有達到標準... http://jsfiddle.net/415rxhgy/7/ – russbaker2016