首页 > 帮忙解释一下这几行有关触屏事件的代码?

帮忙解释一下这几行有关触屏事件的代码?

//?注释部分不是很理解
$(function(){
    var size=$(window).width()/20;
    $("html").css("font-size",size);
    var myscroll=new IScroll("#file-list");
   
    attachEvent($("#file-list li"),function(){
        $(this).remove();
    })
})
function  attachEvent(src,cb){
    $(src).unbind();//?
    var isTouchDevice="ontouchstart"in window||navigator.msMaxTouchPoints;//?
    if(isTouchDevice){
        $(src).bind("touchstart",function(event){
            $(this).data("touchon",true);//?
            $(this).addClass("pressed");
        });
        $(src).bind("touchend",function(){
            if($(this).data("touchon")){//?
                cb.bind(this)();//?
            }
            $(this).data("touchon",false);
        });
        $(src).bind("touchmove",function(){
            $(this).data("touchon",false);
            $(this).removeClass("pressed");
        })
    }else{
        //绑定鼠标事件
        $(src).bind("mousedown",function(){
            $(this).addClass("pressed");
            $(this).data("touchon",true);
            cb.bind(this)();
        });
        $(src).bind("mouseup",function(){
            $(this).removeClass("pressed");
            $(this).data("touchon",false);
            cb.bind(this)();
        })
    }
}

$(src).unbind(); 就是取消绑定咯,然后后面再绑定。

var isTouchDevice="ontouchstart"in window||navigator.msMaxTouchPoints //判断是否是触屏.
"ontouchstart"in window:看window对象有没有ontouchstart属性,如果有window.ontouchstart,这个 "ontouchstart"in window会返回true. 后同。


$(src).unbind();
// src最上面代码传参过来的$("#file-list li"),解除src上所有通过 jQuery 附加的事件处理程序或函数

var isTouchDevice="ontouchstart"in window||navigator.msMaxTouchPoints;
// 判断是否支持触屏 用||号是因为针对不同客户端使用不同的判断方式,看看这个链接http://www.cnblogs.com/jaxu/archive/2013/05/21/3089995.html

$(this).data("touchon",true);
// 为当前触碰的$("#file-list li")元素加上data-touchon="true"的属性,这个你看一下jQ的data函数

if($(this).data("touchon")){// 判断当前触碰的$("#file-list li")元素是否存在data-touchon属性
    cb.bind(this)();// cb就是最上面代码传参过来的function(){$(this).remove();},绑定bind(this)就是保持function中的this指向,看看这个链接http://www.jb51.net/article/48729.htm
}
【热门文章】
【热门文章】