找回密码
 立即注册
搜索
热搜: 生活 中国

用CSS来控制图片显示大小的代码

[复制链接]
admin 发表于 2015-1-9 18:52:57 | 显示全部楼层 |阅读模式
  1. img,a img{     
  2. border:0;      
  3. margin:0;      
  4. padding:0;     
  5. max-width:590px;     
  6. width:e­xpression(this.width>590?"590px":this.width);     
  7. max-height:590px;     
  8. height:e­xpression(this.height>590?"590px":this.height);     
  9. }
复制代码
  1.   Css防止图片尺寸过大

  2.   添加如下CSS:
  3. 代码如下          

  4. img {  
  5. max-width: 800px;  
  6. height: auto;  
  7. }

  8.   代码中的max-width:800px限制图片的最大宽度为800像素,而下面的hight:auto很关键,可以保证图片有正确的长宽比,不至于因为被调整宽度而变形。

  9.   实用例子

  10.   WordPress自动调整图片大小

  11.   1、打开你的“样式表 (style.css)”文件,然后在 p img{ 或类似的地方添加下列代码(可以将所有550改成你想要的宽度)
  12. 代码如下          

  13. p img{
  14. max-width: 550px;
  15. width: expression(this.width > 550 ? "550px" : true);
  16. height: auto;
  17. }

  18.   2、清空缓存就可以啦!

  19.   3、同样对于某些老版本IE不支持。

  20.   如果你想兼职所有浏览器jq或js是最好的办法
  21. 代码如下          

  22. //  方法:setSelectReadOnly  用于设定极select控件ReadOnly,
  23. //这个一个模拟只读不是真的只读
  24. //使用了onbeforeactivate,onfocus,onmouseover,onmouseout事件
  25. //示例:< img src='img.jpg' onload='ImgAutoSize(ImgD,FitWidth,FitHeight)' > ;
  26. //  create by sl  
  27. // ---------------------------------------------------
  28. function ImgAutoSize(imgD,FitWidth,FitHeight)  
  29. {
  30. var image1=new Image();  
  31. image1.onload = function ()
  32. {
  33. if(this.width>0 && this.height>0)  
  34. {  
  35. if(this.width/this.height>= FitWidth/FitHeight)  
  36. {  
  37. if(this.width>FitWidth)  
  38. {  
  39. imgD.width=FitWidth;  
  40. imgD.height=(this.height*FitWidth)/this.width;  
  41. }  
  42. else  
  43. {  
  44. imgD.width=this.width;  
  45. imgD.height=this.height;  
  46. }  
  47. }  
  48. else  
  49. {  
  50. if(this.height>FitHeight)  
  51. {  
  52. imgD.height=FitHeight;  
  53. imgD.width=(this.width*FitHeight)/this.height;  
  54. }  
  55. else  
  56. {  
  57. imgD.width=this.width;  
  58. imgD.height=this.height;  
  59. }  
  60. }  
  61. }
  62. image1 = null;
  63. }

  64. image1.src=imgD.src;  
  65. imgD.style.cursor = 'hand';
  66. imgD.onclick= function(){openWin(this.src,'imgphoto',600,400)};
  67. imgD.title = "点击在新窗口中查看原图";
  68. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生活导航|生活导航 ( 新ICP备12003026-1号 )

GMT+8, 2024-5-8 10:37 , Processed in 0.106719 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表