0

I wanna move content with animation. There's an animation in the jquery function, like that:

.on('click', '.btn', function () {
  var contentWidth = $(window).width() - $('.sidebar').width();
  $( ".content" ).animate({
      left: "+=1000",
    }, 5000, function() {
      // moving content function
    }
  );
});

So, because I have various positions of content, I need to use this value in the variable contentWidth. How I can set this value in a variable and decrement the left value every time when I call a function? Something like left: "+=contentWidth" doesn't work. Is where I make mistake?

1
  • I cannot use a fixed value like 1000, because, this value is permanently changing depending on the window width. So I need to calculate it, set in variable and then decrement. Something like, left: "+=contentWidth", but it doesn't work. Avoid using integer values in your answer, please. Thank you in advance! Probably, I formulated it unclear, firstly. Commented Jul 22, 2022 at 14:51

1 Answer 1

1

Not sure if i got it but...

maybe you could use string interpolation

.on('click', '.btn', function () {
   var contentWidth = $(window).width() - $('.sidebar').width();
     $( ".content" ).animate({
       left: `+=${contentWith}`,
     }, 5000, function() {
     // moving content function
   }
 );

});

Not tested but should work theoretically

1
  • everything works, thank you Commented Jul 22, 2022 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.