经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue实现原生下拉刷新
来源:jb51  时间:2022/3/8 10:55:28  对本文有异议

本文实例为大家分享了vue实现原生下拉刷新的具体代码,供大家参考,具体内容如下

这是动画样式 

 文字样式

html代码

  1. <template>
  2. ? <div class="car-box">
  3. ? ? <div class="car">下拉刷新</div>
  4. ? ? <div class="box" @touchstart="fnstart" ref="Element">
  5. ? ? ? <div class="con-txt">上拉刷新</div>
  6. ? ? </div>
  7. ? </div>
  8. </template>

js代码

  1. <script>
  2. export default {
  3. ? methods: {
  4. ? ? fnstart(ev) {
  5. ? ? ? this.$refs["Element"].style.top = 0;
  6. ? ? ? this.scroolTop =
  7. ? ? ? ? ev.changedTouches[0].pageY - this.$refs["Element"].offsetTop;
  8. ?
  9. ? ? ? document.ontouchmove = this.fnmove;
  10. ? ? ? document.ontouchend = this.fnEnd;
  11. ? ? ? ev.preventDefault && ev.preventDefault();
  12. ? ? },
  13. ? ? fnmove(ev) {
  14. ? ? ? ev.target.parentNode.children[0].innerHTML = "下拉刷新";
  15. ? ? ? this.T = ev.changedTouches[0].pageY - this.scroolTop;
  16. ? ? ? if (this.scale > 0.12) {
  17. ? ? ? ? this.scale = 1 - this.T / 200;
  18. ? ? ? } else {
  19. ? ? ? ? this.scale = 0.12;
  20. ? ? ? }
  21. ? ? ? this.$refs["Element"].style.top = this.T * this.scale + "px";
  22. ? ? },
  23. ? ? fnEnd(ev) {
  24. ? ? ? ev.target.parentNode.children[0].innerHTML = "正在刷新...";
  25. ? ? ? document.ontouchmove = null;
  26. ? ? ? document.ontouchend = null;
  27. ? ? ? setTimeout(() => {
  28. ? ? ? ? this.$refs["Element"].style.top = 0;
  29. ? ? ? ? this.$refs["Element"].style.transition = ".3s ease all";
  30. ? ? ? ? this.$refs["Element"].addEventListener("transitionend", () => {
  31. ? ? ? ? ? this.$refs["Element"].style.transition = null;
  32. ? ? ? ? });
  33. ? ? ? }, 3000);
  34. ? ? },
  35. ? },
  36. };
  37. </script>

css代码  我这边用的是scss 

  1. <style lang="scss" scoped>
  2. .box {
  3. ? text-align: center;
  4. ? height: 600px;
  5. ? width: 100vw;
  6. ? background-color: orange;
  7. ? position: absolute;
  8. ? left: 0;
  9. ? top: 0;
  10. }
  11. .car {
  12. ? text-align: center;
  13. ? margin: auto;
  14. ? width: 199px;
  15. ? height: 60px;
  16. ? line-height: 60px;
  17. ? background-position: 0 0;
  18. ? background-size: 100% auto;
  19. ? animation: animation_car 1.5s steps(1) infinite;
  20. }
  21. </style>

如果下拉刷新用动画就用这个css样式 

图片的话我用的是28帧的 根据100除以28  也就是3.5 ,所以每3.5%换一个图,就能形成一个逐帧动画,每一个页面的宽高都不一样所以要计算 ,我的页面的大小是1080的这个也时我设置好的宽高。

这是代码

  1. <style lang="scss" scoped>
  2. .box {
  3. ? text-align: center;
  4. ? height: 600px;
  5. ? width: 100vw;
  6. ? background-color: orange;
  7. ? position: absolute;
  8. ? left: 0;
  9. ? top: 0;
  10. }
  11. .car {
  12. ? text-align: center;
  13. ? margin: auto;
  14. ? width: 199px;
  15. ? height: 134px;
  16. ? // ? margin-bottom: 200px;
  17. ? background: url("../assets/img/car.png") no-repeat;
  18. ? background-position: 0 0;
  19. ? background-size: 100% auto;
  20. ? animation: animation_car 1.5s steps(1) infinite;
  21. }
  22. @keyframes animation_car {
  23. ? 0% {
  24. ? ? background-position: 0px;
  25. ? }
  26. ? 3.5% {
  27. ? ? background-position: 0px -134px;
  28. ? }
  29. ? 7% {
  30. ? ? background-position: 0px -268px;
  31. ? }
  32. ? 10.5% {
  33. ? ? background-position: 0px -402px;
  34. ? }
  35. ? 14% {
  36. ? ? background-position: 0px -536px;
  37. ? }
  38. ? 17.5% {
  39. ? ? background-position: 0px -670px;
  40. ? }
  41. ? 21% {
  42. ? ? background-position: 0px -804px;
  43. ? }
  44. ? 24.5% {
  45. ? ? background-position: 0px -938px;
  46. ? }
  47. ? 28% {
  48. ? ? background-position: 0px -1072px;
  49. ? }
  50. ? 31.5% {
  51. ? ? background-position: 0px -1206px;
  52. ? }
  53. ? 35% {
  54. ? ? background-position: 0px -1340px;
  55. ? }
  56. ? 38.5% {
  57. ? ? background-position: 0px -1474px;
  58. ? }
  59. ? 42% {
  60. ? ? background-position: 0px -1608px;
  61. ? }
  62. ? 45.5% {
  63. ? ? background-position: 0px -1742px;
  64. ? }
  65. ? 49% {
  66. ? ? background-position: 0px -1876px;
  67. ? }
  68. ? 52.5% {
  69. ? ? background-position: 0px -2010px;
  70. ? }
  71. ? 56% {
  72. ? ? background-position: 0px -2144px;
  73. ? }
  74. ? 59.5% {
  75. ? ? background-position: 0px -2278px;
  76. ? }
  77. ? 63% {
  78. ? ? background-position: 0px -2412px;
  79. ? }
  80. ? 66.5% {
  81. ? ? background-position: 0px -2546px;
  82. ? }
  83. ? 70% {
  84. ? ? background-position: 0px -2680px;
  85. ? }
  86. ? 73.5% {
  87. ? ? background-position: 0px -2814px;
  88. ? }
  89. ? 77% {
  90. ? ? background-position: 0px -2948px;
  91. ? }
  92. ? 80.5% {
  93. ? ? background-position: 0px -3082px;
  94. ? }
  95. ? 84% {
  96. ? ? background-position: 0px -3216px;
  97. ? }
  98. ? 87.5% {
  99. ? ? background-position: 0px -3350px;
  100. ? }
  101. ? 91% {
  102. ? ? background-position: 0px -3350px;
  103. ? }
  104. ? 94.5% {
  105. ? ? background-position: 0px -3484px;
  106. ? }
  107. ? 98% {
  108. ? ? background-position: 0px -3618px;
  109. ? }
  110. }
  111. ?
  112. </style>

图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号