What methods can I utilize to increase the speed of my JavaScript animation?

Recently, I implemented a Vue component designed to loop a div over the X axis. While it functions correctly, I can't help but notice that it is consuming an excessive amount of CPU. I am interested in optimizing this animation for better performance. The galleryItems are supposed to move along the x-axis and when they reach the width of their parent div, they should disappear from one side and reappear on the other side to continue the loop seamlessly. Ideally, I would like to achieve this optimization without relying on external plugins.

<template>
    <div class="placeholder">
      <div class="gallery" ref="gallery">
          <div class="gallery-item" v-for="i in 10" :key="i" ref="galleryItems" />
      </div>
    </div>
</template>

<script>
export default {
    props:{
      xoffset : {
        type: Number,
        default: 0,
      },
      speed : {
        type: Number,
        default: 1,
      },
      spaceBetween : {
        type: Number,
        default: 50,
      },
    },
    data() {
      return {
      }
    },
    mounted(){
      for(let i = 0; i < this.$refs.galleryItems.length; i++)
        this.$refs.galleryItems[i].style.transform = 'translateX(' + (this.$refs.galleryItems[i].clientWidth + this.spaceBetween) * i + 'px)';
      this.animate();
    },
    methods:{
      animate(){
        window.requestAnimationFrame(this.animate);
        for(let i = 0; i < this.$refs.galleryItems.length; i++)
        {
          let galleryItemRect = this.$refs.galleryItems[i].getBoundingClientRect();
          if(galleryItemRect.x > this.$refs.gallery.clientWidth)
          {
            this.$refs.galleryItems[i].style.transform = 'translateX(' + -galleryItemRect.width + 'px)';
            continue;
          }

          this.$refs.galleryItems[i].style.transform = 'translateX(' + (galleryItemRect.x + this.speed) + 'px)';
        }
      }
    }
}
</script>

<style lang="scss" scoped>
.placeholder{
  height: 100vh;
  display: flex;
  align-items: center;
}

.gallery
{
  position: relative;
  background-color: green;
  height: 100px;
  width: 100%;
  overflow: hidden;
}

.gallery-item{
  display: block;
  position: absolute;
  background-color: black;
  height: inherit;
  min-width: 100px;
  transition: 0s;
}
</style>

Answer №1

Consider utilizing an external plugin for improved performance and quality. In vue.js, explore framework options such as v-animate and transition. Check out the link provided below for more information:

https://v2.vuejs.org/v2/guide/transitions.html

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

What is the process for incorporating Transformer instances into the buildVideoUrl() function using cloudinary-build-url?

This particular package is quite impressive, however, it seems to lack built-in support for looping gifs. Fortunately, the provided link demonstrates how custom URL sections like "e_loop" can be created. One challenge I'm facing is figuring out how t ...

inject custom styles into a Material-UI styled component

Although I have come across similar questions, none seem to directly address my current situation. I am in the process of transitioning from MUI v4 to MUI v5 and have encountered various scenarios where a specific style is applied externally. For instance ...

Transmit and receive information between Javascript and Node.js through Express framework

Currently, I am utilizing the Express platform along with the Twilio Node.js SMS API and JavaScript to send text messages to my users. However, I am facing an issue in sending data through GET variables on the front-end and capturing those values with node ...

What is the best way to utilize d3.domain for extracting both d3.min and d3.max values from various columns within a JSON dataset?

I have a JSON file that contains data from different years for "male," "female," and "both" categories. I want to set the minimum value in my domain to be the lowest value across all three columns and do the same for the maximum value. Is there a way for ...

The :empty selector is failing to function properly in @media print queries

Currently, I am working in Twig on Symphony and have encountered an issue with an empty div that needs to be hidden in print mode. <div class="form-fields__list"> </div> Surprisingly, the :empty selector works perfectly fine on the screen, bu ...

What is the proper file format for a form action in CSS?

What I Currently Have: In my Index.html file, there are a total of 4 form elements including text fields and a dropdown. Upon submission by the user, the data is processed in confirm.html using a separate JavaScript file for formatting before being displa ...

Various text sizes within a nested HTML list structure

I've developed a nested CSS class for an ordered list on my website, but I'm encountering a problem where each list item is appearing in different font sizes even though I have specified the font size. .number_list ol { font:normal 1.2em ...

Ways to remove the default classes added by ngbootstrap

My current challenge involves utilizing ngbootstrap for popovers, yet desiring to override its default styling. Specifically, I have a form that should function as a popover triggered by a button click, and it needs to maintain its own distinct styles. Up ...

Using Vue to input an array

I'm struggling with how to handle this issue. The task involves taking input which should be a URL, and the user should be able to enter multiple URLs. For instance: <input type="text" v-model="fields.urls" class=&quo ...

What is the proper way to disable asynchronous behavior in JavaScript?

Can someone provide assistance on how to make the following jQuery ajax function asynchronous in this code? $.post(base_url+"search/questionBox/"+finalTopic+"/"+finalCountry+'/'+findSearchText+'/'+ID,function(data){ if (data != "") { ...

"Encountering an 'Undefined function' error while implementing AJAX in the code

I'm encountering the issue Uncaught ReferenceError: GetLicenceUserList is not defined in the browser console when I utilize the function with $.ajax inside. However, the function works perfectly fine when I invoke it with just an alert("example& ...

Interactive Navigation Bar in JavaScript

Is there a way to create a horizontal menu with an arrow that follows the mouse cursor? I saw this effect on a website (http://cartubank.ge) and I'm curious if anyone has the source code for it? Your help would be greatly appreciated. ...

Access an HTML page programmatically using JavaScript

Make sure to have a confirmation window pop up before submitting the form, and after confirming submission (by clicking OK), redirect to a confirmation page. Here's an example code snippet: <button type="submit" value="Save" id="Save" onclick="cl ...

Trying to configure and use two joysticks at the same time using touch events

I have been grappling with this problem for the past two days. My current project involves using an HTML5/JS game engine called ImpactJS, and I came across a helpful plugin designed to create joystick touch zones for mobile devices. The basic concept is th ...

Error: The function semrush.backlinks_refdomains does not exist as a valid function

Hey there! So I've been working with the SEMRUSH API and encountered an issue when trying to retrieve data using backlinks_refdomains and backlinks_refips. However, when I called the domain_rank function, it responded in JSON format without any proble ...

The function you are trying to use is not a valid jQuery function

Although I've come across this issue in previous posts, none of the solutions worked for me and it's really frustrating. I'm a newbie to javascript and I'm sure the answer is probably simple. I'm attempting to incorporate the rapt ...

Ways to authenticate various fields with identical names in vue.js

I am facing an issue where input fields with the same name are displaying errors when one of them is invalid. Is there a better way to handle this situation? <tr v-for="(form, index) in forms" :key="index"> ...

What is the syntax for creating a link tag with interpolation in Angular 2 / Ionic 2?

As I work on developing an app using Ionic 2/Angular 2, I have encountered a challenge that I am struggling to overcome. Let me provide some context: I am retrieving multiple strings from a webservice, and some of these strings contain links. Here is an e ...

Encountering difficulties while trying to access the SQLite database file through a JavaScript Axios GET request

Having trouble opening an sqlite DB file from a js axios.get request which is resulting in an exception message being outputted to the console. The request is supposed to call my PHP controller to retrieve data from the DB and return it json-encoded. On t ...

Determine the exact location where the mouse was clicked on a webpage containing an iframe

I am trying to retrieve the mouse position on my HTML page, but I am encountering some issues. Here's what I have so far: document.onclick = function(click) { if (click.clientX<340){ myControl.inputs.selection = false; btnRotate.remove ...