hideScroll.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <div class="hide-scroll" ref="hide-scroll">
  3. <div class="scroll-content" ref="scroll">
  4. <div :style="`width:${width}px;`">
  5. <slot></slot>
  6. </div>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: "hideScroll",
  13. props: {
  14. itemHeight: {
  15. type: Number,
  16. default: 50
  17. }
  18. },
  19. data(){
  20. return {
  21. width:0,
  22. height: 0,
  23. timer: null,
  24. timeWait: 700,// 等待延迟
  25. }
  26. },
  27. mounted(){
  28. this.width = this.$refs["hide-scroll"].offsetWidth;
  29. this.height = this.$refs["hide-scroll"].offsetHeight;
  30. window.addEventListener('resize',()=>{
  31. this.width = this.$refs["hide-scroll"].offsetWidth;
  32. this.height = this.$refs["hide-scroll"].offsetHeight;
  33. })
  34. let scroll = this.$refs["scroll"]
  35. scroll.addEventListener('scroll',(e)=>{
  36. // console.log('scroll change')
  37. // console.log(scroll.scrollTop)
  38. if(this.timer){
  39. clearTimeout(this.timer);
  40. this.timer = null;
  41. }
  42. let scrollTop = scroll.scrollTop;
  43. let index = Math.floor(scrollTop/this.itemHeight);
  44. // 获取当前位置应该对应的元素index
  45. this.timer = setTimeout(()=>{
  46. this.$emit('onScroll', {
  47. scrollTop:scrollTop,
  48. index:index
  49. });
  50. },this.timeWait);
  51. })
  52. },
  53. methods:{
  54. }
  55. }
  56. </script>
  57. <style scoped >
  58. .hide-scroll{
  59. width: 100%;
  60. height: 100%;
  61. position: absolute;
  62. overflow: hidden;
  63. }
  64. .hide-scroll .scroll-content{
  65. width: calc(100% + 100px);
  66. height: 100%;
  67. overflow:auto;
  68. }
  69. .hide-scroll .scroll-content div{
  70. }
  71. </style>