滚动指令

v-scroll 指令允许你在窗口或者特定定义的元素滚动时提供回调。

用例

默认行为是绑定到窗口。如果不需要其他配置选项,则可以简单的传递回调函数。

template script


  1. <template>
  2. <v-container>
  3. <v-row>
  4. <v-col cols="12">
  5. <h3
  6. ref="radio"
  7. class="headline"
  8. >
  9. Target
  10. </h3>
  11. <v-radio-group
  12. v-model="type"
  13. row
  14. >
  15. <v-radio
  16. label="Number"
  17. value="number"
  18. ></v-radio>
  19. <v-radio
  20. label="Selector"
  21. value="selector"
  22. ></v-radio>
  23. <v-radio
  24. label="DOMElement"
  25. value="element"
  26. ></v-radio>
  27. </v-radio-group>
  28. <v-text-field
  29. v-if="type === 'number'"
  30. v-model="number"
  31. type="number"
  32. label="Number"
  33. ></v-text-field>
  34. <v-text-field
  35. v-if="type === 'selector'"
  36. v-model="selector"
  37. label="Selector"
  38. ></v-text-field>
  39. <v-select
  40. v-if="type === 'element'"
  41. v-model="selected"
  42. :items="elements"
  43. label="DOMElement"
  44. ></v-select>
  45. </v-col>
  46. <v-col cols="12">
  47. <h3 class="headline">Options</h3>
  48. <v-select
  49. v-model="easing"
  50. :items="easings"
  51. label="Easing"
  52. ></v-select>
  53. <v-slider
  54. v-model="duration"
  55. min="0"
  56. max="1000"
  57. label="Duration"
  58. thumb-label
  59. ></v-slider>
  60. <v-slider
  61. v-model="offset"
  62. min="-500"
  63. max="500"
  64. label="Offset"
  65. thumb-label
  66. ></v-slider>
  67. </v-col>
  68. <v-col>
  69. <v-btn
  70. ref="button"
  71. block
  72. color="primary"
  73. @click="$vuetify.goTo(target, options)"
  74. >
  75. scroll
  76. </v-btn>
  77. </v-col>
  78. </v-row>
  79. </v-container>
  80. </template>
  1. <script>
  2. import * as easings from 'vuetify/es5/services/goto/easing-patterns'
  3. export default {
  4. data () {
  5. return {
  6. type: 'number',
  7. number: 9999,
  8. selector: '#scroll-with-options',
  9. selected: 'Button',
  10. elements: ['Button', 'Radio group'],
  11. duration: 300,
  12. offset: 0,
  13. easing: 'easeInOutCubic',
  14. easings: Object.keys(easings),
  15. }
  16. },
  17. computed: {
  18. target () {
  19. const value = this[this.type]
  20. if (!isNaN(value)) return Number(value)
  21. else return value
  22. },
  23. options () {
  24. return {
  25. duration: this.duration,
  26. offset: this.offset,
  27. easing: this.easing,
  28. }
  29. },
  30. element () {
  31. if (this.selected === 'Button') return this.$refs.button
  32. else if (this.selected === 'Radio group') return this.$refs.radio
  33. },
  34. },
  35. }
  36. </script>

Scrolling(滚动) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Scrolling(滚动) - 图2

示例

下面是一些简单到复杂的例子。

滚动选项

为了更好地调整方法,您可以给指定目标绑定滚动事件监听器。

template script


  1. <template>
  2. <div>
  3. <v-row justify="center" align="center">
  4. <v-subheader>Offset Top</v-subheader>
  5. {{ offsetTop }}
  6. </v-row>
  7. <v-container
  8. id="scroll-target"
  9. style="max-height: 400px"
  10. class="overflow-y-auto"
  11. >
  12. <v-row
  13. v-scroll:#scroll-target="onScroll"
  14. align="center"
  15. justify="center"
  16. style="height: 1000px"
  17. >
  18. </v-row>
  19. </v-container>
  20. </div>
  21. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. offsetTop: 0,
  5. }),
  6. methods: {
  7. onScroll (e) {
  8. this.offsetTop = e.target.scrollTop
  9. },
  10. },
  11. }
  12. </script>

Scrolling(滚动) - 图3