Animate with jQuery

248
November 21, 2016, at 10:48 PM

I am trying to animate the "margin-left" via "animate()" jQuery, where the value of "margin-left" is saved in a variable dynamically and not hard-coded as in

$('.animate-box').animate({'margin-left': '+=60'}) // value is not hard-coded like this

but rather like this so that the value can be dynamically changed.

$('.animate-box').animate({'margin-left': +=margin}// like this

But I could not get it animated for some reason. Fiddle to the Demonstration

Thank you.

Answer 1

Two issues:

  1. You left out a period in your selector. It should be var margin = $('.top-box').width();

  2. Second you need to enclose the += in quotes and concatenate it with the value. 'margin-left': '+=' + margin

jsFiddle example

READ ALSO
jQuery AJAX behaving differently with normal form & call in a div based popup

jQuery AJAX behaving differently with normal form & call in a div based popup

When submitting the form based on normal method it execute jQuery AJAX perfectly. When submitting the same form used within the popup the jQuery AJAX is not executed.

228
Unable to load boot strap modal using jquery post method

Unable to load boot strap modal using jquery post method

I have one main jsp page, here I have 6 inputs, on click of each input I'm having a jquery onclickfunction and here I'm using jquery post method to load a jsp page in which I'm calling db function to get data and then show this as boot strap modal. .

211
jqGrid - checkbox editing not able to edit selected row

jqGrid - checkbox editing not able to edit selected row

In my jqGrid, I have a checkbox which is also available for editing, i. e.

692
Accessing parent element in javascript without creating handler

Accessing parent element in javascript without creating handler

Is it posible to access the variable defined in parent function without creating the handler for it ?.

235