Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当高度大于JQuery的宽度时,图像宽度为100%_Javascript_Jquery_Html_Css_Wordpress - Fatal编程技术网

Javascript 当高度大于JQuery的宽度时,图像宽度为100%

Javascript 当高度大于JQuery的宽度时,图像宽度为100%,javascript,jquery,html,css,wordpress,Javascript,Jquery,Html,Css,Wordpress,我正在尝试制作wordpress主题,因此我的代码是: <div class="post-thumb"> <?php echo get_the_post_thumbnail(); ?> </div> 任何解决办法 既然可以使用CSS,为什么还要使用jQuery .post-thumb-img { height:auto; width:auto; max-width:100%; max-height:100%; } 之所以包

我正在尝试制作wordpress主题,因此我的代码是:

<div class="post-thumb">
<?php echo get_the_post_thumbnail(); ?> 
</div>

任何解决办法

既然可以使用CSS,为什么还要使用jQuery

.post-thumb-img {
    height:auto;
    width:auto;
    max-width:100%;
    max-height:100%;
}

之所以包含
auto
部分,是因为旧版本的Firefox不会查看
min-
声明,除非常规声明是明确的。

您可以将图像设置为背景图像并使用背景封面

.post-thumb{
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
 background-size: cover;
}
试试这个代码

var landscape = $(".post-thumb img ").width();
var portrait = $(".post-thumb img").height();
if (landscape > portrait)
{
   $(".post-thumb img").height('100%') .width('auto');
}
else 
{
   $(".post-thumb img").width('100%') .height('auto');
}

当我这样做时,宽度图像或高度图像将太大,.post thumb{overflow:hidden;}因此图像的很大一部分将不会显示您不是在寻找完全覆盖,您是在寻找一种包含的设置?然后将
min
切换到
max
!我会修改我的答案。
var landscape = $(".post-thumb img ").width();
var portrait = $(".post-thumb img").height();
if (landscape > portrait)
{
   $(".post-thumb img").height('100%') .width('auto');
}
else 
{
   $(".post-thumb img").width('100%') .height('auto');
}