1#

其实原理很简单,我们点击的时候我们给元素加上一个自定义的attr,加上后便会有有一个匹配的样式去自动适配背景,几秒后去掉该样式恢复原状

首先在自己的js中拓展一个方法hoverEl

  1. $.extend($.fn, {
  2.         hoverEl:function(){

  3.             var _this = $(this);
  4.             var _t = setTimeout(function(){
  5.                 _this.attr("hover", "on");
  6.             }, 10);
  7.             _this.attr("hoverTimeout", _t);

  8.             setTimeout(function(){
  9.                 clearTimeout( _this.attr("hoverTimeout") );
  10.                 var _t = setTimeout(function(){
  11.                     _this.removeAttr("hover");
  12.                 }, 100);
  13.                 _this.attr("hoverTimeout", _t);
  14.             },200);

  15.         }
  16.     });
复制代码

其次定义样式,当特定attr被加上时

  1. li[hover=on]{
  2.     background-image:-webkit-gradient(linear, 0% 100%, 0% 0%, from(#194FDB), to(#4286F5))!important;
  3.     background-image: -webkit-linear-gradient(top, #4286F5, #194FDB)!important;
  4.     color: white!important;
  5.     cursor: pointer!important;
  6. }
复制代码

调用示例:

  1. $(e.target).hoverEl();
复制代码