Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top