其实原理很简单,我们点击的时候我们给元素加上一个自定义的attr,加上后便会有有一个匹配的样式去自动适配背景,几秒后去掉该样式恢复原状
首先在自己的js中拓展一个方法hoverel
复制代码 代码如下:
$.extend($.fn, {
hoverel:function(){
var _this = $(this);
var _t = settimeout(function(){
_this.attr(hover, on);
}, 10);
_this.attr(hovertimeout, _t);
settimeout(function(){
cleartimeout( _this.attr(hovertimeout) );
var _t = settimeout(function(){
_this.removeattr(hover);
}, 100);
_this.attr(hovertimeout, _t);
},200);
}
});
其次定义样式,当特定attr被加上时
复制代码 代码如下:
li[hover=on]{
background-image:-webkit-gradient(linear, 0% 100%, 0% 0%, from(#194fdb), to(#4286f5))!important;
background-image: -webkit-linear-gradient(top, #4286f5, #194fdb)!important;
color: white!important;
cursor: pointer!important;
}
调用示例:
复制代码 代码如下:
$(e.target).hoverel();
