WordPress如何使用jQuery 按比例调整图片高度/宽度

事业上得寸进尺,生活中不论短长,何愁事业无成。人生最精彩的不是实现梦想的瞬间,而是坚持梦想的过程。

同学们,今天我们来谈谈WordPress主题制作的细节问题:限制文章中图片的最大宽度,防止图片“撑破”页面,用的比较多的方法是在CSS中使用 overflow:hiddenmax-width:600px 来限制,但是效果不理想,倡萌今天分享一下比较完美的方法: jQuery 按比例调整图片高度/宽度。

2013-4-14更新:使用max-width 也是可以按等比缩小图片的。以前倡萌之所以任务无法实现,是因为我自己使用WLW离线发布时,使用WLW定义了图片的格式,而没办法继承样式表的样式。特此说明,希望大家不要被误导!

三种方法的效果比较

从上图可以看出,overflow 仅仅是隐藏了部分图片,导致图片显示不全;max-width 只是限制的最大宽度,图片压缩变形;而jQuery 却可以等比例调整图片的高和宽,图片没有变形,这就是我们要的效果!

等比例调整图片的高和宽

方法一:jQuery 代码(荐)

现在大部分的网站都使用了 jQuery 库,所以我们只需添加下面的 jQuery 代码即可实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$(document).ready(function() {
    $('.post img').each(function() {
    var maxWidth = 600; // 图片最大宽度
    var maxHeight = 2000;    // 图片最大高度
    var ratio = 0;  // 缩放比例
    var width = $(this).width();    // 图片实际宽度
    var height = $(this).height();  // 图片实际高度

    // 检查图片是否超宽
    if(width > maxWidth){
        ratio = maxWidth / width;   // 计算缩放比例
        $(this).css("width", maxWidth); // 设定实际显示宽度
        height = height * ratio;    // 计算等比例缩放后的高度
        $(this).css("height", height * ratio);  // 设定等比例缩放后的高度
    }

    // 检查图片是否超高
    if(height > maxHeight){
        ratio = maxHeight / height; // 计算缩放比例
        $(this).css("height", maxHeight);   // 设定实际显示高度
        width = width * ratio;    // 计算等比例缩放后的高度
        $(this).css("width", width * ratio);    // 设定等比例缩放后的高度
    }
    });
});

$(document).ready(function() { $('.post img').each(function() { var maxWidth = 600; // 图片最大宽度 var maxHeight = 2000; // 图片最大高度 var ratio = 0; // 缩放比例 var width = $(this).width(); // 图片实际宽度 var height = $(this).height(); // 图片实际高度 // 检查图片是否超宽 if(width > maxWidth){ ratio = maxWidth / width; // 计算缩放比例 $(this).css("width", maxWidth); // 设定实际显示宽度 height = height * ratio; // 计算等比例缩放后的高度 $(this).css("height", height * ratio); // 设定等比例缩放后的高度 } // 检查图片是否超高 if(height > maxHeight){ ratio = maxHeight / height; // 计算缩放比例 $(this).css("height", maxHeight); // 设定实际显示高度 width = width * ratio; // 计算等比例缩放后的高度 $(this).css("width", width * ratio); // 设定等比例缩放后的高度 } }); });

实际使用时,注意修改 $('.post img') 为你文章内容的class值,以及最大宽度、高度。

关于jQuery 库,请阅读《WordPress提速:选择好的jQuery库(Google/Microsoft/SAE) 》

方法二:纯Javascript代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<script type="text/javascript">
function ResizeImage(image, 插图最大宽度, 插图最大高度)
{
    if (image.className == "Thumbnail")
    {
        w = image.width;
        h = image.height;

        if( w == 0 || h == 0 )
        {
            image.width = maxwidth;
            image.height = maxheight;
        }
        else if (w > h)
        {
            if (w > maxwidth) image.width = maxwidth;
        }
        else
        {
            if (h > maxheight) image.height = maxheight;
        }

        image.className = "ScaledThumbnail";
    }
}
</script>

<script type="text/javascript"> function ResizeImage(image, 插图最大宽度, 插图最大高度) { if (image.className == "Thumbnail") { w = image.width; h = image.height; if( w == 0 || h == 0 ) { image.width = maxwidth; image.height = maxheight; } else if (w > h) { if (w > maxwidth) image.width = maxwidth; } else { if (h > maxheight) image.height = maxheight; } image.className = "ScaledThumbnail"; } } </script>

实现效果和上面一样,不需要引用 jQuery 库,请设置最大高度和宽度值;在发布文章时,要手动给每张图片添加一个 class=”Thumbnail”

好了,今天课程到此为止,如果你有更好的方法,欢迎投稿分享!

到此这篇关于WordPress如何使用jQuery 按比例调整图片高度/宽度就介绍到这了。青春的所有者,不能总是在高山麓溪水旁谈情话看流云,青春的魅力,应当叫枯枝长出鲜果,沙漠布满森林。更多相关WordPress如何使用jQuery 按比例调整图片高度/宽度内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?