简体   繁体   中英

jQuery CSS Attributes not Applying

I am centering a element inside a div horizontally and vertically, however the jQuery CSS code seems to be only resulting in style="margin: 0px" any ideas?

Example ( script src )

jQuery:

    image.css({
            'margin-top': '-'+(style['img-width']/2)+'px',
            'margin-left': '-'+(style['img-height']/2)+'px',
    });

Alternatively I tried:

    image.css({
            'marginTop': '-'+(style['img-width']/2)+'px',
            'marginLeft': '-'+(style['img-height']/2)+'px',
    });

CSS:

                #theater-img {
                    position: absolute;
                    text-align: center;
                    margin: 0;
                    padding: 0;
                }
                #theater-img img {
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    -webkit-box-shadow: 0 0 20px 20px rgba(0,0,0,0.5);
                    box-shadow: 0 0 20px 20px rgba(0,0,0,0.5);
                }

Comment the following line in your bfx.viewer.js :

image.css('margin', style['img-padding']+'px');

This is there the margin: 0px; comes from.

I just commented below positioning CSS:

/*
#theater-img img {
    box-shadow: 0 0 20px 20px rgba(0, 0, 0, 0.5);
    left: 50%;
    position: absolute;
    top: 50%;
}
*/

and then changed the postion to relative instead of absolute here:

#theater-img {
    margin: 0;
    padding: 0;
    position: relative;
    text-align: center;
}

Is it you wanted ??

具有更新的CSS的页面图像

I saw your example link. I think use that codes after all images loaded.

jQuery:

$(window).load(function() {    
    $('#theater-img img').css({
        'margin-top': '-' + $('#theater-img img').height()/2 + 'px',
        'margin-left': '-' + $('#theater-img img').width()/2 + 'px'
    });
});

and here's some css changes for your page. You should change #theater-img

HMTL:

#theater-img {
  position: relative;
  text-align: center;
  margin: 0 0 46px 0;
  padding: 0;
  width: 100%;
  height: 100%;
  top: -46px;
  z-index: -1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM