mousemove to touchmove isn不工作

时间:2014-07-23 16:59:54

标签: javascript jquery mobile

我有一个非常简单的脚本,其中两个图像相互叠加,然后通过水平滑动鼠标,顶部图像淡出或重新出现。这用于当时/现在的混搭。

该脚本在桌面上运行良好,但我无法在移动设备上运行。在移动设备上,“鼠标”仅在初始触摸时被拾取,而不是幻灯片。

我尝试过touch-punch.js,尝试修改我的代码以使用它,但到目前为止我还没有任何运气......

这是我的代码

<script type="text/javascript" language="JavaScript">

$(document).ready(function() {
    $('.trackMe').each(function(){
        //$(this).children("img:last").mousemove(function(e) {
        $(this).children("img:last").on("mousemove touchmove", function(e) {
            var offset = $(this).offset();
            var xpos = (e.pageX - offset.left);
            var ypos = (e.pageY - offset.top);
            //now to get the first child image width..
            var thisImage = $(this);
            var thisWidth = thisImage.width();
            var pct = Math.round((xpos/thisWidth)*100)/100;
            var ipct = Math.abs(Math.round(((xpos-thisWidth)/thisWidth)*100)/100);
            thisImage.css({ 'opacity' : ipct });
        });
    });
});

</script>

我没有公开访问它,但缺少的唯一组件是样式和上/下图像。

<style type="text/css">
    .trackMe img.packard {
        width:100% !important;
        top:0 !important;
        left:0 !important;
        position:absolute;
        margin:0 !important;
    }

    .trackMe img.now{
        width:100% !important;
        margin:0 !important;
    }
</style>

<div style="position:relative; min-width:320px; height:auto; overflow:hidden;" class="trackMe">
    <img src="1a.jpg" class="now" />
    <img src="1b.jpg" class="packard" />
</div>

1 个答案:

答案 0 :(得分:6)

嘿,在你的代码中试试这个.. 试着像这样得到x和y

$(document).bind('touchmove mousemove', function (e) {

    var currentY = e.originalEvent.touches ?  e.originalEvent.touches[0].pageY : e.pageY;
      var currentX = e.originalEvent.touches ?  e.originalEvent.touches[0].pageX : e.pageX;
//do your stuff
});

感谢