鼠标滚轮用于滑动

时间:2013-06-19 06:23:33

标签: javascript jquery

我有一个适用于鼠标滚轮的功能,我想使用相同的功能进行滑动。

// Mouse Wheel support 
this.MouseWheel =
{   
    init: function()
    {
        // Init mouse wheel listener 
        if(window.addEventListener)
        {
            my.ImageFlowDiv.addEventListener('DOMMouseScroll', my.MouseWheel.get, false);
        }
        my.Helper.addEvent(my.ImageFlowDiv,'mousewheel',my.MouseWheel.get);
    },

    get: function(event)
    {
        var delta = 0;
        if (!event)
        {
            event = window.event;
        }
        if (event.wheelDelta)
        {
            delta = event.wheelDelta / 120;
        }
        else if (event.detail)
        {
            delta = -event.detail / 3;
        }
        if (delta)
        {
            my.MouseWheel.handle(delta);
        }
        my.Helper.suppressBrowserDefault(event);
    },

    handle: function(delta)
    {
    alert('handle called');
        var change = false;
        var newImageID = 0;
        if(delta > 0)
        {
            if(my.imageID >= 1)
            {
                newImageID = my.imageID -3;
                change = true;
            }
        }
        else
        {
            if(my.imageID < (my.max-1))
            {
                newImageID = my.imageID +4;
                change = true;
            }
        }

        /* Glide to next (mouse wheel down) / previous (mouse wheel up) image  */
        if(change)
        {
        //alert('new image id='+newImageID);
            my.glideOnEvent(newImageID);
        }
    }
};

早些时候我使用了touchswipe插件,它运行正常,但我想使用另一个插件touchswipe令人不安的触摸支持。

$("#imageFlow").swipe({
  swipe:function(event, direction, distance, duration, fingerCount) {
    alert('swipe called');
    if(direction == 'left') {
      my.MouseWheel.handle(-1);
    } else if (direction == 'right') {
      my.MouseWheel.handle(1);
    }       
  }
});

1 个答案:

答案 0 :(得分:0)

你看过Hammer JS了吗?我发现他们的语法使用起来相当简单,并且不会干扰我的其他触摸事件。

您的代码可能与此类似:

Hammer($('#imageFlow')).on("swipe", function(event) {
        console.log('swipe called'); //(instead of alert)
        if (event.gesture.direction === 'left'){
           my.MouseWheel.handle(-1);
        } else if (event.gesture.direction === 'right'){
           my.MouseWheel.handle(1);
        };
    });

docs

还要记住在JavaScript中使用3个等号。这样:

if (myName === 'Jean-Marie'){//dostuff}

而不是

if (myName == 'Jean-Marie'){//dostuff}