Javascript+CSS控制日志图片大小

知更鸟
知更鸟
知更鸟
站长
2511
文章
0
粉丝
HotNews主题3,674阅读模式

网上的方法很多,但都有局限性,本文提供的方法基本兼容主流浏览器。

以HotNews主题为例。打开HotNews主题HotNewspro\js目录的custom.js把下面代码加到最后:

  1. // 控制图片大小
  2. $(document).ready(function() {
  3.     var maxwidth=580;
  4.     $(".entry p img").each(function(){
  5.         if (this.width > maxwidth)
  6.          this.width = maxwidth;
  7.     });
  8. });

再打开主题样式文件style在最后添加:

  1. .entry p img{
  2.     height: auto;
  3. }

 

也可单独使用Javascript或者CSS控制图片大小,不过兼容性较差

javascrip:

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3.     var maxwidth=580;
  4.     var maxheight=450
  5.     $(".entry p img").each(function(){
  6.         if (this.width > maxwidth)
  7.          this.width = maxwidth;
  8.         if (this.height > maxheight)
  9.          this.height = maxheight;
  10.     });
  11. });
  12. </script>

CSS:

  1. .entry p img{
  2.     max-width: 90%;
  3.     height: auto;
  4. }

其它主题可以将上述代码中的 .entry 为所用主题选择器。

编辑文章时手动将插入的图片拖小,个人认为一劳永逸。

本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
微信号已复制
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  28  访客  24  作者  4