WordPress教程

WordPress文章内容图片自适应大小缩放

阿里云

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

方法一:

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

也想出现在这里?联系我们
创客主机
  1. p img {
  2. max-width:600px;
  3. width: expression(this.width > 600 ? “600px: true);
  4. height:auto;
  5. }

方法二:

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

  1. $(document).ready(function(){
  2.     $('div').autoResize({height:750});
  3. });  
  4. jQuery.fn.autoResize = function(options)
  5. {
  6.     var opts = {
  7.         'width' : 700,
  8.         'height': 750
  9.     }
  10.     var opt = $.extend(true, {},opts,options || {});
  11.     width = opt.width;
  12.     height = opt.height;
  13.     $('img',this).each(function(){
  14.         var image = new Image();
  15.         image.src = $(this).attr('src');   if(image.width > 0 && image.height > 0 ){
  16.             var image_rate = 1;
  17.             if( (width / image.width) < (height / image.height)){
  18.                 image_rate = width / image.width ;
  19.             }else{
  20.                 image_rate = height / image.height ;
  21.             }
  22.             if ( image_rate <= 1){
  23.                 $(this).width(image.width * image_rate);
  24.                 $(this).height(image.height * image_rate);
  25.             }
  26.         }
  27.     });
  28. }

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

WordPress 文章内容图片自适应大小缩放

已有 869 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!