portal html css js resource

zoomify.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Zoomify
  3. * A jQuery plugin for simple lightboxes with zoom effect.
  4. * http://indrimuska.github.io/zoomify
  5. *
  6. * (c) 2015 Indri Muska - MIT
  7. */
  8. ;(function($){
  9. // initialization
  10. Zoomify = function (element, options) {
  11. var that = this;
  12. this._zooming = false;
  13. this._zoomed = false;
  14. this._timeout = null;
  15. this.$shadow = null;
  16. this.$image = $(element).addClass('zoomify');
  17. this.options = $.extend({}, Zoomify.DEFAULTS, this.$image.data(), options);
  18. this.$image.on('click', function () { that.zoom(); });
  19. $(window).on('resize', function () { that.reposition(); });
  20. $(document).on('scroll', function () { that.reposition(); });
  21. };
  22. Zoomify.DEFAULTS = {
  23. duration: 200,
  24. easing: 'linear',
  25. scale: 0.9
  26. };
  27. // css utilities
  28. Zoomify.prototype.transition = function ($element, value) {
  29. $element.css({
  30. '-webkit-transition': value,
  31. '-moz-transition': value,
  32. '-ms-transition': value,
  33. '-o-transition': value,
  34. 'transition': value
  35. });
  36. };
  37. Zoomify.prototype.addTransition = function ($element) {
  38. this.transition($element, 'all ' + this.options.duration + 'ms ' + this.options.easing);
  39. };
  40. Zoomify.prototype.removeTransition = function ($element, callback) {
  41. var that = this;
  42. clearTimeout(this._timeout);
  43. this._timeout = setTimeout(function () {
  44. that.transition($element, '');
  45. if ($.isFunction(callback)) callback.call(that);
  46. }, this.options.duration);
  47. };
  48. Zoomify.prototype.transform = function (value) {
  49. this.$image.css({
  50. '-webkit-transform': value,
  51. '-moz-transform': value,
  52. '-ms-transform': value,
  53. '-o-transform': value,
  54. 'transform': value
  55. });
  56. };
  57. Zoomify.prototype.transformScaleAndTranslate = function (scale, translateX, translateY, callback) {
  58. this.addTransition(this.$image);
  59. this.transform('scale(' + scale + ') translate(' + translateX + 'px, ' + translateY + 'px)');
  60. this.removeTransition(this.$image, callback);
  61. };
  62. // zooming functions
  63. Zoomify.prototype.zoom = function () {
  64. if (this._zooming) return;
  65. if (this._zoomed) this.zoomOut();
  66. else this.zoomIn();
  67. };
  68. Zoomify.prototype.zoomIn = function () {
  69. var that = this,
  70. transform = this.$image.css('transform');
  71. this.$image.css("opacity",1)
  72. this.$image.css("height","auto")
  73. this.transition(this.$image, 'none');
  74. this.transform('none');
  75. var offset = this.$image.offset(),
  76. width = this.$image.outerWidth(),
  77. height = this.$image.outerHeight(),
  78. nWidth = this.$image[0].naturalWidth || +Infinity,
  79. nHeight = this.$image[0].naturalHeight || +Infinity,
  80. wWidth = $(window).width(),
  81. wHeight = $(window).height(),
  82. scaleX = Math.min(nWidth, wWidth * this.options.scale) / width,
  83. scaleY = Math.min(nHeight, wHeight * this.options.scale) / height,
  84. scale = Math.min(scaleX, scaleY),
  85. translateX = (-offset.left + (wWidth - width) / 2) / scale,
  86. translateY = (-offset.top + (wHeight - height) / 2 + $(document).scrollTop()) / scale;
  87. this.transform(transform);
  88. this._zooming = true;
  89. this.$image.addClass('zoomed').trigger('zoom-in.zoomify');
  90. setTimeout(function () {
  91. that.addShadow();
  92. that.transformScaleAndTranslate(scale, translateX, translateY, function () {
  93. that._zooming = false;
  94. that.$image.trigger('zoom-in-complete.zoomify');
  95. });
  96. that._zoomed = true;
  97. });
  98. };
  99. Zoomify.prototype.zoomOut = function () {
  100. var that = this;
  101. this.$image.css("opacity",0)
  102. this.$image.css("height","100%")
  103. this._zooming = true;
  104. this.$image.trigger('zoom-out.zoomify');
  105. this.transformScaleAndTranslate(1, 0, 0, function () {
  106. that._zooming = false;
  107. that.$image.removeClass('zoomed').trigger('zoom-out-complete.zoomify');
  108. });
  109. this.removeShadow();
  110. this._zoomed = false;
  111. };
  112. // page listener callbacks
  113. Zoomify.prototype.reposition = function () {
  114. if (this._zoomed) {
  115. this.transition(this.$image, 'none');
  116. this.zoomIn();
  117. }
  118. };
  119. // shadow background
  120. Zoomify.prototype.addShadow = function () {
  121. var that = this;
  122. if (this._zoomed) return;
  123. if (that.$shadow) that.$shadow.remove();
  124. this.$shadow = $('<div class="zoomify-shadow"></div>');
  125. $('body').append(this.$shadow);
  126. this.addTransition(this.$shadow);
  127. this.$shadow.on('click', function () { that.zoomOut(); })
  128. setTimeout(function () { that.$shadow.addClass('zoomed'); }, 10);
  129. };
  130. Zoomify.prototype.removeShadow = function () {
  131. var that = this;
  132. if (!this.$shadow) return;
  133. this.addTransition(this.$shadow);
  134. this.$shadow.removeClass('zoomed');
  135. this.$image.one('zoom-out-complete.zoomify', function () {
  136. if (that.$shadow) that.$shadow.remove();
  137. that.$shadow = null;
  138. });
  139. };
  140. // plugin definition
  141. $.fn.zoomify = function (option) {
  142. return this.each(function () {
  143. var $this = $(this),
  144. zoomify = $this.data('zoomify');
  145. if (!zoomify) $this.data('zoomify', (zoomify = new Zoomify(this, typeof option == 'object' && option)));
  146. if (typeof option == 'string' && ['zoom', 'zoomIn', 'zoomOut', 'reposition'].indexOf(option) >= 0) zoomify[option]();
  147. });
  148. };
  149. })(jQuery);