SmallImageZoom

Enables Maxthon4 to zoom in images smaller than the screen to fit into the window or zoom back to its original size.

当前为 2014-10-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SmallImageZoom
  3. // @author ElDoRado1239
  4. // @version 0.93
  5. // @description Enables Maxthon4 to zoom in images smaller than the screen to fit into the window or zoom back to its original size.
  6. // @include *.jpg
  7. // @include *.jpeg
  8. // @include *.png
  9. // @include *.gif
  10. // @namespace https://greasyfork.org/users/6103
  11. // ==/UserScript==
  12.  
  13. var img = document.getElementById('img_elem');
  14. var drag = false;
  15. var mdown = false;
  16. var state = 0;
  17. setTimeout(init,10);
  18. function init(){
  19. if(img.naturalWidth == 0) setTimeout(init,10);
  20. else{
  21. if(img.naturalHeight >= window.innerHeight || img.naturalWidth >= window.innerWidth){
  22. img.onmousemove = undefined;
  23. img.onmouseup = undefined;
  24. img.onmousedown = undefined;
  25. return;
  26. }
  27. img.onmousedown = mouseDown;
  28. img.onmousemove = mouseMove;
  29. img.onmouseup = mouseUp;
  30. window.onresize = mouseUp;
  31. mouseUp('init');
  32. }
  33. }
  34. function mouseDown(e){
  35. if(e.which != 3) mdown = true;
  36. }
  37. function mouseMove(){
  38. if(mdown){
  39. drag = true;
  40. img.style.cursor = "all-scroll";
  41. }
  42. }
  43. function mouseUp(e){
  44. if(mdown==false && e!='init') return;
  45. mdown = false;
  46. switch(state){
  47. case 0:{
  48. if(drag){
  49. drag = false;
  50. img.style.cursor = "-webkit-zoom-in";
  51. return;
  52. }
  53. if(img.naturalHeight>=img.naturalWidth){
  54. img.style.height = window.innerHeight;
  55. img.style.width = window.innerHeight*(img.naturalWidth/img.naturalHeight);
  56. img.style.left = ((window.innerWidth-parseInt(img.style.width))/2)+"px";
  57. img.style.top = "0px";
  58. }
  59. if(img.naturalWidth>img.naturalHeight){
  60. img.style.width = window.innerWidth;
  61. img.style.height = window.innerWidth*(img.naturalHeight/img.naturalWidth);
  62. img.style.left = "0px";
  63. img.style.top = ((window.innerHeight-parseInt(img.style.height))/2)+"px";
  64. }
  65. if(parseInt(img.style.width)>window.innerWidth){
  66. img.style.width = window.innerWidth;
  67. img.style.height = window.innerWidth*(img.naturalHeight/img.naturalWidth);
  68. img.style.left = "0px";
  69. img.style.top = ((window.innerHeight-parseInt(img.style.height))/2)+"px";
  70. }
  71. if(parseInt(img.style.height)>window.innerHeight){
  72. img.style.height = window.innerHeight;
  73. img.style.width = window.innerHeight*(img.naturalWidth/img.naturalHeight);
  74. img.style.left = ((window.innerWidth-parseInt(img.style.width))/2)+"px";
  75. img.style.top = "0px";
  76. }
  77. img.style.cursor = "-webkit-zoom-out";
  78. state++;
  79. return;
  80. }
  81. case 1:{
  82. if(drag){
  83. drag = false;
  84. img.style.cursor = "-webkit-zoom-out";
  85. return;
  86. }
  87. img.style.width = img.naturalWidth;
  88. img.style.height = img.naturalHeight;
  89. img.style.left = ((window.innerWidth-parseInt(img.style.width))/2)+"px";
  90. img.style.top = ((window.innerHeight-parseInt(img.style.height))/2)+"px";
  91. img.style.cursor = "-webkit-zoom-in";
  92. state--;
  93. return;
  94. }
  95. }
  96. }