🏠 Home 

MaxShowBox

最大程度显示图片的库

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/518420/1532441/MaxShowBox.js

  1. // ==UserScript==
  2. // @name MaxShowBox
  3. // @version 2025.02.06
  4. // @description 最大程度显示图片的库
  5. // @author You
  6. // @grant none
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_deleteValue
  10. // @grant GM_download
  11. // @require https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js
  12. // ==/UserScript==
  13. /**
  14. * 让图片放大到原始或满屏大小
  15. * @class
  16. * @example
  17. const maxShowBox = new MaxShowBox();
  18. MaxShowBox.Add_Img(img);
  19. @ps 每调用一次Add_Img显示一次Img;
  20. */
  21. function MaxShowBox(){
  22. const box = `<div class="max-show-box">
  23. <button class="closeBox">X</button>
  24. </div>
  25. <style>
  26. .max-show-box{
  27. width:100vw;
  28. height:100vh;
  29. overflow:auto;
  30. z-index:9999;
  31. position:fixed;
  32. top:0;
  33. left:0;
  34. background:#434343;
  35. }
  36. .max-show-box button{
  37. width:20vmin;
  38. height:20vmin;
  39. border-radius:50%;
  40. position:fixed;
  41. bottom:0;
  42. left:50%;
  43. transform:translateX(-50%);
  44. background: #272727;
  45. color: #ffffff;
  46. opacity: .5;
  47. border: none;
  48. }
  49. .max-show-box img{
  50. min-width:100vw;
  51. min-height:100vh;
  52. max-width:none;
  53. max-height:none;
  54. width:auto !important;
  55. height:auto !important;
  56. }
  57. </style>
  58. `
  59. /**
  60. * 最大显示一张图片
  61. * @param {JQuery} img
  62. * @example maxShowBox.Add_Img(img);
  63. */
  64. this.Add_Img = (img)=>{
  65. CreatBox();
  66. Add_Img(img);
  67. }
  68. function CreatBox(){
  69. if($('.max-show-box').length==0){
  70. $('body').append(box);
  71. $('.max-show-box').hide();
  72. $('.max-show-box .closeBox').click(function(){
  73. $('.max-show-box').fadeOut();
  74. });
  75. }
  76. }
  77. function Add_Img(img){
  78. const newimg = $('<img />').attr('src',img[0].src);
  79. $('.max-show-box img').remove();
  80. $('.max-show-box').append(newimg);
  81. $('.max-show-box').fadeIn();
  82. }
  83. let autoRun = false;
  84. /**
  85. * 自动给图片加一个最大显示按钮,由body touchstart触发
  86. * @param null
  87. * @example maxShowBox.StartAutoRun();
  88. */
  89. this.StartAutoRun = ()=>{
  90. autoRun = true;
  91. $('body').on('touchstart',()=>{
  92. if(!autoRun){return;}
  93. $('img:not([add-max-show-box]):not(.max-show-box img)').each(function(){
  94. $(this).attr('add-max-show-box','yes');
  95. const parent = $(this).parent();
  96. if(parent.css("position")!="absolute"||parent.css("position")!="relative"){
  97. parent.css("position","relative");
  98. }
  99. const img = $(this);
  100. const bu = $('<div>B</div>').css({
  101. 'position': 'absolute',
  102. 'bottom': '0px',
  103. 'color': 'white',
  104. 'font-size': '10vw',
  105. 'background': '#0000005e'
  106. })
  107. img.after(bu);
  108. bu.click(function(event){
  109. event.stopPropagation();
  110. maxShowBox.Add_Img(img);
  111. ScrollToCenter($('.max-show-box'));
  112. });
  113. });
  114. });
  115. }
  116. /**
  117. * 停止给图片加一个最大显示按钮
  118. * @param null
  119. * @example maxShowBox.StopAutoRun();
  120. */
  121. this.StopAutoRun = ()=>{autoRun = false;}
  122. function ScrollToCenter(item){
  123. const sh = item[0].scrollHeight;
  124. const ch = item[0].clientHeight;
  125. const y = (sh-ch)/2;
  126. const sw = item[0].scrollWidth;
  127. const cw = item[0].clientWidth;
  128. const x = (sw-cw)/2;
  129. item[0].scrollTop = y;
  130. item[0].scrollLeft = x;
  131. }
  132. }
  133. const maxShowBox = new MaxShowBox();
  134. window.MaxShowBox = maxShowBox;