WordPress网站文章图片如何实现自适应大小缩放?

我们在WordPress主题开发中经常需要考虑图片自适应问题,那么WordPress网站文章图片如何实现自适应大小缩放?今天总结的二种方法给大家,希望能帮助大家解决手头的问题。
WordPress网站文章图片如何实现自适应大小缩放? (https://www.wp-admin.cn/) WordPress使用教程 第1张
方法一:

将以下代码复制粘贴到主题style.css文件内即可,强制最大化宽度为600px,高度为相对高度,通过以上设置不失为一个最佳的解决方案:

p img {

max-width:600px;

width: expression(this.width > 600 ? “600px” : true);

height:auto;

}

方法二:

通过jQuery库来实现图片自适应,首先我们要加载jquer库,然后以下面的代码添加到wordpress主题中的header.php文件中。可以对图片进行自动缩放,方法较为完美:

$(document).ready(function(){

$(‘div’).autoResize({height:750});

});

jQuery.fn.autoResize = function(options)

{

var opts = {

‘width’ : 700,

‘height’: 750

}

var opt = $.extend(true, {},opts,options || {});

width = opt.width;

height = opt.height;

$(‘img’,this).each(function(){

var image = new Image();

image.src = $(this).attr(‘src’);   if(image.width > 0 && image.height > 0 ){

var image_rate = 1;

if( (width / image.width) < (height / image.height)){

image_rate = width / image.width ;

}else{

image_rate = height / image.height ;

}

if ( image_rate <= 1){

$(this).width(image.width * image_rate);

$(this).height(image.height * image_rate);

}

}

});

}

这两种方法可以说都通过各自的方式达到了我们想要的效果。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-07-15 11:48:23
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: