-
Notifications
You must be signed in to change notification settings - Fork 22
/
zepto.highlight.js
39 lines (39 loc) · 1.5 KB
/
zepto.highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @file 实现了通用highlight方法。
* @name Highlight
* @desc 点击高亮效果
* @import core/zepto.js, core/zepto.extend.js
*/
(function($) {
var actElem, inited = false, timer, cls, removeCls = function(){
clearTimeout(timer);
if(actElem && (cls = actElem.attr('highlight-cls'))){
actElem.removeClass(cls).attr('highlight-cls', '');
actElem = null;
}
};
$.extend($.fn, {
/**
* @name highlight
* @desc 禁用掉系统的高亮,当手指移动到元素上时添加指定class,手指移开时,移除该class
* @grammar highlight(className) ⇒ self
* @example var div = $('div');
* div.highlight('div-hover');
*
* $('a').highlight();// 把所有a的自带的高亮效果去掉。
*/
highlight: function(className) {
inited = inited || !!$(document).on('touchend.highlight touchmove.highlight touchcancel.highlight', removeCls);
removeCls();
return this.each(function() {
var $el = $(this);
$el.css('-webkit-tap-highlight-color', 'rgba(255,255,255,0)').off('touchstart.highlight');
className && $el.on('touchstart.highlight', function() {
timer = $.later(function() {
actElem = $el.attr('highlight-cls', className).addClass(className);
}, 100);
});
});
}
});
})(Zepto);