سؤال

may I ask you? How do I set negative margin (or anything else) in .css() method in jQuery? I am adding the size by variable, but it doesn´t works

var width = $('.gallery div img').outerWidth(true);
$('.gallery').css('margin-left', '- ' + width + 'px');

Or is there any way how to convert var width into negative number?

هل كانت مفيدة؟

المحلول

The following worked for me on this StackOverflow page:

$(".bottom-notice").css("margin-left", "-"+500+"px");

Notice there's no space between the - and the closing ". I think that might be what is causing your issue. Otherwise make sure width is actually a number.

نصائح أخرى

Check width first to make sure its a number, and then take the space out and the true.

var mywidth = $('.gallery div img').outerWidth();
alert(mywidth);
$('.gallery').css('margin-left', '-' + mywidth + 'px');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top