Javascript 调整窗口大小时修改图像大小(jQuery)
我试图在加载页面和调整浏览器大小时调整图像大小。有些图像是纵向的,有些是横向的,它们位于容器div中,容器div也可能是纵向/横向的(取决于浏览器大小)。无论图像是否应拉伸以填充框(100%宽度或100%高度),如果比率不匹配,则将发生剪切(通过CSSJavascript 调整窗口大小时修改图像大小(jQuery),javascript,jquery,image-resizing,window-resize,Javascript,Jquery,Image Resizing,Window Resize,我试图在加载页面和调整浏览器大小时调整图像大小。有些图像是纵向的,有些是横向的,它们位于容器div中,容器div也可能是纵向/横向的(取决于浏览器大小)。无论图像是否应拉伸以填充框(100%宽度或100%高度),如果比率不匹配,则将发生剪切(通过CSS溢出:隐藏) 我的方法是获得容器盒的比率(即portrate或横向)和图像比率(纵向或横向),比较它们并相应地调整CSS宽度/高度 注意:所有容器都有一个mr_-our-dest-img-container类,并且都只包含一个img元素(以及一些不
溢出:隐藏)
我的方法是获得容器盒的比率(即portrate或横向)和图像比率(纵向或横向),比较它们并相应地调整CSS宽度/高度
注意:所有容器都有一个mr_-our-dest-img-container
类,并且都只包含一个img元素(以及一些不同的元素)
这是我的代码,它不起作用。它不起任何作用,我也弄不明白。有人能帮我修一下吗
$(document).ready(function () {
updateImageSize();
$(window).resize(function() {
updateImageSize();
});
});
function updateImageSize() {
$(".mr_our-dest-img-container").each(function(){
var ratio_cont = jQuery(this).width()/jQuery(this).height();
var $img = jQuery(this).find("img");
var ratio_img = $img.width()/$img.height();
if (ratio_cont > ratio_img)
{
$img.css({"width": "100%", "height": "auto"});
}
else if (ratio_cont < ratio_img)
{
$img.css({"width": "auto", "height": "100%"});
}
}
};
$(文档).ready(函数(){
updateImageSize();
$(窗口)。调整大小(函数(){
updateImageSize();
});
});
函数updateImageSize(){
$(“.mr_our-dest-img-container”)。每个(函数(){
var ratio_cont=jQuery(this.width()/jQuery(this.height();
var$img=jQuery(this.find)(“img”);
var ratio_img=$img.width()/$img.height();
如果(比率控制>比率调整)
{
$img.css({“宽度”:“100%”,“高度”:“自动”});
}
否则如果(比率控制<比率img)
{
$img.css({“宽度”:“自动”,“高度”:“100%”);
}
}
};
正如控制台所说,这是一个语法错误。在每个函数结束后,您将错过一个)
function updateImageSize() {
$(".mr_our-dest-img-container").each(function(){
var ratio_cont = jQuery(this).width()/jQuery(this).height();
var $img = jQuery(this).find("img");
var ratio_img = $img.width()/$img.height();
if (ratio_cont > ratio_img)
{
$img.css({"width": "100%", "height": "auto"});
}
else if (ratio_cont < ratio_img)
{
$img.css({"width": "auto", "height": "100%"});
}
}); //missing )
};
函数updateImageSize(){
$(“.mr_our-dest-img-container”)。每个(函数(){
var ratio_cont=jQuery(this.width()/jQuery(this.height();
var$img=jQuery(this.find)(“img”);
var ratio_img=$img.width()/$img.height();
如果(比率控制>比率调整)
{
$img.css({“宽度”:“100%”,“高度”:“自动”});
}
否则如果(比率控制<比率img)
{
$img.css({“宽度”:“自动”,“高度”:“100%”);
}
});//丢失)
};
Google Chrome console给了我这个错误Uncaught SyntaxError:Unexpected token}
并突出显示了主要问题代码中的最后一个花括号。我看了一遍又一遍这段代码,花括号似乎都互相抵消了?