php图片自适应缩放,两种实现WordPress 文章内容图片自适应大小缩放的方法

摘要:关于wordpress主题开发中文章图片自适应的问题已经是老生常谈了,今天大挖总结的三种方法给大家,希望能帮助大家解决手...

关于wordpress主题开发中文章图片自适应的问题已经是老生常谈了,今天大挖总结的三种方法给大家,希望能帮助大家解决手头的问题,其它也没有什么创新的方法,还是css与jquery的解决方法。

推荐方法:运用用官方默认CSS样式

将以下代码复制粘贴到主题style.css文件内即可

强制最大化宽度为600px,高度为相对高度,通过以上设置不失为一个最佳的解决方案

p img {

max-width:600px;

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

height:auto;

}

1

2

3

4

5

pimg{

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);

}

}

});

}

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

27

28

$(document).ready(function(){

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

});

jQuery.fn.autoResize=function(options)

{

varopts={

'width':700,

'height':750

}

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

width=opt.width;

height=opt.height;

$('img',this).each(function(){

varimage=newImage();

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

varimage_rate=1;

if((width/image.width)

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);

}

}

});

}

这两种方法可以说都通过各自的方式达到了我们想要的效果,同时大挖还是推荐第一种,因为第二种对于不了解代码的同学,操作成本还是比较大的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值