jquery.reflection.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. reflection.js for jQuery v1.12
  3. */
  4. ;(function($) {
  5. $.fn.reflect = function(options) {
  6. options = $.extend({
  7. height: 1/3,
  8. opacity: 0.5
  9. }, options);
  10. return this.unreflect().each(function() {
  11. var img = this;
  12. if (/^img$/i.test(img.tagName)) {
  13. function doReflect() {
  14. var imageWidth = img.width, imageHeight = img.height, reflection, reflectionHeight, wrapper, context, gradient;
  15. reflectionHeight = Math.floor((options.height > 1) ? Math.min(imageHeight, options.height) : imageHeight * options.height);
  16. reflection = $("<canvas />")[0];
  17. if (reflection.getContext) {
  18. context = reflection.getContext("2d");
  19. try {
  20. $(reflection).attr({width: imageWidth, height: reflectionHeight});
  21. context.save();
  22. context.translate(0, imageHeight-1);
  23. context.scale(1, -1);
  24. context.drawImage(img, 0, 0, imageWidth, imageHeight);
  25. context.restore();
  26. context.globalCompositeOperation = "destination-out";
  27. gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
  28. gradient.addColorStop(0, "rgba(255, 255, 255, " + (1 - options.opacity) + ")");
  29. gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
  30. context.fillStyle = gradient;
  31. context.rect(0, 0, imageWidth, reflectionHeight);
  32. context.fill();
  33. } catch(e) {
  34. return;
  35. }
  36. } else {
  37. if (!window.ActiveXObject) return;
  38. reflection = $("<img />").attr("src", img.src).css({
  39. width: imageWidth,
  40. height: imageHeight,
  41. marginBottom: reflectionHeight - imageHeight,
  42. filter: "FlipV progid:DXImageTransform.Microsoft.Alpha(Opacity=" + (options.opacity * 100) + ", FinishOpacity=0, Style=1, StartX=0, StartY=0, FinishX=0, FinishY=" + (reflectionHeight / imageHeight * 100) + ")"
  43. })[0];
  44. }
  45. $(reflection).css({display: "block", border: 0});
  46. wrapper = $(/^a$/i.test(img.parentNode.tagName) ? "<span />" : "<div />").insertAfter(img).append([img, reflection])[0];
  47. wrapper.className = img.className;
  48. $(img).data("reflected", wrapper.style.cssText = img.style.cssText);
  49. $(wrapper).css({width: imageWidth, height: imageHeight + reflectionHeight, overflow: "hidden"});
  50. img.style.cssText = "display: block; border: 0px";
  51. img.className = "reflected";
  52. }
  53. if (img.complete) doReflect();
  54. else $(img).load(doReflect);
  55. }
  56. });
  57. }
  58. $.fn.unreflect = function() {
  59. return this.unbind("load").each(function() {
  60. var img = this, reflected = $(this).data("reflected"), wrapper;
  61. if (reflected !== undefined) {
  62. wrapper = img.parentNode;
  63. img.className = wrapper.className;
  64. img.style.cssText = reflected;
  65. $(img).data("reflected", null);
  66. wrapper.parentNode.replaceChild(img, wrapper);
  67. }
  68. });
  69. }
  70. })(window.jQuery || window.Zepto);