index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <div class="better-scroll-container">
  3. <el-row :gutter="20">
  4. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  5. 动画时长
  6. <el-slider
  7. v-model="time"
  8. style="width: 300px;"
  9. :min="100"
  10. :max="3000"
  11. ></el-slider>
  12. <el-button @click="handleScrollTo(100)">滚动到100像素位置</el-button>
  13. <el-button @click="handleScrollTo(300)">滚动到300像素位置</el-button>
  14. <el-button @click="handleScrollBy(100)">向下滚动100像素</el-button>
  15. <el-button @click="handleScrollBy(-50)">向上滚动50像素</el-button>
  16. <el-button @click="handleScrollToElement(15)">滚动到第15个</el-button>
  17. <el-button @click="handleScrollToElement(25)">滚动到第25个</el-button>
  18. </el-col>
  19. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  20. <div ref="wrapper" class="right-content">
  21. <ul>
  22. <li v-for="n in 100" :id="`bs-item-${n}`" :key="n">n : {{ n }}</li>
  23. </ul>
  24. </div>
  25. </el-col>
  26. </el-row>
  27. </div>
  28. </template>
  29. <script>
  30. import BScroll from "better-scroll";
  31. export default {
  32. name: "BetterScroll",
  33. data() {
  34. return {
  35. time: 1000,
  36. BS: null,
  37. };
  38. },
  39. mounted() {
  40. this.scrollInit();
  41. },
  42. beforeDestroy() {
  43. this.scrollDestroy();
  44. },
  45. methods: {
  46. handleScrollTo(y) {
  47. this.BS.scrollTo(0, -y, this.time);
  48. },
  49. handleScrollBy(y) {
  50. this.BS.scrollBy(0, -y, this.time);
  51. },
  52. handleScrollToElement(n) {
  53. this.BS.scrollToElement(`#bs-item-${n}`, this.time);
  54. },
  55. scrollInit() {
  56. this.BS = new BScroll(this.$refs["wrapper"], {
  57. mouseWheel: true,
  58. scrollbar: {
  59. fade: true,
  60. interactive: false,
  61. },
  62. });
  63. },
  64. scrollDestroy() {
  65. if (this.BS) {
  66. this.BS.destroy();
  67. }
  68. },
  69. },
  70. };
  71. </script>
  72. <style lang="scss" scoped>
  73. .better-scroll-container {
  74. .right-content {
  75. height: 500px;
  76. margin-top: 40px;
  77. overflow: hidden;
  78. }
  79. }
  80. </style>