Javascript 当高度大于JQuery的宽度时,图像宽度为100%
我正在尝试制作wordpress主题,因此我的代码是: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%; } 之所以包
<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');
}