Javascript 滑动功能不适用于我的if语句

Javascript 滑动功能不适用于我的if语句,javascript,jquery,mobile,swipe,Javascript,Jquery,Mobile,Swipe,这是我正在处理的Web项目的滑动功能。但不知何故,我的代码不适用于if语句。如果我只使用这个: (function(){ if ($('#right-col').hasClass('shown')){ $(this).swipe({ swipe:function(event, direction, distance, duration, fingerCount) { $(this).addClass('hidden'

这是我正在处理的Web项目的滑动功能。但不知何故,我的代码不适用于if语句。如果我只使用这个:

(function(){
    if ($('#right-col').hasClass('shown')){
        $(this).swipe({
            swipe:function(event, direction, distance, duration, fingerCount) {
                $(this).addClass('hidden');//.text(direction);
            }
        });
    }; 
})();
它很好用。有人能告诉我,我上面的自调用函数有什么特别的错误吗

当您执行
$(this)时。滑动(…)
这是
窗口。使用元素的id选择元素:

$('#right-col').swipe({
  swipe:function(event, direction, distance, duration, fingerCount) {
    $(this).addClass('bg-blue').text("You swiped " + direction );
  }
})

请注意,如果
#right col
元素的类稍后更改,则滑动功能不会受到影响。

这有点离题。。。但不久前,我编写了自己的滑动函数

此功能主要在ios设备中测试

1.创建自定义事件

fc=快速点击

swl=swipeleft

swr=swiperight

swu=swipeup

swd=swipedown

2.它比其他滑动快得多,您可以使用简单的纯js addeventlistener或attachevent

3.它是用纯javascript编写的

4.fastclick事件还允许您更快地单击元素

点击=400-500毫秒

快速点击=40-50ms

(function () {
    if ($('#right-col').hasClass('shown')) {
        $('#right-col').swipe({
            swipe: function (event, direction, distance, duration, fingerCount) {
                $(this).addClass('hidden'); //.text(direction);
            }
        });
    };
})();
window.onload=function(){
(职能(d){
变量
ce=function(e,n){var a=document.createEvent(“CustomEvent”);a.initCustomEvent(n,true,true,e.target);e.target.dispatchEvent(a);a=null;返回false},
nm=true,sp={x:0,y:0},ep={x:0,y:0},
触碰={
touchstart:function(e){sp={x:e.touchs[0].pageX,y:e.touchs[0].pageY},
touchmove:function(e){nm=false;ep={x:e.touchs[0]。pageX,y:e.touchs[0]。pageY},

touchend:function(e){if(nm){ce(e,'fc')}else{var x=ep.x-sp.x,xr=Math.abs(x),y=ep.y-sp.y,yr=Math.abs(y);if(Math.max(xr,yr)>20{ce(e,(xr>yr?(x(this))不会在这样的if语句中触发是的,解决了我的问题。有时我仍然使用“this”-关键字错误。但感谢您的帮助!
这在javascript中肯定会令人困惑。
window.onload=function(){
(function(d){
 var
 ce=function(e,n){var a=document.createEvent("CustomEvent");a.initCustomEvent(n,true,true,e.target);e.target.dispatchEvent(a);a=null;return false},
 nm=true,sp={x:0,y:0},ep={x:0,y:0},
 touch={
  touchstart:function(e){sp={x:e.touches[0].pageX,y:e.touches[0].pageY}},
  touchmove:function(e){nm=false;ep={x:e.touches[0].pageX,y:e.touches[0].pageY}},
  touchend:function(e){if(nm){ce(e,'fc')}else{var x=ep.x-sp.x,xr=Math.abs(x),y=ep.y-sp.y,yr=Math.abs(y);if(Math.max(xr,yr)>20){ce(e,(xr>yr?(x<0?'swl':'swr'):(y<0?'swu':'swd')))}};nm=true},
  touchcancel:function(e){nm=false}
 };
 for(var a in touch){d.addEventListener(a,touch[a],false);}
})(document);

// example
var h=function(e){console.log(e.type);};
document.body.addEventListener('fc',h,false);
document.body.addEventListener('swl',h,false);
document.body.addEventListener('swr',h,false);
document.body.addEventListener('swu',h,false);
document.body.addEventListener('swd',h,false);

}