温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

原生js如何实现简单的Ripple按钮

发布时间:2021-06-21 13:43:41 来源:亿速云 阅读:155 作者:小新 栏目:web开发

这篇文章主要介绍原生js如何实现简单的Ripple按钮,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

整理文档,搜刮出一个原生js实现简单的Ripple按钮的代码,稍微整理精简一下做下分享。

效果如图

原生js如何实现简单的Ripple按钮

准备食材(html部分)

 <ul id="nav">
  <li>
   <a href='#'>
    <span>首页</span>
   <span class="circle"></span>
   </a>
  </li>
  <li>
   <a href='#'>
    <span>我的</span>
   <span class="circle"></span>
   </a>
  </li>
  <li>
   <a href='#'>
    <span>更多</span>
   <span class="circle"></span>
   </a>
  </li>
 </ul>

典型的菜单li布局,里面的span.circle表示的是触摸弹出的小圆圈。

准备辅料 (css部分)

 #nav {
 display: flex;
 }
 #nav li {
 position: relative;
 overflow: hidden;
 flex: 1;
 }
 li a {
  display: flex;
 flex-direction: column;
 justify-content: center;
 align-items: center;
 }

 .circle{
 position: absolute;
 background: rgba(86,187,247,.1);
 width: 1px;
 height: 1px;
 top:50%;
 left: 50%;
 border-radius: 50%;
 }
 .circle.act{
  animation: navCircle .4s;
 }

 @keyframes navCircle
 {
 from {transform: scale(0);border-radius: 50%;}
 to {transform:scale(200);border-radius: 50%;}
 }

我的思路是这样的,给li相对定位,给小圆圈绝对定位,再给小圆圈添加动画navCircle,采用css3的缩放使其变大,至于为什么是200倍和.4s呢,经过测试这样更人性化,其余的倍数你们也可以试试。

大火烹饪 (js部分)

 var li = document.getElementById('nav').querySelectorAll('li');
 var circle = document.getElementById('nav').querySelectorAll('.circle');
   for(var i=0,len = li.length;i<len;i++){
    ((i)=>{
    li[i].addEventListener('click',(e)=>{
     circle[i].setAttribute('class','circle act');
     circle[i].setAttribute('style','top:'+e.layerY+'px;left:'+e.layerX+'px');
    });

    li[i].addEventListener('touchend',()=>{
     circle[i].setAttribute('class','circle');
    })
    })(i)

   }

以上是“原生js如何实现简单的Ripple按钮”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI