Tips on implementing smooth-scroll scrollLeft in Vue3 with JavaScript

Currently, I am in the process of creating a horizontal scroll menu for displaying images. The navigation functionality includes arrow buttons for scrolling:

<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>

The setup displays two images at once, with the option to scroll left or right using the arrow buttons in order to reveal the third image. However, there is an issue with the smoothness of the scrolling animation.

I have attempted to address this by setting:

html: {scroll-behaviour: smooth}

and also experimenting with:

vue3-smooth-scroll

library, although it seems not to have the desired effect on the horizontal scrolling motion.

Is there a simple way within Vue3 to achieve the smooth scrolling? Any suggestions would be greatly appreciated!

Answer №1

After some experimentation, I discovered that rather than using scollLeft, it's more effective to utilize scrollTo which offers useful features:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on toggling the button by clicking on the icon to turn it on or off

Within my angular application, I have implemented font icons alongside a toggle switch. Initially, the switch is in the ON state. The specific functionality desired is for clicking on an icon to change its color from white to red (this has been achieved) ...

AngularJS filter that retrieves only values that have an exact match

In my AngularJS application, I have a checkbox that acts as a filter: Here is the data being used: app.controller('listdata', function($scope, $http) { $scope.users = [{ "name": "pravin", "queue": [{ "number": "456", "st ...

Client-side image upload problem in Next.js API routes

I've been banging my head against this bug for a couple of hours now and I just can't seem to figure out the reason behind it. The issue is with an API route I'm trying to set up in next.js where I need to modify an image and then upload it ...

Exploring the JSON data in Javascript using Ajax

Completely new to Javascript, I am just trying to grasp the basics of the language. Currently, I have a JSON request set up with the following code: function request(){ $.ajax({ dataType: "jsonp", type: 'GET', url: "getWebsite", ...

Issue with Vue Router not rendering components after dynamically importing them

I am facing an issue with vue-router while trying to register components dynamically. When I use hardcoded code, the router functions properly. However, when I load my routes dynamically, it fails to render the component. The component file is being impor ...

When working with Vue.js, toggling the 'readonly' attribute of a textarea between true and false may not be effective

My goal is to accomplish the following: Allow the textarea to be edited when double-clicked; Prevent the textarea from being edited when it loses focus; However, regardless of toggling the 'readonly' attribute, I am currently unable to achieve ...

Shift the item directly beneath the position of the mouse pointer

I'm currently working on positioning an object called 'tracer' under my mouse cursor. You can view my jsfiddle here. While researching, I came across this question, but I had difficulty applying it to my project. In my scene, I have a basi ...

Tips on avoiding the default behavior and then proceeding with the default action using VueJS

I am currently working on a form submission using VueJS and I have a specific requirement. I need to perform two actions sequentially upon final submit, one after the other. Each action works fine when executed individually. My goal is to first sign up a ...

"Using AngularJS to clear input fields and update the scope upon clicking with ng

1. Custom Directive app.directive('inputField', function() { return { restrict: 'E', require: 'ngModel', scope: { words: '=ngModel' }, ...

Modifying the label focus does not alter the CSS style

I'm struggling with a simple form where I want to highlight the focused labels by changing their background colors. However, the jquery code doesn't seem to be working as expected. Despite no errors showing up in the console, the functionality is ...

Implementing restrictions for each duplicated field using jQuery: A step-by-step guide

Is there a way to limit the number of words in each duplicated field using jQuery? I want to restrict users from entering more than 5 words per field. I have a snippet of code, but it's not quite working as expected. Can anyone provide some assistanc ...

The AngularJS $resource is taking the entire object and embedding it into the URL

I am currently utilizing Angular version 1.4.8 along with Angular UI and TypeScript in my project. The models are defined as follows: export interface IBatch extends ng.resource.IResource<IBatch> { id: Number; ... } export interface IBatchR ...

Can you explain the functionality of dismissing a Bootstrap alert?

In this example, we can see how Bootstrap handles dismissible alerts: <div class="alert alert-warning alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidde ...

Issue: ER_PARSE_ERROR: A mistake has been found in your SQL syntax;

I am having trouble inserting values using MySQL in Node.js. I wrote the code below and installed MySQL support via npm, but I am unable to INSERT INTO the table due to this issue. Here is my code: var mysql = require('mysql'); var values=rand ...

Does Google's web crawler have the ability to index content created by javascript?

Our web app generates content using javascript. Can Google index these pages? In our research on this issue, we primarily found solutions on older pages suggesting the use of "#!" in links. Within our app, the links are structured as follows: domain.co ...

Attempting to verify JavaScript input by utilizing OnClick and a custom function. Regardless of the content inputted, the system consistently confirms that the format is accurate

I am currently working on a project that involves an HTML file and an external JavaScript file. In the HTML file, there is user input and a validation button that triggers the courseValidation() function when clicked. However, I am facing an issue with the ...

Querying the children of an element using jQuery

$('.Title').children(function() { var titleValue = $('.Title').text(); console.log(titleValue); }); Wondering how to access all children with the 'title' tag and log their values individually. ...

How can we translate this php json_encode function into Node.js?

Seeking the equivalent Node.js code for this PHP Script: $SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS"; $SMAres ...

Tips for including images while drafting an article

In my current project, users are able to write articles. One feature of the editor is the ability to upload photos and include them in the article. While this functionality works well, there is an issue with how the photos are handled and stored. Each ar ...

Functions for abbreviating and combining strings in Javascript

Looking for help to simplify and shorten a Javascript function: $scope.doRefresh = function (){ if($scope.bulletpointPopular){ ArticleService.popular().then(function(data){ $scope.articles = data; }) .finally(function() { ...