back-to-top.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // [z-paging]点击返回顶部view模块
  2. import u from '.././z-paging-utils'
  3. export default {
  4. props: {
  5. //自动显示点击返回顶部按钮,默认为否
  6. autoShowBackToTop: {
  7. type: Boolean,
  8. default: u.gc('autoShowBackToTop', false)
  9. },
  10. //点击返回顶部按钮显示/隐藏的阈值(滚动距离),单位为px,默认为400rpx
  11. backToTopThreshold: {
  12. type: [Number, String],
  13. default: u.gc('backToTopThreshold', '400rpx')
  14. },
  15. //点击返回顶部按钮的自定义图片地址,默认使用z-paging内置的图片
  16. backToTopImg: {
  17. type: String,
  18. default: u.gc('backToTopImg', '')
  19. },
  20. //点击返回顶部按钮返回到顶部时是否展示过渡动画,默认为是
  21. backToTopWithAnimate: {
  22. type: Boolean,
  23. default: u.gc('backToTopWithAnimate', true)
  24. },
  25. //点击返回顶部按钮与底部的距离,注意添加单位px或rpx,默认为160rpx
  26. backToTopBottom: {
  27. type: [Number, String],
  28. default: u.gc('backToTopBottom', '160rpx')
  29. },
  30. //点击返回顶部按钮的自定义样式
  31. backToTopStyle: {
  32. type: Object,
  33. default: function() {
  34. return u.gc('backToTopStyle', {});
  35. },
  36. },
  37. //iOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向,默认为是
  38. enableBackToTop: {
  39. type: Boolean,
  40. default: u.gc('enableBackToTop', true)
  41. },
  42. },
  43. data() {
  44. return {
  45. backToTopClass: 'zp-back-to-top zp-back-to-top-hide',
  46. lastBackToTopShowTime: 0,
  47. showBackToTopClass: false,
  48. }
  49. },
  50. computed: {
  51. finalEnableBackToTop() {
  52. return this.usePageScroll ? false : this.enableBackToTop;
  53. },
  54. finalBackToTopThreshold() {
  55. return u.convertTextToPx(this.backToTopThreshold);
  56. },
  57. finalBackToTopStyle() {
  58. const backToTopStyle = this.backToTopStyle;
  59. if (!backToTopStyle.bottom) {
  60. backToTopStyle.bottom = this.windowBottom + u.convertTextToPx(this.backToTopBottom) + 'px';
  61. }
  62. if(!backToTopStyle.position){
  63. backToTopStyle.position = this.usePageScroll ? 'fixed': 'absolute';
  64. }
  65. return backToTopStyle;
  66. },
  67. },
  68. methods: {
  69. //点击返回顶部
  70. _backToTopClick() {
  71. !this.backToTopWithAnimate && this._checkShouldShowBackToTop(0);
  72. this.scrollToTop(this.backToTopWithAnimate);
  73. },
  74. //判断是否要显示返回顶部按钮
  75. _checkShouldShowBackToTop(scrollTop) {
  76. if (!this.autoShowBackToTop) {
  77. this.showBackToTopClass = false;
  78. return;
  79. }
  80. if (scrollTop > this.finalBackToTopThreshold) {
  81. if (!this.showBackToTopClass) {
  82. this.showBackToTopClass = true;
  83. this.lastBackToTopShowTime = new Date().getTime();
  84. setTimeout(() => {
  85. this.backToTopClass = 'zp-back-to-top zp-back-to-top-show';
  86. }, 300)
  87. }
  88. } else {
  89. if (this.showBackToTopClass) {
  90. this.backToTopClass = 'zp-back-to-top zp-back-to-top-hide';
  91. setTimeout(() => {
  92. this.showBackToTopClass = false;
  93. }, new Date().getTime() - this.lastBackToTopShowTime < 500 ? 0 : 300)
  94. }
  95. }
  96. },
  97. }
  98. }