手指触摸动画效果(完整代码附效果图)

本文共有两个示例,先上图 

示例一:  示例二:

示例一代码(微信小程序):

 
  1. // pages/test/test.js

  2. Page({

  3. containerTap: function (res) {

  4. var that = this

  5. var x = res.touches[0].pageX;

  6. var y = res.touches[0].pageY + 85;

  7. this.setData({

  8. rippleStyle: ''

  9. });

  10. setTimeout(function () {

  11. that.setData({

  12. rippleStyle: 'top:' + y + 'px;left:' + x + 'px;-webkit-animation: ripple 0.4s linear;animation:ripple 0.4s linear;'

  13. });

  14. },200)

  15. },

  16. })

 
  1. <view class="ripple" style="{{rippleStyle}}"></view>

  2. <view class="container" bindtouchstart="containerTap"></view>

 
  1. page{height:100%}

  2. .container{

  3. width:100%;

  4. height:100%;

  5. overflow: hidden

  6. }

  7. .ripple {

  8. background-color: rgba(0, 0, 0, 0.6);

  9. border-radius: 100%;

  10. height:10px;

  11. width:10px;

  12. margin-top: -90px;

  13. position: absolute;

  14. -webkit-transform: scale(0);

  15. overflow: hidden

  16. }

  17. @-webkit-keyframes ripple {

  18. 100% {

  19. -webkit-transform: scale(12);

  20. transform: scale(12);

  21. background-color: transparent;

  22. }

  23. }

示例二代码(html5)

 
  1. <!DOCTYPE html>

  2. <html>

  3.  
  4. <head>

  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

  6. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

  7. <title>点击后水波纹扩散填充组件效果</title>

  8. <style>

  9. .btn {

  10. position: relative;

  11. width: 13em;

  12. height: 3em;

  13. margin: 2em;

  14. border: none;

  15. outline: none;

  16. letter-spacing: .2em;

  17. font-weight: bold;

  18. background: #F6774F;

  19. cursor: pointer;

  20. overflow: hidden;

  21. user-select: none;

  22. border-radius: 2px;

  23. color: #fff;

  24. }

  25.  
  26. .ripple {

  27. position: absolute;

  28. background: rgba(0, 0, 0, .15);

  29. border-radius: 100%;

  30. transform: scale(0);

  31. pointer-events: none;

  32. }

  33.  
  34. .ripple.show {

  35. animation: ripple 1s ease-out;

  36. }

  37.  
  38. @keyframes ripple {

  39. to {

  40. transform: scale(2);

  41. opacity: 0;

  42. }

  43. }

  44. </style>

  45. </head>

  46.  
  47. <body>

  48. <h1 class="center mt40">点击后水波纹扩散填充组件效果</h1>

  49. <div class="main center">

  50. <button id="bowen" class="btn ">BUTTON</button>

  51. </div>

  52. <script>

  53. var addRippleEffect = function(e) {

  54. var target = e.target;

  55. // target 事件属性可返回事件的目标节点(触发该事件的节点)

  56. // console.log(e.target)

  57. if(target.id != 'bowen') return false;

  58. // 如果当前节点的id不等于'bowen',就返回false,停止执行函数

  59. var rect = target.getBoundingClientRect();

  60. // target.getBoundingClientRect() 方法返回元素的大小及其相对于视口的位置。

  61. var ripple = target.querySelector('.ripple');

  62. // target.querySelector()方法返回文档中匹配指定 CSS 选择器的一个元素。

  63. console.log(ripple) //这里创建一个ripple节点对象,此时为null

  64. if(!ripple) {

  65. ripple = document.createElement('span'); //这里ripple等于<span></span>

  66. // document.createElement()在当前节点创建一个标签元素

  67. ripple.className = 'ripple';//这里ripple等于<span class="ripple"></span>

  68. // 给ripple添加一个样式类名

  69. ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px';//这里height和width是相等的

  70. // Math.max(a,b)返回两个指定的数中带有较大的值的那个数。

  71. target.appendChild(ripple);//在当前节点添加ripple元素对象

  72. // appendChild(); 在指定节点添加元素

  73. }

  74. ripple.classList.remove('show');//移除ripple对象名为的'show' CSS 类

  75. // classList 属性返回元素的类名,可以使用 add() 和 remove() 方法修改它.该属性用于在元素中添加,移除及切换 CSS 类。

  76. var top = e.pageY - rect.top - ripple.offsetHeight / 2 - document.body.scrollTop;

  77. var left = e.pageX - rect.left - ripple.offsetWidth / 2 - document.body.scrollLeft;

  78. // e.pageY 显示鼠标的位置 offsetHeight 获取元素的高度 offsetWidth 获取元素的宽度 scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。

  79. ripple.style.top = top + 'px';

  80. ripple.style.left = left + 'px';

  81. ripple.classList.add('show');

  82. return false;

  83. }

  84. document.addEventListener('click', addRippleEffect);//addEventListener('事件名称',执行函数)监听事件

  85. </script>

  86.  
  87. </body>

  88.  
  89. </html>

转自:https://blog.csdn.net/qq_35713752/article/details/78682954

猜你喜欢

转载自blog.csdn.net/qq_41813695/article/details/81661934