javascript - 帮忙解释一下这几行有关触屏事件的代码?
巴扎黑
巴扎黑 2017-04-10 16:58:03
[JavaScript讨论组]
//?注释部分不是很理解
$(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)();
        })
    }
}
巴扎黑
巴扎黑

全部回复(2)
大家讲道理
$(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
}
伊谢尔伦

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号