Warning: Declaration of c2c_ConfigureSMTP::options_page_description() should be compatible with C2C_Plugin_023::options_page_description($localized_heading_text = '') in /home/mslandymdsol1agnid/wwwroot/wp-content/plugins/configure-smtp/configure-smtp.php on line 47 WordPress 实现图片自动缩放 | 迷失的世界
当前位置: 首页 > 网站相关 > 正文

WordPress 实现图片自动缩放

偶经常发表日志需要配图,感觉加点图片怎么都看着舒服些,有时候加的图片比较大,
导致图片显示不全,所以需要这个自动缩放效果!
wordpress官方皮肤自带有大图片自动缩放,但是效果不是很好,下面列举3种比较完美的缩放代码供大家参考。

方法一、采用官方默认CSS

使用方法:将以下代码直接加入style.css即可
相关说明:只能控制宽度的缩放,高度无法控制,且不支持IE6

p img {
	padding: 0;
	max-width: 100%;
	}

官方的这个方法,不能控制高度,而且不支持IE6,下面偶寻找了一个比较完美的解决方案,我现在用的就是这种CSS控制方法:

p img {
max-width:600px;
width: expression(this.width > 600 ? “600px” : true);
height:auto;
}

方法二、使用jQuery实现

使用方法:1、加载jQuery库 2、将以下代码加入header.php或单独保存为JS并加载

相关说明:可以对图片进行自动缩放,方法较为完美。

$(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);
            }
        }
    });
}

方法三、CSS控制

使用方法:将以下代码直接加入style.css即可

相关说明:可以缩放高度以及宽度,但是比较死板,支持IE6

p img {
    max-width:100px; /* FF IE7 */
    max-height:80px; /* FF IE7 */
    width:expression(this.width > 100 && this.width > this.height ? 100 : true);
    height:expression(this.height > 80 && this.height > this.width ? 80 : true);
    overflow:hidden;
}

这个可以控制缩放固定大小~~也挺好的!



分享到:

本文固定链接: http://www.msland.cn/css-kongzhi.html | 迷失的世界

该日志由 Zero 于2012年06月26日发表在 网站相关 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: WordPress 实现图片自动缩放 | 迷失的世界
关键字: , ,

WordPress 实现图片自动缩放:目前有11 条留言

  1. 6楼
    泪雪 Internet Explorer Windows :

    这个挺好的 去用用

    2012-07-04 下午 12:42
    • Zero Safari iPhone :

      建议用jQ的方法…………

      2012-07-05 上午 1:11
  2. 5楼
    宾果儿 360 Browser Windows :

    做个记号,回头添加试试效果,吼吼。

    2012-07-02 上午 10:27
  3. 4楼
    潇遥 Mozilla Firefox Windows :

    确实啊,我现在写影评就遇到图片大小的问题,wp自带的缩放功能确实不咋地,有空试试你的办法,不过看起来好复杂啊。

    2012-06-30 下午 10:50
    • Zero Google Chrome Windows :

      一点都不复杂啊,挺简单的!就是几段代码!

      2012-07-02 上午 12:09
  4. 地板
    泪雪 Internet Explorer Windows :

    能做成一个插件就最好了 方便嘛

    2012-06-28 下午 1:58
    • zero 360 Browser Windows :

      加段代码的问题还需要搞成插件啊。多麻烦啊!

      2012-06-28 下午 4:54
  5. 板凳
    混乱羽翼 360 Browser Windows :

    额,有木有Demo呀,求个演示看看,我是用了灯箱插件了

    2012-06-27 下午 10:01
  6. 沙发
    5不出来 360 Browser Windows :

    我之前就是在后台改大小,还要不停的试百分之多少大小合适

    2012-06-27 下午 7:52
    • zero Internet Explorer Windows :

      百分之多少合适。。。晕死!~ :cool:

      2012-06-27 下午 11:37

发表评论

您必须 [ 登录 ] 才能发表留言!