当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Getting css background-size height number of img after change

问题描述:

var $img='<img id="_img"src="large.png"style="display:none;"/>';

$('body').prepend($img);

$img.load(function(){

if(this.width>$(window).width()){$('#_64b'+n).css({'background-size':'100% auto'});}else

if(this.width<$(window).width()){$('#_64b'+n).css({'background-size':'100% auto'});}

if(this.height>$(window).height()){$('#_64b'+n).css({'background-size':'auto 100%'});}else

if(this.height<$(window).height()){$('#_64b'+n).css({'background-size':'auto 100%'});}

if(this.width<$(window).width()&&this.height<$(window).height()){

if(this.width>this.height){$('#_64b'+n).css({'background-size':'100% auto'});}

else{$('#_64b'+n).css({'background-size':'auto 100%'});}

$('#_img').fadeIn(500);

}

I'm not understanding how to get the new background-size in px after the css background-size 'resize'

If I knew that then I think I could ask JavaScript to put background-position something like:

//if the height is bigger than width

var css_height=?; //unknown number

var same_posx_new_posy='0px '+(($(window).height()-css_height)/2)+'px';

$('#_img').css({'background-position':same_posx_new_posy});

Correct?

Dang!! I've just realized that I'm not dealing with a background as-in

<div style="background:url('whatever.png');">

I'm using images instead because the load event is better with

$img.load(function(/*do stuff*/){}).each(function(){

var t=setTimeout(function(){if(&&(this.complete){$(this).load();}},1000);//handles cahing nicely

});

So is my Jquery css manipulation idea stupid or what?

分享给朋友:
您可能感兴趣的文章:
随机阅读: