简体   繁体   English

如何在 Vue3 中平滑滚动 javascript scrollLeft?

[英]How to smooth-scroll javascript scrollLeft in Vue3?

I am building a horizontal scroll menu for some images.我正在为一些图像构建一个水平滚动菜单。 The navigation has to work with arrow buttons:导航必须使用箭头按钮:

<template>
  <section id="artwork" class="artwork">
    <div class="arrow left" @click="scrollLeft"></div>
    <div class="artwork-container">
      <img :src="state.image1" class="image">
      <img :src="state.image2" class="image">
      <img :src="state.image3" class="image">
    </div>
    <div class="arrow right" @click="scrollRight"></div>
  </section>
</template>

<script setup>
const scrollLeft = (() => {
    document.querySelector('.artwork-container').scrollLeft -= 500
})
const scrollRight = (() => {
    document.querySelector('.artwork-container').scrollLeft += 500
})
</script>

2 images are displayed at any time with the arrow buttons to enable scrolling right/left to show the third image.使用箭头按钮可随时显示 2 张图像,以启用向右/向左滚动以显示第三张图像。 Problem is that the scrolling is not smooth.问题是滚动不流畅。

I've tried setting:我试过设置:

html: {scroll-behaviour: smooth}

or using:或使用:

vue3-smooth-scroll

library but it doesn't affect horizontal scroll it seems.库,但它似乎不影响水平滚动。

How do I get it smooth?我怎样才能让它顺利? Maybe Vue3 provides a simple way?也许Vue3提供了一种简单的方法?

I found that instead of scollLeft you can use scrollTo which brings some handy options:我发现你可以使用scrollTo而不是scollLeft ,它带来了一些方便的选项:

const scrollRight = (() => {
    document.querySelector('.artwork-container').scrollTo({
        left: 500,
        behavior: 'smooth'
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM