自动等比例缩放网页中的图片

resizeimg.js

  1. window.onload = function() {
  2.  
  3.     for (var index = 0; index < document.images.length; index++) {
  4.  
  5.         var widthRestriction = 400;
  6.         var heightRestriction = 400;
  7.         var rate = document.images[index].width / document.images[index].height;
  8.  
  9.         if (document.images[index].width > widthRestriction) {
  10.             document.images[index].width = widthRestriction;
  11.             document.images[index].height = widthRestriction / rate;
  12.         } else if (document.images[index].height > heightRestriction) {
  13.             document.images[index].height = heightRestriction;
  14.             document.images[index].width = heightRestriction * rate;
  15.         }
  16.  
  17.         document.images[index].onclick = function() {window.open(this.src)};
  18.         document.images[index].title = document.images[index].title + ' 点击在新窗口中查看原图';
  19.     }
  20. }
Tags: javascript, 图片, 网页设计

相关日志

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

还没有评论。

发表评论

(必填)

(必填)


*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comment moderation is enabled. Your comment may take some time to appear.