0

I am trying to achieve this effect using jQuery and it has been giving me a bit of trouble:

I would like a css attribute value to have the same value as another css attribute value multiplied by 2.

In more detail, I would like a margin-left value which is set as a percentage to have the same value as a margin-top value multiplied by 2.

I would appreciate any help.

4
  • 1
    I think you should read about LESS CSS.. That will make it possible to handle variables and much more.. :) Commented Sep 27, 2013 at 0:16
  • Also, check out CSS calc() developer.mozilla.org/en-US/docs/Web/CSS/calc Commented Sep 27, 2013 at 0:22
  • Is margin-top set in pixels or percentage as well? Commented Sep 27, 2013 at 0:33
  • LESS or SASS is the way to go! Commented Sep 27, 2013 at 0:41

1 Answer 1

1

Like denlau says, you should use LESS, or like JoshC says, use the CSS3 property calc(). But in jQuery, you could use:

var element = $('#element'),
  marginTop = parseFloat(element.css('marginTop'));

element.css('marginLeft', 2*marginTop);

Check the jsfiddle example.

Sign up to request clarification or add additional context in comments.

6 Comments

Not exactly... that would change the style attached to #element, but wouldn't change the css value of the underlying style.
You want to edit in CSS, not in jQuery so. For this, you could use LESS or calc().
Who are you talking to? If you type here, I'm the only one getting notified.
Sorry, I thought it was user2812034 who was commenting. But I didn't understand your comment. I think is the approach he is seeking.
I mean if you .css an element, the styles will be attached to the element. The actual style (e.g. #element { something }) is not changed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.