实际上,我尝试在JS中获取图像的方向,以便向其添加一个类。它在Firefox上似乎工作得很好,但我在其他浏览器上遇到了问题。它对非常小的图像效果更好,所以我猜如果图像没有完全加载,它就不能工作。但是我找不到一个解决方案来让它正常工作!谢谢!
我的代码:
$(document).ready(function() {
$('.gallery a').find('img').each(function() {
var imgClass = (this.width / this.height > 1) ? 'landscape' : 'portrait';
$(this).addClass(imgClass);
})
});.gallery {
display: flex;
flex-wrap: wrap;
width: 450px;
}
.gallery a {
width: 200px;
height: 200px;
display: flex;
justify-content: center;
overflow: hidden;
}
.gallery a img.landscape {
align-self: center;
height: 100%;
filter: sepia(100%);
}
.gallery a img.portrait {
align-self: center;
width: 100%;
filter: grayscale(100%);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery">
<a href="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
<img src="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
</a>
<a href="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
<img src="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
</a>
<a href="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
<img src="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
</a>
<a href="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
<img src="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
</a>
</div>
发布于 2017-06-21 22:30:59
基本上,您需要先检查图像是否已加载,如果未加载,则添加一个事件侦听器来触发它。
您可以判断是否加载了图像,因为它的width和height将大于0。如果它们等于0,则不加载。它可能会在FF中工作,因为它缓存了图像,所以它们会立即加载。
下面这样的代码应该能起到作用:
function setImageClass(img) {
var imgClass = (img.width / img.height > 1) ? 'landscape' : 'portrait';
$(img).addClass(imgClass);
}
$(document).ready(function() {
$('.gallery a').find('img').each(function() {
if (this.width > 0) {
setImageClass(this);
} else {
$(this).once('load', function () {
setImageClass(this);
});
}
})
});发布于 2017-06-22 15:54:15
我终于找到了!应该使用naturalWidth和naturalHeight来获取图片的原始值。如果有人感兴趣,这里是我的代码工作的每一个浏览器。谢谢你,Samanime!
function setImageClass(img) {
var imgClass = (img.naturalWidth / img.naturalHeight > 1) ? 'landscape' : 'portrait';
$(img).addClass(imgClass);
}
$(document).ready(function() {
$('.gallery a').find('img').each(function() {
if (this.naturalWidth > 0) {
setImageClass(this);
} else {
$(this).once('load', function () {
setImageClass(this);
});
};
})
});.gallery {
display: flex;
flex-wrap: wrap;
width: 450px;
}
.gallery a {
width: 200px;
height: 200px;
display: flex;
justify-content: center;
overflow: hidden;
}
.gallery a img.landscape {
align-self: center;
height: 100%;
filter: sepia(100%);
}
.gallery a img.portrait {
align-self: center;
width: 100%;
filter: grayscale(100%);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="gallery">
<a href="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
<img src="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
</a>
<a href="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
<img src="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
</a>
<a href="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
<img src="http://lorempixel.com/output/city-q-c-1500-1000-3.jpg">
</a>
<a href="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
<img src="http://lorempixel.com/output/city-h-c-1000-1500-4.jpg">
</a>
</div>
https://stackoverflow.com/questions/44678926
复制相似问题