MyModal

原生js弹出层

ეს სკრიპტი არ უნდა იყოს პირდაპირ დაინსტალირებული. ეს ბიბლიოთეკაა, სხვა სკრიპტებისთვის უნდა ჩართეთ მეტა-დირექტივაში // @require https://update.greatest.deepsurf.us/scripts/463930/1177945/MyModal.js.

  1. // ==UserScript==
  2. // @name MyModal
  3. // @namespace http://https://wish123.cnblogs.com/?MyModal
  4. // @version 0.1.1.2
  5. // @description 原生js弹出层
  6. // @author Wilson
  7.  
  8. function MyModal(options) {
  9. this.zIndex = 1010
  10. this.config = options || {}
  11. this.modal = null;
  12. if(this.config) {
  13. this.createStyle();
  14. this.create();
  15. }
  16. }
  17. MyModal.prototype.createStyle = function() {
  18. if(document.querySelector("#myModalStyle")) {
  19. return;
  20. }
  21. let style = `
  22. <style id="myModalStyle">
  23. /* The Modal (background) */
  24. .my-modal {
  25. display: none; /* Hidden by default */
  26. position: fixed; /* Stay in place */
  27. z-index: 1010; /* Sit on top */
  28. padding-top: 100px;
  29. left: 0;
  30. top: 0;
  31. width: 100%; /* Full width */
  32. height: 100%; /* Full height */
  33. overflow: auto; /* Enable scroll if needed */
  34. background-color: rgb(0,0,0); /* Fallback color */
  35. background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  36. }
  37.  
  38. .my-modal button {
  39. background-color: #1c9fff;
  40. color: white;
  41. padding: 14px 20px;
  42. margin: 8px 0;
  43. border: none;
  44. cursor: pointer;
  45. width: 100%;
  46. opacity: 0.9;
  47. }
  48.  
  49. .my-modal button:hover {
  50. opacity:1;
  51. }
  52.  
  53. .my-modal h2{
  54. margin: 15px 0;
  55. }
  56.  
  57. /* Modal Content */
  58. .my-modal .modal-content {
  59. position: relative;
  60. background-color: #fefefe;
  61. margin: auto;
  62. padding: 0;
  63. border: 1px solid #888;
  64. width: 50%;
  65. box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  66. -webkit-animation-name: animatetop;
  67. -webkit-animation-duration: 0.4s;
  68. animation-name: animatetop;
  69. animation-duration: 0.4s;
  70. border-radius: 5px;
  71. }
  72.  
  73. .my-modal .modal-body-content{
  74. font-size: 16px;
  75. }
  76.  
  77. /* Add Animation */
  78. @-webkit-keyframes animatetop {
  79. from {top:-300px; opacity:0}
  80. to {top:0; opacity:1}
  81. }
  82.  
  83. @keyframes animatetop {
  84. from {top:-300px; opacity:0}
  85. to {top:0; opacity:1}
  86. }
  87.  
  88. /* The Close Button */
  89. .my-modal .close {
  90. color: #333;
  91. float: right;
  92. font-size: 28px;
  93. font-weight: bold;
  94. }
  95.  
  96. .my-modal .modal-header .close{
  97. margin-top: 6px;
  98. }
  99.  
  100. .my-modal .close:hover,
  101. .my-modal .close:focus {
  102. color: #333;
  103. text-decoration: none;
  104. cursor: pointer;
  105. opacity: 0.60;
  106. }
  107.  
  108. .my-modal .modal-header {
  109. padding: 2px 16px;
  110. background-color: #fff;
  111. color: #333;
  112. border-bottom: 1px solid #f0f0f0;
  113. border-radius: 5px;
  114. }
  115.  
  116. .my-modal .modal-body {
  117. padding: 10px 16px;
  118. min-height: 28px;
  119. line-height: 28px;
  120. overflow: auto;
  121. }
  122.  
  123. .my-modal .modal-footer {
  124. padding: 2px 16px;
  125. background-color: #fff;
  126. color: #333;
  127. /* border-top: 1px solid #f0f0f0; */
  128. border-radius: 5px;
  129. }
  130.  
  131. .clearfix::after {
  132. content: "";
  133. clear: both;
  134. display: table;
  135. }
  136.  
  137. .my-modal .cancelbtn,.my-modal .okbtn {
  138. float: right;
  139. width: 50%;
  140. }
  141.  
  142. /* Add a color to the cancel button */
  143. .my-modal .cancelbtn {
  144. background-color: #f1f1f1;
  145. color: #333;
  146. /* border: 1px solid #dedede; */
  147. }
  148.  
  149. /* Add a color to the delete button */
  150. .my-modal .okbtn {
  151. background-color: #1c9fff;
  152. }
  153.  
  154. @media screen and (max-width: 300px) {
  155. .my-modal .cancelbtn,.my-modal .okbtn {
  156. width: 100%;
  157. }
  158. }
  159. </style>
  160. `
  161. document.body.insertAdjacentHTML("beforeend", style);
  162. }
  163. MyModal.prototype.create = function(options) {
  164. options = options || this.config
  165. if(document.querySelector("#myModal")) {
  166. document.querySelector("#myModal").remove();
  167. }
  168. let width = options.width ? 'width:'+options.width+';' : '';
  169. let height = options.height ? 'height:'+options.height+';' : '';
  170. let borderRadius = options.borderRadius ? 'border-radius:'+options.borderRadius+';' : '';
  171. let zIndex = options.zIndex ? 'z-index:'+options.zIndex+';' : '';
  172. let myModal = `
  173. <div id="myModal" class="my-modal" style="${zIndex}${options.top?'padding-top:'+options.top+';':''}">
  174. <div class="modal-content" style="${width}${height}${borderRadius}">
  175. <div class="modal-header" style="${borderRadius}${options.title===null?'display:none;':''}${options.content===null?'border-bottom:none':''}">
  176. <span class="close">&times;</span>
  177. <h2>${options.title||''}</h2>
  178. </div>
  179. <div class="modal-body" style="${options.content===null?'display:none;':''}">
  180. ${options.title===null?'<span class="close">&times;</span>':''}
  181. <div class="modal-body-content">${options.content||''}</div>
  182. </div>
  183. <div class="modal-footer" style="${borderRadius}${options.okText===null&&options.closeText===null?'display:none;':''}">
  184. <div class="clearfix">
  185. <button type="button" class="okbtn" style="${options.okText===null?'display:none;':''}${options.okWidth?'width:'+options.okWidth+';':''}">${options.okText||'OK'}</button>
  186. <button type="button" class="cancelbtn" style="${options.closeText===null?'display:none;':''}${options.closeWidth?'width:'+options.closeWidth+';':''}">${options.closeText||'Cancel'}</button>
  187. </div>
  188. </div>
  189. </div>
  190. </div>`
  191. document.body.insertAdjacentHTML("beforeend", myModal);
  192. this.modal = document.querySelector(`#myModal`);
  193.  
  194. if(options.height) {
  195. document.querySelector("#myModal .modal-body").style.height = (parseFloat(options.height) - 125) + 'px';
  196. }
  197.  
  198. let modalContent = document.querySelector(".modal-content");
  199. setTimeout(()=>{
  200. if(modalContent.offsetHeight){
  201. let padding = document.documentElement.clientHeight - modalContent.offsetHeight;
  202. padding = padding > 0 ? padding : 0;
  203. this.modal.style.paddingTop = (padding/2)+'px';
  204. }
  205. });
  206.  
  207. let _this = this;
  208. //绑定关闭事件
  209. let headerCloseBtn = document.querySelector(`#myModal .modal-header .close`);
  210. if(headerCloseBtn) {
  211. headerCloseBtn.addEventListener("click", function(e){
  212. _this.close();
  213. });
  214. }
  215. let bodyCloseBtn = document.querySelector(`#myModal .modal-body .close`);
  216. if(bodyCloseBtn){
  217. bodyCloseBtn.addEventListener("click", function(e){
  218. _this.close();
  219. });
  220. }
  221. //绑定cancel事件
  222. document.querySelector(`#myModal .cancelbtn`).addEventListener("click", function(e){
  223. if(_this.config.closeFn) {
  224. e.myModal = _this;
  225. _this.config.closeFn(e);
  226. } else {
  227. _this.close();
  228. }
  229. });
  230. //绑定OK事件
  231. document.querySelector(`#myModal .okbtn`).addEventListener("click", function(e){
  232. if(_this.config.okFn) {
  233. e.myModal = _this;
  234. _this.config.okFn(e);
  235. } else {
  236. _this.close();
  237. }
  238. });
  239. //点击空白,菜单消失
  240. document.addEventListener('click', function(e){
  241. if (e.target == _this.modal) {
  242. _this.close();
  243. }
  244. });
  245. }
  246. MyModal.prototype.show = function() {
  247. if(this.modal) {
  248. this.modal.style.display = 'block';
  249. }
  250. }
  251. MyModal.prototype.close = function() {
  252. if(this.modal) {
  253. this.modal.remove();
  254. }
  255. }
  256.  
  257. // //测试1
  258. // document.querySelector("#test1").addEventListener("click", function(){
  259. // new MyModal({
  260. // top: '',
  261. // width: '50%',
  262. // height: 'auto',
  263. // borderRadius: '5px',
  264. // zIndex: 1010,
  265. // //null,不显示title
  266. // title: 'test1',
  267. // //支持HTML格式,null不显示content
  268. // content: 'Hello World!',
  269. // //closeText:null,不显示关闭按钮
  270. // closeText: '关闭',
  271. // closeWidth: '',
  272. // //okText:null,不显示ok按钮
  273. // okText: '好了',
  274. // okWidth: '',
  275. // //closeFn和okFn可以省略
  276. // closeFn: function (e) {
  277. // console.log('closeFn clicked');
  278. // e.myModal.close();
  279. // },
  280. // okFn: function (e) {
  281. // console.log('okFn clicked');
  282. // e.myModal.close();
  283. // }
  284. // }).show();
  285. // });
  286.  
  287. // //测试2
  288. // document.querySelector("#test2").addEventListener("click", function(){
  289. // new MyModal({
  290. // top: '',
  291. // width: '50%',
  292. // height: 'auto',
  293. // borderRadius: '5px',
  294. // zIndex: 1010,
  295. // title: 'test2',
  296. // content: 'Hello World2!',
  297. // closeText: '取消',
  298. // closeWidth: '',
  299. // okText: '确定',
  300. // okWidth: '',
  301. // closeFn: function (e) {
  302. // console.log('closeFn clicked');
  303. // e.myModal.close();
  304. // },
  305. // okFn: function (e) {
  306. // console.log('okFn clicked');
  307. // e.myModal.close();
  308. // }
  309. // }).show();
  310. // });