简体   繁体   English

Jquery触摸启用滑块

[英]Jquery touch enable slider

I have setup a slider, here is jsfiddler http://jsfiddle.net/zZv5B/ . 我已经设置了一个滑块,这里是jsfiddler http://jsfiddle.net/zZv5B/ How can I enable it for touch devises, I want to be able to swipe through panel area to slide next and prev slide. 如何为触摸设备启用它,我希望能够通过面板区域滑动以下一个滑动和上一个幻灯片。 any idea would be really appreciated. 任何想法都会非常感激。

var currentIndex = 0;// store current pane index displayed
var ePanes = $('#slider .panel');// store panes collection
function showPane(index){// generic showPane
    // hide current pane
    ePanes.eq(currentIndex).stop(true, true).fadeOut();
    // set current index : check in panes collection length
    currentIndex = index;
    if(currentIndex < 0) currentIndex = ePanes.length-1;
    else if(currentIndex >= ePanes.length) currentIndex = 0;
    // display pane
    ePanes.eq(currentIndex).stop(true, true).fadeIn();
    // menu selection
    $('.nav li').removeClass('current').eq(currentIndex).addClass('current');
}
// bind ul links
$('.nav li').click(function(ev){    showPane($(this).index());});
// bind previous & next links
$('.previous').click(function(){    showPane(currentIndex-1);});
$('.next').click(function(){    showPane(currentIndex+1);});
// apply start pane
showPane(0);

Use the events ( more here ) 'touchstart' and 'touchend' and get the start X position and the end x position. 使用事件( 更多这里 )'touchstart'和'touchend'并获得起始X位置和结束x位置。 You can then compare the two and determine which direction the touch/swipe has happend. 然后,您可以比较两者并确定触摸/滑动发生的方向。

var xStart, xEnd;
$('.wrap').on('mousedown touchstart', function (e) {     
    //get start x position
    xStart = e.pageX;
}).on('mouseup touchend', function (e) {
    //get the end x position 
    xEnd = e.originalEvent.pageX;
    if (xStart != xEnd) {
        //swiped
        if (xStart < xEnd) {
            console.log('Right');
            showPane(currentIndex + 1);
        }
        if (xStart > xEnd) {
            console.log('Left');
            showPane(currentIndex - 1);
        }
    }
});

example fiddle - not sure how browser compatibile this is. 示例小提琴 - 不确定浏览器兼容性如何。

Or you could just use my fave touch enabled slider swipejs 或者你可以使用我的fave touch启用滑块swipejs

UPDATE: To make it work correctly for mobile changed xEnd = e.originalEvent.pageX as per @User543294's comment 更新:为了使其正常工作移动更改xEnd = e.originalEvent.pageX根据@ User543294的评论

Also a new fiddle example to using e.changedTouches[0].pageX as per MDN documetation 也是一个新的小提琴例如使用e.changedTouches [0] .pageX按MDN documetation

I just came across TouchSwipe . 我刚遇到TouchSwipe It seems pretty nice, straightforward, and robust. 它看起来非常好,直截了当,而且非常强大。

Maybe something like this? 也许是这样的?

var swipe_obj = { 
    swipeLeft: function(event, direction, distance, duration, fingerCount) { 
        $(".next").click(); }, 
    swipeRight: function(event, direction, distance, duration, fingerCount) { 
        $(".previous").click(); }, 
};

$(".nav li").swipe(swipe_obj);

Many touch devices also trigger a click, after a touch event. 在触摸事件之后,许多触摸设备也会触发点击。 They usually follow a secuence similar to this: a touch happens, then it will be fired the events "touchstart" -> "touchend" -> "click". 他们通常遵循类似于此的安全:触摸发生,然后它将被触发事件“touchstart” - >“touchend” - >“点击”。 Where that last click it's a fake one, and has a huge delay respect native events. 最后点击它的地方是假的,并且对原生事件有很大的延迟。 But it also works. 但它也有效。

So, if efficiencie don't care, don't worry. 所以,如果效率不在乎,请不要担心。 Your slider will work as you expected it will do. 您的滑块将按预期的方式工作。

But if you are interested in to achieve a good performance, I recommend you to handle native events directly. 但如果您有兴趣获得良好的性能,我建议您直接处理本机事件。 Which can be done using Modernizr dinamic testing. 使用Modernizr dinamic测试可以做到这一点。

It's a simple idea: 这是一个简单的想法:

1 Download and link the Modernizr library into your code. 1下载Modernizr库并将其链接到您的代码中。 2 Test at the first line of your code which kind of events you can expect, based on device capabilities. 2根据设备功能,在代码的第一行测试您可以预期的事件类型。 A single line of javascript will be enough: 一行javascript即可:

var actualEvent = (Modernizr.touch) ? 'touchstart' : 'click';

Extra points if you do this before onReady occurs. 如果在onReady发生之前执行此操作,请加分。

3 Move your current handlers into the "on" form: 3将当前处理程序移到“on”表单中:

$('selector').on(actualEvent, callback)

Then you will be always handling the real event, not the simulated click fired by tablets and phones. 然后,您将始终处理真实事件,而不是平板电脑和手机触发的模拟点击。 Because the test at the beginning ensures you that "actualEvent" will be "touchstart" just on touchable devices and "click" for the rest. 因为一开始的测试可以确保“actualEvent”在触摸设备上只是“touchstart”,其余部分则“点击”。

Of course, you can substitute "touchstar" by the touch event you want. 当然,您可以通过所需的触摸事件替换“touchstar”。 But I think that "touchstar" is the one who best suits your needs. 但我认为“touchstar”是最适合您需求的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM