[英]Select, then resize images that have a certain ratio with jQuery
On my Tumblr blog, I'm trying to use jQuery to select photo entries (imgs) that have certain ratios and resize them in certain ways. 在我的Tumblr博客上,我试图使用jQuery选择具有特定比例的照片条目(img),并以某些方式调整它们的大小。
Specifically: If an image's width is greater than 250px, resize it so the height is 250px, and the width "auto". 具体来说:如果图像的宽度大于250px,请调整其大小,以使高度为250px,宽度为“自动”。 If an image's height is greater than 250px, resize it so the width is 250px, and the height "auto". 如果图像的高度大于250px,请调整其大小,使其宽度为250px,高度为“自动”。 Lastly, if the image is a perfect square, resize it to 250px by 250px. 最后,如果图片是完美的正方形,请将其大小调整为250px x 250px。 Here's what I'm currently using. 这是我目前正在使用的。 If it's weirdly coded, please forgive me, I honestly just fiddled with it until I got some desired results... 如果编码很奇怪,请原谅我,老实说,我一直在弄弄它,直到得到一些期望的结果...
<script>
$(document).ready(function() {
$('.photo_post img').each(function() {
var maxWidth = 250; // Max width for the image
var maxHeight = 250; // Max height for the image
var ratio = 0; // Used for aspect ratio
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Check if the current width is larger than the max
if(height > maxHeight){
ratio = maxWidth / width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(this).css("height", height * ratio); // Scale height based on ratio
height = height * ratio; // Reset height to match scaled image
width = width * ratio; // Reset width to match scaled image
}
// Check if current height is larger than max
if(height < maxHeight){
ratio = maxHeight / height; // get ratio for scaling image
$(this).css("height", maxHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
height = height * ratio; // Reset height to match scaled image
}
});
});
</script>
My problem is that it doesn't work correctly on all photos. 我的问题是,它不能在所有照片上正常工作。
I'm not super familiar with jQuery, so deliberate and detailed answers are appreciated. 我对jQuery并不是很熟悉,因此深思熟虑和详尽的答案将不胜感激。
You need to check if the image is portrait or landscape and then adjust the size based on that. 您需要检查图像是纵向还是横向,然后根据该图像调整尺寸。
Updated JS: 更新的JS:
$(document).ready(function() {
$('.photo_post img').each(function() {
var maxWidth = 250; // Max width for the image
var maxHeight = 250; // Max height for the image
var ratio = 0; // Used for aspect ratio
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Portrait
if (height > width) {
// Check if the current height is larger than the max
if(height > maxHeight){
ratio = maxHeight / height; // get ratio for scaling image
$(this).css("height", maxHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
height = height * ratio; // Reset height to match scaled image
}
}
// Landscape
else {
// Check if the current width is larger than the max
if(width > maxWidth){
ratio = maxWidth / width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(this).css("height", height * ratio); // Scale height based on ratio
height = height * ratio; // Reset height to match scaled image
width = width * ratio; // Reset width to match scaled image
}
}
});
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.