index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <view class="" :style="{
  7. display: !top ? 'block' : 'none'
  8. }">
  9. <view class="rect-block-1">第一个节点</view>
  10. <view class="rect-block-2">第2个节点</view>
  11. <view class="u-no-demo-here">节点信息为</view>
  12. <view class="u-demo-result-line">{{ JSON.stringify(result) }}</view>
  13. </view>
  14. <view class="jump-to-top">
  15. <u-button @click="scrollToTop" :style="{
  16. display: top ? 'block' : 'none'
  17. }">
  18. 点我自动滚动到顶部
  19. </u-button>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="u-config-wrap">
  24. <view class="u-config-title u-border-bottom">参数配置</view>
  25. <view class="u-config-item">
  26. <view class="u-item-title">元素</view>
  27. <u-subsection :list="['第一个节点', '第2个节点']" @change="elChange"></u-subsection>
  28. </view>
  29. <view class="u-config-item">
  30. <view class="u-item-title">指定元素置顶</view>
  31. <u-subsection current="1" :list="['是', '否']" @change="topChange"></u-subsection>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. result: '',
  41. scrollTop: 0,
  42. top: false
  43. };
  44. },
  45. onReady() {
  46. this.elChange(0);
  47. },
  48. onPageScroll(e) {
  49. this.scrollTop = e.scrollTop;
  50. },
  51. methods: {
  52. async elChange(index) {
  53. let el = index == 0 ? '.rect-block-1' : '.rect-block-2';
  54. this.result = await this.$u.getRect(el);
  55. },
  56. scrollToTop() {
  57. this.$u.getRect('.jump-to-top').then(res => {
  58. uni.pageScrollTo({
  59. scrollTop: this.scrollTop + res.top,
  60. duration: 0
  61. });
  62. });
  63. },
  64. topChange(index) {
  65. this.top = index == 0 ? true : false;
  66. if(index == 1) {
  67. uni.pageScrollTo({
  68. scrollTop: 0,
  69. duration: 0
  70. });
  71. }
  72. }
  73. }
  74. };
  75. </script>
  76. <style lang="scss" scoped>
  77. .u-demo {
  78. min-height: 200vh;
  79. }
  80. .rect-block-1 {
  81. background-color: #a0cfff;
  82. padding: 26rpx 60rpx;
  83. color: #ffffff;
  84. display: inline-flex;
  85. margin: auto;
  86. }
  87. .rect-block-2 {
  88. background-color: #fcbd71;
  89. padding: 12rpx 8rpx;
  90. width: 60%;
  91. color: #ffffff;
  92. margin: 30rpx auto;
  93. }
  94. </style>