移动端不能使用click,由于click会有300ms。全部有了fastclick这样的解决方案。而后fastclick并无解决点击态(用户点击的瞬间要有及时的外观变化反馈)的问题。hover会有不消失的问题,全部你们通常用:active。利用 :active 伪类来设置某元素被点击时的点击态样式。
在IOS上使用active必须声明下面js:git
document.addEventListener("touchstart", function() {},false);复制代码
CSS中记得去掉highlight color:github
-webkit-tap-highlight-color: rgba(0,0,0,0);复制代码
须要注意的是:Android 2.x 仍不支持:active。web
那么就 fastclick + :active + 一堆声明 + 放弃部分系统的兼容?app
有没有更好的解决方案?且看AlloyTouch Button插件~~函数
new AlloyTouch.Button(selector, onTap [,activeClass])复制代码
new AlloyTouch.Button("#button", function () {
console.log("You tapped me.");
}, "active");复制代码
AlloyTouch.Button = function (selector, tap, active) {
var element = typeof selector === "string" ? document.querySelector(selector) : selector;
var option = {
touch: selector,
tap: tap,
preventDefault: false
};
if (active !== undefined) {
option.touchStart = function ( ) {
addClass(element, active);
};
touchMove = function ( ) {
removeClass(element, active);
};
option.touchEnd = function ( ) {
removeClass(element, active);
};
option.touchCancel = function () {
removeClass(element, active);
};
}
new AlloyTouch(option);
}复制代码
在建立Button对象实例的时候,其实建立了AlloyTouch对象实例。这里分析在option。ui
当用户传入了active参数时候,分别给绑定了touchMove、toucStart、touchEnd和touchCancel事件。spa
更多例子演示和代码能够在Github上找到。
Github:github.com/AlloyTeam/A…插件