美文网首页
el-scrollbar鼠标横向滚动

el-scrollbar鼠标横向滚动

作者: 生于乱世 | 来源:发表于2023-02-22 10:33 被阅读0次

    vue2

    //html
    <el-scrollbar ref="scrollContainer" :vertical="false" class="scroll-container" @wheel.native.prevent="handleScroll">
        <slot />
      </el-scrollbar>
    //js
    computed: {
        scrollWrapper() {
          return this.$refs.scrollContainer.$refs.wrap
        }
      },
    
    handleScroll(e) {
          const eventDelta = e.wheelDelta || -e.deltaY * 40
          const $scrollWrapper = this.scrollWrapper
          $scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4
        },
    

    vue3

    //html
    <el-scrollbar ref="scrollContainer" style="height: 35px" @wheel.native.prevent="handleScroll">
       <slot />
    </el-scrollbar>
    
    //js
    const scrollContainer = ref()
    
    const scrollWrapper = computed(() => {
     return scrollContainer.value.wrapRef
    })
    
    function handleScroll(e:any) {
     const eventDelta = e.wheelDelta || -e.deltaY * 40
     const $scrollWrapper = scrollWrapper.value
     $scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4
    }
    
    //css
    .el-scrollbar__wrap{
     overflow-x: auto;
     height: calc(100% + 20px); //多出来的20px是横向滚动条默认的样式
    }
    .el-scrollbar .el-scrollbar__wrap .el-scrollbar__view{
     white-space: nowrap;
     display: inline-block;
    }
    

    相关文章

      网友评论

          本文标题:el-scrollbar鼠标横向滚动

          本文链接:https://www.haomeiwen.com/subject/fahakdtx.html