这是一款效果非常炫酷的jQueryCSS3炫酷按钮点击波特效。该特效当用户在菜单按钮上点击的时候,从鼠标点击的点开始,会有一道光波以改点为原点向外辐射的动画效果,非常绚丽。

使用方法

HTML结构

该例中的点击波按钮使用的是一个无序列表。

<ul>
  <li><a>Dashboard</a></li>
  <li><a>My Account</a></li>
  <li><a>Direct Messages</a></li>
  <li><a>Chat Rooms</a></li>
  <li><a>Settings</a></li>
  <li><a>Logout</a></li>
</ul>          
              
CSS样式

首先作为按钮使用的<a>元素和它的父元素li都设置为相对定位方式。为了使后面的光波效果不至于扩散到按钮之外,设置li元素的overflow:hidden;

ul {
      background: white; border-top: 6px solid hsl(180, 40%, 60%);
      width: 200px; margin: 5em auto;
    }
    ul li {
      list-style-type: none;
      position: relative;
      overflow: hidden;
    }
    ul li a {
      font: normal 14px/28px Montserrat; 
      color: hsl(180, 40%, 40%);
      display: block; 
      padding: 10px 15px;
      text-decoration: none;
      cursor: pointer; /*since the links are dummy without href values*/
      /*prevent text selection*/
      user-select: none;
      position: relative;
    }              
              

.ink是点击波特效的主要样式,它是通过js动态生成的<span>元素,它的位置和大小有js代码决定。开始时,.ink元素的尺寸被设置为0,然后在用户点击时被放大。

/*.ink styles - the elements which will create the ripple effect. The size and position of these elements will be set by the JS code. Initially these elements will be scaled down to 0% and later animated to large fading circles on user click.*/
.ink {
  display: block; 
  position: absolute;
  background: hsl(180, 40%, 80%);
  border-radius: 100%;
  transform: scale(0);
}
/*animation effect*/
.ink.animate {animation: ripple 0.65s linear;}
@keyframes ripple {
  /*scale the element to 250% to safely cover the entire link and fade it out*/
  100% {opacity: 0; transform: scale(2.5);}
}           
              
JAVASCRIPT

该点击波特效的js代码使用jQuery来实现。当列表中的<a>元素被点击的时候,插件会查找它的父元素li中是否有.ink的元素,如果没有则创建一个。然后设置.ink元素的尺寸大小和坐标,最后为它添加.animate class,使其产生点击波的动画效果。

<script type="text/javascript">
var parent, ink, d, x, y;
$("ul li a").click(function(e){
  parent = $(this).parent();
  //create .ink element if it doesn't exist
  if(parent.find(".ink").length == 0)
    parent.prepend("<span class='ink'></span>");
    
  ink = parent.find(".ink");
  //incase of quick double clicks stop the previous animation
  ink.removeClass("animate");
  
  //set size of .ink
  if(!ink.height() && !ink.width())
  {
    //use parent's width or height whichever is larger for the diameter to make a circle which can cover the entire element.
    d = Math.max(parent.outerWidth(), parent.outerHeight());
    ink.css({height: d, width: d});
  }
  
  //get click coordinates
  //logic = click coordinates relative to page - parent's position relative to page - half of self height/width to make it controllable from the center;
  x = e.pageX - parent.offset().left - ink.width()/2;
  y = e.pageY - parent.offset().top - ink.height()/2;
  
  //set the position and add class .animate
  ink.css({top: y+'px', left: x+'px'}).addClass("animate");
})
</script>