Reputation: 3801
Can anyone help, there are similar questions already but none quite satisfy my issue. I am building a parallax site http://www.bettondesignwork.co.uk/tim/Cayton2 and for the animations all divs and elements have to be absolutely positioned. This causes issues in different window sizes. Is there a way to set a div to be center horizontally using half the window with - half the div width? I have this already but its not quite working
var h = $(window).height();
var w = $(window).width();
var shoptw = $('#shoptitle').css('height');
$('#shoptitle') .css({'left': ((w/2) - (shoptw/2) + "px")
});
Thanks
Upvotes: 0
Views: 166
Reputation: 7971
use parseInt
method. This method convert sting to integer
var h = $(window).height();
var w = $(window).width();
var shoptw = parseInt($('#shoptitle').css('height'));
$('#shoptitle') .css({'left': ((w/2) - (shoptw/2) + "px")
});
Upvotes: 0
Reputation: 16585
This should do what you are after:
var h = $(window).height();
var w = $(window).width();
var shopth = $('#shoptitle').height();
var shoptw = $('#shoptitle').width();
$('#shoptitle') .css({'left': (w/2) - (shoptw/2), 'top': (h/2) - (shopth/2)});
Upvotes: 1