0

I am hoping that I can get solution to my problem - I have been trying to work on this for some time and cannot figure out where the problem is. For good measure, here is the code;

$(window).resize(function(){
var windowWidth = $(window).width();
var sec1LogoWidth = $('#sec1-logo').width();     

    if (windowWidth == sec1LogoWidth) {
            $('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))) + 'px','left': '0' + '!important' + 'px', 'margin-left': 10 + 'px', 'margin-right': 10 + 'px'});
        } else {
            $('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))) - 100 + 'px','left': Math.abs((windowWidth / 2) - (sec1LogoWidthImg-(Math.ceil(sec1LogoWidthImg/2)))) + 'px'});
        };     
};

Basically what I am doing here is ensuring that the logo is always center on the page (top and left) however IF the logo width and window width are equal, then I want it to put left at 0 px...and leave it at 0 px. The issue is, once left position gets to 0px it then starts to ADD left positioning past 0px. I have no idea how to stop this from happening. Any suggestions? Thank you,

Ctsa3
  • 123
  • 1
  • 1
  • 7
  • So when the window resizes, you change the size of elements contained in the window, which causes the window to resize, firing the "resize" event. See [Resizing an element triggers the resize event of the window](http://stackoverflow.com/questions/15902920/resizing-an-element-triggers-the-resize-event-of-the-window) – Tibrogargan Apr 20 '17 at 22:06
  • 2
    Also, to center content a page there is CSS to do that automatically... Including scaling down if the window gets too small. – JvO Apr 20 '17 at 22:07

1 Answers1

0

you shouldn't need to keep redefining numbers as strings and adding the 'px'. Try:

if (windowWidth == sec1LogoWidth) {
        $('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))),'left': 0, 'margin-left': 10, 'margin-right': 10 });
    } else {
        $('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))) - 100,'left': Math.abs((windowWidth / 2) - (sec1LogoWidthImg-(Math.ceil(sec1LogoWidthImg/2)))) });
    }
Sam0
  • 1,459
  • 1
  • 11
  • 13