Javascript 选择,然后使用jQuery调整具有一定比例的图像大小
在我的Tumblr博客上,我尝试使用jQuery选择具有特定比率的照片条目(IMG),并以特定方式调整其大小。Javascript 选择,然后使用jQuery调整具有一定比例的图像大小,javascript,jquery,html,css,image,Javascript,Jquery,Html,Css,Image,在我的Tumblr博客上,我尝试使用jQuery选择具有特定比率的照片条目(IMG),并以特定方式调整其大小。 特别是:如果图像的宽度大于250px,请调整其大小,使其高度为250px,宽度为“自动”。如果图像的高度大于250px,请调整其大小,使其宽度为250px,高度为“自动”。最后,如果图像是一个完美的正方形,将其调整为250px乘以250px。 这是我目前正在使用的。如果它的编码很奇怪,请原谅我,我真的只是摆弄它,直到我得到一些想要的结果 <script> $(documen
特别是:如果图像的宽度大于250px,请调整其大小,使其高度为250px,宽度为“自动”。如果图像的高度大于250px,请调整其大小,使其宽度为250px,高度为“自动”。最后,如果图像是一个完美的正方形,将其调整为250px乘以250px。 这是我目前正在使用的。如果它的编码很奇怪,请原谅我,我真的只是摆弄它,直到我得到一些想要的结果
<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>
$(文档).ready(函数(){
$('.photo_post img')。每个(函数(){
var maxWidth=250;//图像的最大宽度
var maxHeight=250;//图像的最大高度
var ratio=0;//用于纵横比
var width=$(this).width();//当前图像宽度
var height=$(this).height();//当前图像高度
//检查当前宽度是否大于最大值
如果(高度>最大高度){
ratio=maxWidth/width;//获取缩放图像的比率
$(this.css(“width”,maxWidth);//设置新宽度
$(this).css(“高度”,高度*比率);//根据比率缩放高度
高度=高度*比率;//重置高度以匹配缩放图像
宽度=宽度*比率;//重置宽度以匹配缩放图像
}
//检查当前高度是否大于最大值
如果(高度<最大高度){
ratio=maxHeight/height;//获取缩放图像的比率
$(this.css(“height”,maxHeight);//设置新高度
$(this).css(“宽度”,宽度*比率);//基于比率缩放宽度
宽度=宽度*比率;//重置宽度以匹配缩放图像
高度=高度*比率;//重置高度以匹配缩放图像
}
});
});
我的问题是它不能在所有照片上正常工作。我对jQuery不太熟悉,所以请仔细回答。您需要检查图像是纵向还是横向,然后根据这一点调整大小 更新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
}
}
});
})) 我们可以看看你的博客或者至少是JSFIDLE!上的html示例吗!?