仿material点击波浪纹效果 css实现

发布日期 目录 CSS, HTML, JavaScript

仿material点击波浪纹效果 css实现

分享一段css实现的material点击波浪纹效果,代码很简介,逻辑很清晰,觉得不错,拿来保存一下

没有动态图,复制到本地查看下吧

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>仿material点击波浪纹效果</title>
  <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
  <style>
    ul {
      font-size: 0;
      position: relative;
      padding: 0;
      width: 480px;
      margin: 40px auto;
      user-select: none;
    }

    li {
      display: inline-block;
      width: 160px;
      height: 60px;
      background: #E95546;
      font-size: 16px;
      text-align: center;
      line-height: 60px;
      color: #fff;
      text-transform: uppercase;
      position: relative;
      overflow: hidden;
      cursor: pointer;
    }

    .slider {
      display: block;
      position: absolute;
      bottom: 0;
      left: 0;
      height: 4px;
      background: #4FC2E5;
      transition: all 0.5s;
    }

    .ripple {
      width: 0;
      height: 0;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.4);
      -webkit-transform: scale(0);
      -ms-transform: scale(0);
      transform: scale(0);
      position: absolute;
      opacity: 1;
    }

    .rippleEffect {
      -webkit-animation: rippleDrop .4s linear;
      animation: rippleDrop .4s linear;
    }

    @-webkit-keyframes rippleDrop {
      100% {
        -webkit-transform: scale(2);
        transform: scale(2);
        opacity: 0;
      }
    }

    @keyframes rippleDrop {
      100% {
        -webkit-transform: scale(2);
        transform: scale(2);
        opacity: 0;
      }
    }
  </style>
</head>

<body>
  <ul>
    <li>Tab One</li>
    <li>Tab Two</li>
    <li>Tab Three</li>
    <li class="slider"></li>
  </ul>

  <script>
    $("ul li").click(function (e) {

      if ($(this).hasClass('slider')) {
        return;
      }

      var whatTab = $(this).index();

      var howFar = 160 * whatTab;

      $(".slider").css({
        left: howFar + "px"
      });

      $(".ripple").remove();

      var posX = $(this).offset().left,
        posY = $(this).offset().top,
        buttonWidth = $(this).width(),
        buttonHeight = $(this).height();

      $(this).prepend("<span class='ripple'></span>");

      if (buttonWidth >= buttonHeight) {
        buttonHeight = buttonWidth;
      } else {
        buttonWidth = buttonHeight;
      }

      var x = e.pageX - posX - buttonWidth / 2;
      var y = e.pageY - posY - buttonHeight / 2;

      $(".ripple").css({
        width: buttonWidth,
        height: buttonHeight,
        top: y + 'px',
        left: x + 'px'
      }).addClass("rippleEffect");

    });
  </script>
</body>

</html>

发表评论

邮箱地址不会被公开。