zoom-image.min.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. !(function () {
  2. function o(e) {
  3. return 'IMG' === e.tagName
  4. }
  5. function E(e) {
  6. return e && 1 === e.nodeType
  7. }
  8. function c(e) {
  9. return '.svg' === (e.currentSrc || e.src).substr(-4).toLowerCase()
  10. }
  11. function m(e) {
  12. try {
  13. return Array.isArray(e)
  14. ? e.filter(o)
  15. : ((t = e), NodeList.prototype.isPrototypeOf(t) ? [].slice.call(e).filter(o) : E(e) ? [e].filter(o) : 'string' == typeof e ? [].slice.call(document.querySelectorAll(e)).filter(o) : [])
  16. } catch (e) {
  17. throw new TypeError('The provided selector is invalid.\nExpects a CSS selector, a Node element, a NodeList or an array.\nSee: https://github.com/francoischalifour/medium-zoom')
  18. }
  19. var t
  20. }
  21. function w(e, t) {
  22. var o = l({ bubbles: !1, cancelable: !1, detail: void 0 }, t)
  23. return 'function' == typeof window.CustomEvent ? new CustomEvent(e, o) : ((t = document.createEvent('CustomEvent')).initCustomEvent(e, o.bubbles, o.cancelable, o.detail), t)
  24. }
  25. function a(e, t) {
  26. function o(e) {
  27. function u() {
  28. var e = { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, left: 0, top: 0, right: 0, bottom: 0 },
  29. t = void 0,
  30. o = void 0
  31. v.container &&
  32. (v.container instanceof Object
  33. ? ((t = (e = l({}, e, v.container)).width - e.left - e.right - 2 * v.margin), (o = e.height - e.top - e.bottom - 2 * v.margin))
  34. : ((d = (i = (E(v.container) ? v.container : document.querySelector(v.container)).getBoundingClientRect()).width),
  35. (a = i.height),
  36. (r = i.left),
  37. (m = i.top),
  38. (e = l({}, e, { width: d, height: a, left: r, top: m })))),
  39. (t = t || e.width - 2 * v.margin),
  40. (o = o || e.height - 2 * v.margin)
  41. var n = z.zoomedHd || z.original,
  42. i = (!c(n) && n.naturalWidth) || t,
  43. d = (!c(n) && n.naturalHeight) || o,
  44. r = (a = n.getBoundingClientRect()).top,
  45. m = a.left,
  46. n = a.width,
  47. a = a.height,
  48. i = Math.min(i, t) / n,
  49. d = Math.min(d, o) / a,
  50. d = 'scale(' + (d = Math.min(i, d)) + ') translate3d(' + ((t - n) / 2 - m + v.margin + e.left) / d + 'px, ' + ((o - a) / 2 - r + v.margin + e.top) / d + 'px, 0)'
  51. ;(z.zoomed.style.transform = d), z.zoomedHd && (z.zoomedHd.style.transform = d)
  52. }
  53. var s = (0 < arguments.length && void 0 !== e ? e : {}).target
  54. return new i(function (e) {
  55. if (s && -1 === f.indexOf(s)) e(b)
  56. else {
  57. function t() {
  58. ;(g = !1), z.zoomed.removeEventListener('transitionend', t), z.original.dispatchEvent(w('medium-zoom:opened', { detail: { zoom: b } })), e(b)
  59. }
  60. var o, n, i, d, r, m, a, c, l
  61. if (z.zoomed) e(b)
  62. else {
  63. if (s) z.original = s
  64. else {
  65. if (!(0 < f.length)) return void e(b)
  66. z.original = f[0]
  67. }
  68. z.original.dispatchEvent(w('medium-zoom:open', { detail: { zoom: b } })),
  69. (h = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0),
  70. (g = !0),
  71. (z.zoomed =
  72. ((o = z.original),
  73. (n = o.getBoundingClientRect()),
  74. (i = n.top),
  75. (d = n.left),
  76. (r = n.width),
  77. (m = n.height),
  78. (a = o.cloneNode()),
  79. (n = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0),
  80. (o = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0),
  81. a.removeAttribute('id'),
  82. (a.style.position = 'absolute'),
  83. (a.style.top = i + n + 'px'),
  84. (a.style.left = d + o + 'px'),
  85. (a.style.width = r + 'px'),
  86. (a.style.height = m + 'px'),
  87. (a.style.transform = ''),
  88. a)),
  89. document.body.appendChild(y),
  90. v.template &&
  91. ((a = E(v.template) ? v.template : document.querySelector(v.template)),
  92. (z.template = document.createElement('div')),
  93. z.template.appendChild(a.content.cloneNode(!0)),
  94. document.body.appendChild(z.template)),
  95. document.body.appendChild(z.zoomed),
  96. window.requestAnimationFrame(function () {
  97. document.body.classList.add('medium-zoom--opened')
  98. }),
  99. z.original.classList.add('medium-zoom-image--hidden'),
  100. z.zoomed.classList.add('medium-zoom-image--opened'),
  101. z.zoomed.addEventListener('click', p),
  102. z.zoomed.addEventListener('transitionend', t),
  103. z.original.getAttribute('data-zoom-src')
  104. ? ((z.zoomedHd = z.zoomed.cloneNode()),
  105. z.zoomedHd.removeAttribute('srcset'),
  106. z.zoomedHd.removeAttribute('sizes'),
  107. (z.zoomedHd.src = z.zoomed.getAttribute('data-zoom-src')),
  108. (z.zoomedHd.onerror = function () {
  109. clearInterval(c), console.warn('Unable to reach the zoom image target ' + z.zoomedHd.src), (z.zoomedHd = null), u()
  110. }),
  111. (c = setInterval(function () {
  112. z.zoomedHd.complete &&
  113. (clearInterval(c), z.zoomedHd.classList.add('medium-zoom-image--opened'), z.zoomedHd.addEventListener('click', p), document.body.appendChild(z.zoomedHd), u())
  114. }, 10)))
  115. : z.original.hasAttribute('srcset')
  116. ? ((z.zoomedHd = z.zoomed.cloneNode()),
  117. z.zoomedHd.removeAttribute('sizes'),
  118. z.zoomedHd.removeAttribute('loading'),
  119. (l = z.zoomedHd.addEventListener('load', function () {
  120. z.zoomedHd.removeEventListener('load', l),
  121. z.zoomedHd.classList.add('medium-zoom-image--opened'),
  122. z.zoomedHd.addEventListener('click', p),
  123. document.body.appendChild(z.zoomedHd),
  124. u()
  125. })))
  126. : u()
  127. }
  128. }
  129. })
  130. }
  131. var n = 1 < arguments.length && void 0 !== t ? t : {},
  132. i =
  133. window.Promise ||
  134. function (e) {
  135. function t() {}
  136. e(t, t)
  137. },
  138. t = function () {
  139. for (var e = arguments, t = arguments.length, o = Array(t), n = 0; n < t; n++) o[n] = e[n]
  140. var i = o.reduce(function (e, t) {
  141. return [].concat(e, m(t))
  142. }, [])
  143. return (
  144. i
  145. .filter(function (e) {
  146. return -1 === f.indexOf(e)
  147. })
  148. .forEach(function (e) {
  149. f.push(e), e.classList.add('medium-zoom-image')
  150. }),
  151. r.forEach(function (e) {
  152. var t = e.type,
  153. o = e.listener,
  154. n = e.options
  155. i.forEach(function (e) {
  156. e.addEventListener(t, o, n)
  157. })
  158. }),
  159. b
  160. )
  161. },
  162. p = function () {
  163. return new i(function (t) {
  164. var e
  165. !g && z.original
  166. ? ((e = function e() {
  167. z.original.classList.remove('medium-zoom-image--hidden'),
  168. document.body.removeChild(z.zoomed),
  169. z.zoomedHd && document.body.removeChild(z.zoomedHd),
  170. document.body.removeChild(y),
  171. z.zoomed.classList.remove('medium-zoom-image--opened'),
  172. z.template && document.body.removeChild(z.template),
  173. (g = !1),
  174. z.zoomed.removeEventListener('transitionend', e),
  175. z.original.dispatchEvent(w('medium-zoom:closed', { detail: { zoom: b } })),
  176. (z.original = null),
  177. (z.zoomed = null),
  178. (z.zoomedHd = null),
  179. (z.template = null),
  180. t(b)
  181. }),
  182. (g = !0),
  183. document.body.classList.remove('medium-zoom--opened'),
  184. (z.zoomed.style.transform = ''),
  185. z.zoomedHd && (z.zoomedHd.style.transform = ''),
  186. z.template && ((z.template.style.transition = 'opacity 150ms'), (z.template.style.opacity = 0)),
  187. z.original.dispatchEvent(w('medium-zoom:close', { detail: { zoom: b } })),
  188. z.zoomed.addEventListener('transitionend', e))
  189. : t(b)
  190. })
  191. },
  192. d = function (e) {
  193. e = (0 < arguments.length && void 0 !== e ? e : {}).target
  194. return z.original ? p() : o({ target: e })
  195. },
  196. f = [],
  197. r = [],
  198. g = !1,
  199. h = 0,
  200. v = n,
  201. z = { original: null, zoomed: null, zoomedHd: null, template: null }
  202. '[object Object]' === Object.prototype.toString.call(e) ? (v = e) : (!e && 'string' != typeof e) || t(e),
  203. (v = l({ margin: 0, background: '#fff', scrollOffset: 40, container: null, template: null }, v))
  204. var y = ((n = v.background), (e = document.createElement('div')).classList.add('medium-zoom-overlay'), (e.style.background = n), e)
  205. document.addEventListener('click', function (e) {
  206. e = e.target
  207. e !== y ? -1 !== f.indexOf(e) && d({ target: e }) : p()
  208. }),
  209. document.addEventListener('keyup', function (e) {
  210. e = e.key || e.keyCode
  211. ;('Escape' !== e && 'Esc' !== e && 27 !== e) || p()
  212. }),
  213. document.addEventListener('scroll', function () {
  214. var e
  215. !g && z.original && ((e = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0), Math.abs(h - e) > v.scrollOffset && setTimeout(p, 150))
  216. }),
  217. window.addEventListener('resize', p)
  218. var b = {
  219. open: o,
  220. close: p,
  221. toggle: d,
  222. update: function (e) {
  223. var t = 0 < arguments.length && void 0 !== e ? e : {},
  224. e = t
  225. return (
  226. t.background && (y.style.background = t.background),
  227. t.container && t.container instanceof Object && (e.container = l({}, v.container, t.container)),
  228. t.template && ((t = E(t.template) ? t.template : document.querySelector(t.template)), (e.template = t)),
  229. (v = l({}, v, e)),
  230. f.forEach(function (e) {
  231. e.dispatchEvent(w('medium-zoom:update', { detail: { zoom: b } }))
  232. }),
  233. b
  234. )
  235. },
  236. clone: function (e) {
  237. return a(l({}, v, 0 < arguments.length && void 0 !== e ? e : {}))
  238. },
  239. attach: t,
  240. detach: function () {
  241. for (var e = arguments, t = arguments.length, o = Array(t), n = 0; n < t; n++) o[n] = e[n]
  242. z.zoomed && p()
  243. var i =
  244. 0 < o.length
  245. ? o.reduce(function (e, t) {
  246. return [].concat(e, m(t))
  247. }, [])
  248. : f
  249. return (
  250. i.forEach(function (e) {
  251. e.classList.remove('medium-zoom-image'), e.dispatchEvent(w('medium-zoom:detach', { detail: { zoom: b } }))
  252. }),
  253. (f = f.filter(function (e) {
  254. return -1 === i.indexOf(e)
  255. })),
  256. b
  257. )
  258. },
  259. on: function (t, o, e) {
  260. var n = 2 < arguments.length && void 0 !== e ? e : {}
  261. return (
  262. f.forEach(function (e) {
  263. e.addEventListener('medium-zoom:' + t, o, n)
  264. }),
  265. r.push({ type: 'medium-zoom:' + t, listener: o, options: n }),
  266. b
  267. )
  268. },
  269. off: function (t, o, e) {
  270. var n = 2 < arguments.length && void 0 !== e ? e : {}
  271. return (
  272. f.forEach(function (e) {
  273. e.removeEventListener('medium-zoom:' + t, o, n)
  274. }),
  275. (r = r.filter(function (e) {
  276. return !(e.type === 'medium-zoom:' + t && e.listener.toString() === o.toString())
  277. })),
  278. b
  279. )
  280. },
  281. getOptions: function () {
  282. return v
  283. },
  284. getImages: function () {
  285. return f
  286. },
  287. getZoomedImage: function () {
  288. return z.original
  289. },
  290. }
  291. return b
  292. }
  293. var l =
  294. Object.assign ||
  295. function (e) {
  296. for (var t = arguments, o = 1; o < arguments.length; o++) {
  297. var n,
  298. i = t[o]
  299. for (n in i) Object.prototype.hasOwnProperty.call(i, n) && (e[n] = i[n])
  300. }
  301. return e
  302. }
  303. !(function (e, t) {
  304. void 0 === t && (t = {})
  305. var o,
  306. n = t.insertAt
  307. e &&
  308. 'undefined' != typeof document &&
  309. ((o = document.head || document.getElementsByTagName('head')[0]),
  310. ((t = document.createElement('style')).type = 'text/css'),
  311. 'top' === n && o.firstChild ? o.insertBefore(t, o.firstChild) : o.appendChild(t),
  312. t.styleSheet ? (t.styleSheet.cssText = e) : t.appendChild(document.createTextNode(e)))
  313. })(
  314. '.medium-zoom-overlay{position:fixed;top:0;right:0;bottom:0;left:0;opacity:0;transition:opacity .3s;will-change:opacity}.medium-zoom--opened .medium-zoom-overlay{cursor:pointer;cursor:zoom-out;opacity:1}.medium-zoom-image{cursor:pointer;cursor:zoom-in;transition:transform .3s cubic-bezier(.2,0,.2,1)!important}.medium-zoom-image--hidden{visibility:hidden}.medium-zoom-image--opened{position:relative;cursor:pointer;cursor:zoom-out;will-change:transform}'
  315. )
  316. var n = Element.prototype.matches || Element.prototype.webkitMatchesSelector || Element.prototype.msMatchesSelector
  317. $docsify.plugins = [].concat(function (e) {
  318. var o
  319. e.doneEach(function (e) {
  320. var t = (t = Array.apply(null, document.querySelectorAll('.markdown-section img:not(.emoji):not([data-no-zoom])'))).filter(function (e) {
  321. return !1 === n.call(e, 'a img')
  322. })
  323. o && o.detach(), (o = a(t))
  324. })
  325. }, $docsify.plugins)
  326. })()