Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
How to replace margin-left animation with transform in Jquery animate() to fix laggy multislider

I'll preface this by saying my initial problem is difficult to reproduce. Brief explanation of my problem following, question is at the bottom.

So I am using the Jquery multislider for a project. Here is a link to it: Multislider

Now my issue is that the animation of the moving elements seems to lag... Sometimes. It jumps instead of moving smoothly. The way the element works is by applying the animate() method to the first item and applies an inline margin-left property to the first .item

With some research I have found that CSS animations often cause problems when margins are used for the animation(among some other properties like top/bottom/left/right, as well as height/width) and that using transform is preferable. So far so good.

This is the snippet in the javascript that creates the animation:

function singleLeft(){
        isItAnimating(function(){
            reTargetSlides();
            $imgFirst.animate(
                {
                    marginLeft: -animateDistance /* This is the part that causes me problems */
                }, {
                    duration: animateDuration,
                    easing: "swing",
                    complete: function(){
                        $imgFirst.detach().removeAttr('style').appendTo($msContent);
                        doneAnimating();
                    }
                }
            );
        });
    }

    function singleRight(){
        isItAnimating(function(){
            reTargetSlides();
            $imgLast.css('margin-left',-animateDistance).prependTo($msContent);
            $imgLast.animate(
                {
                    marginLeft: 0
                }, {
                    duration: animateDuration,
                    easing: "swing",
                    complete: function(){
                        $imgLast.removeAttr("style");
                        doneAnimating();
                    }
                }
            );
        });
    } 

Now if I understand it correctly, I have to replace the marginLeft: -animateDistance portion with a transformX property, is that correct? But I am failing to make it work.

So my question is, how can I replace the marginLeft: -animateDistance portion with transform: translateX() and add the animateDistance variable between the parentheses?

I have tried something like transform: "translateX(-$(animateDistance))", but that just disables the animation entirely.
Am I missing something? I'm open for other suggestions to solve the issue of the laggy animation as well, this just is the conclusion I came to.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use animate with $(this) and .css() if you use step()

let test = "100";

$('div h2').animate({ pxNumber: test }, {
    step: function(pxNumber) {
      $(this).css('transform','translateX(-' + pxNumber + 'px )'); 
    },
    duration:'slow',
    easing: "swing",
    complete: function(){
      console.log('Animation done');
       // doneAnimating();
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div><h2>Move it</h2></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!