Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: $(window).resize() not working

I'm trying to manually fire the $(window).resize() method on page load. I created a function to center a div element vertically and horizontally on the page

$(window).resize(function(){
    $('.pagecentered').css({
        position: 'absolute',
        left: ($(window).width() - $('.pagecentered').outerWidth())/2,
        top: ($(window).height() - $('.pagecentered').outerHeight())/2
    });
});

After this function I call

$(window).resize();

to fire the resize() event manually.

Unfortunately it doesn't work. On reload the div is not centered as expected. If I resize the browser window the div element is centered it only isn't fired on page load.

Is there any way to solve this? I'm using Chrome 27.0.1453.116 m (latest version atm) and JQuery 1.10.2

like image 480
TorbenJ Avatar asked Nov 03 '22 19:11

TorbenJ


1 Answers

You need to use trigger : $(window).trigger('resize');

like image 147
sdespont Avatar answered Dec 16 '22 13:12

sdespont