这是我的例子
HTML:
<div class="container">
<img src="http://placehold.it/500x500" alt="500x500" />
</div>
<div class="container">
<img src="http://placehold.it/100x500" alt="100x500" />
</div>
<div class="container">
<img src="http://placehold.it/500x100" alt="500x100" />
</div>
<div class="container">
<img src="http://placehold.it/500x800" alt="500x800" />
</div>
<div class="container">
<img src="http://placehold.it/800x500" alt="800x500" />
</div>
<div class="container">
<img src="http://placehold.it/100x100" alt="100x100" />
</div>
<div class="container">
<img src="http://placehold.it/200x100" alt="200x100" />
</div>?
Run Code Online (Sandbox Code Playgroud)
CSS:
div.container { width: 200px; height: 200px; line-height: 200px; overflow: hidden; border: 10px solid white; box-shadow: 0 1px 8px #BBB; margin: 10px; text-align: center; }
div.container img { position: relative; vertical-align:middle; }
div.container img.wide { max-height: 100%; }
div.container img.tall { max-width: 100%; }
div.container img.square { max-width: 100%; }?
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
$(window).load( function(){
var $imgs = $("div.container img");
$imgs.each( function(){
var cW = $(this).parent().width();
var cH = $(this).parent().height();
var w = $(this).width(); //I want the CURRENT width, not original!!
var h = $(this).height(); //I want the CURRENT height, not original!!
var dW = w - cW;
var dH = h - cH;
console.log( cW + " " + cH + " " + w + " " + h + " " + dW + " " + dH );
if ( w > h ){
$(this).addClass("wide");
$(this).css("left", -dW/2);
}
else if ( w < h ){
$(this).addClass("tall");
$(this).css("top", -dH/2);
}
else { $(this).addClass("square"); }
});
});?
Run Code Online (Sandbox Code Playgroud)
我通过javascript将一个类添加到图像元素的基础上.这3个类将设置图像的max-width和max-height css属性,从而调整它们的大小.在那之后,我需要调整大小的图像的尺寸,但我能得到的只是原始图像的尺寸.那不好!我尝试使用width(),outerWidth,naturalWidth,每个类型的宽度,但我只是无法得到它的权利.
那么,有没有办法在CSS规则之后获得图像的尺寸max-width并且max-height已经应用于它?优选地是一致的方式,不使用定时器和间隔,或类似的东西.
非常感谢!
看看这个 jsfiddle.在使用max-height/ max-width而不是应用类之后,您正试图获得widht/height .
这是测试代码(检查第二个console.log):
$(window).load( function(){
var $imgs = $("div.container img");
$imgs.each( function(){
var cW = $(this).parent().width();
var cH = $(this).parent().height();
var w = $(this)[0].clientWidth; //I want the CURRENT width, not original!!
var h = $(this)[0].clientHeight; //I want the CURRENT height, not original!!
var dW = w - cW;
var dH = h - cH;
console.log( cW + " " + cH + " " + w + " " + h + " " + dW + " " + dH );
if ( w > h ){
$(this).addClass("wide");
$(this).css("left", -dW/2);
}
else if ( w < h ){
$(this).addClass("tall");
$(this).css("top", -dH/2);
}
else { $(this).addClass("square"); }
var w = $(this).width(); //I want the CURRENT width, not original!!
var h = $(this).height(); //I want the CURRENT height, not original!!
console.log( w + " " + h );
});
});?
Run Code Online (Sandbox Code Playgroud)
width(),outerWidth- 这总是返回当前宽度.
naturalWidth/ naturalHeight- 这是为了获得一个原始文件,比如没有应用任何样式,图像的宽度/高度(仅适用于较新的浏览器中的IMG标记)