用CSS和JS实现流畅的高质量图像缩放

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用CSS和JS实现流畅的高质量图像缩放相关的知识,希望对你有一定的参考价值。

  1. /*
  2.  
  3. http://unstoppablerobotninja.com/entry/fluid-images/
  4.  
  5. var imgSizer = {
  6. Config : {
  7. imgCache : []
  8. ,spacer : "/path/to/your/spacer.gif"
  9. }
  10.  
  11. ,collate : function(aScope) {
  12. var isOldIE = (document.all && !window.opera && !window.XDomainRequest) ? 1 : 0;
  13. if (isOldIE && document.getElementsByTagName) {
  14. var c = imgSizer;
  15. var imgCache = c.Config.imgCache;
  16.  
  17. var images = (aScope && aScope.length) ? aScope : document.getElementsByTagName("img");
  18. for (var i = 0; i < images.length; i++) {
  19. images[i].origWidth = images[i].offsetWidth;
  20. images[i].origHeight = images[i].offsetHeight;
  21.  
  22. imgCache.push(images[i]);
  23. c.ieAlpha(images[i]);
  24. images[i].style.width = "100%";
  25. }
  26.  
  27. if (imgCache.length) {
  28. c.resize(function() {
  29. for (var i = 0; i < imgCache.length; i++) {
  30. var ratio = (imgCache[i].offsetWidth / imgCache[i].origWidth);
  31. imgCache[i].style.height = (imgCache[i].origHeight * ratio) + "px";
  32. }
  33. });
  34. }
  35. }
  36. }
  37.  
  38. ,ieAlpha : function(img) {
  39. var c = imgSizer;
  40. if (img.oldSrc) {
  41. img.src = img.oldSrc;
  42. }
  43. var src = img.src;
  44. img.style.width = img.offsetWidth + "px";
  45. img.style.height = img.offsetHeight + "px";
  46. img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
  47. img.oldSrc = src;
  48. img.src = c.Config.spacer;
  49. }
  50.  
  51. // Ghettomodified version of Simon Willison's addLoadEvent() -- http://simonwillison.net/2004/May/26/addLoadEvent/
  52. ,resize : function(func) {
  53. var oldonresize = window.onresize;
  54. if (typeof window.onresize != 'function') {
  55. window.onresize = func;
  56. } else {
  57. window.onresize = function() {
  58. if (oldonresize) {
  59. oldonresize();
  60. }
  61. func();
  62. }
  63. }
  64. }
  65. }
  66. */
  67. img,
  68. object {
  69. max-width: 100%; /* Automatically scales height proportionally in FF, IE8, Safari >2 */
  70. }
  71.  
  72. img { -ms-interpolation-mode: bicubic; } /* IE8 scaling of JPEG */
  73.  

以上是关于用CSS和JS实现流畅的高质量图像缩放的主要内容,如果未能解决你的问题,请参考以下文章

高质量图像缩放库[关闭]

图像缩放算法

图像缩放算法

图像缩放算法

MFC 显示位图 缩放与拖动的矛盾

使用 css 裁剪或缩放图像