Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. var Marionette = require('backbone.marionette');
  2.  
  3. module.exports = app.Behaviors.ObjectFitPolyfill = Marionette.Behavior.extend({
  4.  
  5. onRender: function() {
  6. this.selector = this.getOption('selector');
  7. this.parentSelector = this.getOption('parentSelector');
  8. this.ignoreBreakpoint = this.getOption('ignoreBreakpoint');
  9.  
  10. this.objectFit();
  11.  
  12. $(window).on('resize.objectFit', _.debounce(_.bind(this.objectFitToParent, this), 10));
  13. },
  14.  
  15. objectFit: function() {
  16. if ($(window).width > this.ignoreBreakpoint) {
  17. if (!this.hasObjectFit()) {
  18. var self = this;
  19. $(this.selector, this.view.$el).each(function(i) {
  20. $(this)
  21. .on('load', _.bind(self.objectFitOnLoad, self))
  22. .addClass('invisible')
  23. .css({
  24. 'position': 'absolute',
  25. 'max-width': 'none'
  26. });
  27. });
  28. }
  29. }
  30. },
  31.  
  32. objectFitOnLoad: function(e) {
  33. var $img = $(e.target);
  34. var $container = $img.parents(this.parentSelector);
  35. this.objectFitGetNaturalSize(e.target, _.bind(function(naturalSize) {
  36. var containerSize = {
  37. width: $container.width(),
  38. height: this.getContainerHeight($container)
  39. };
  40.  
  41. console.log(containerSize.height);
  42. var horScale = containerSize.width / naturalSize.width;
  43. var verScale = containerSize.height / naturalSize.height;
  44. var scale = (horScale > verScale) ? horScale : verScale;
  45. var css = {};
  46. css.width = scale * naturalSize.width;
  47. css.height = scale * naturalSize.height;
  48. css.left = (containerSize.width - css.width) / 2;
  49. css.top = (containerSize.height - css.height) / 2;
  50. $img.css(css).removeClass('invisible');
  51. }, this));
  52. },
  53.  
  54. objectFitGetNaturalSize: function(img, callback) {
  55.  
  56. if (img.naturalWidth && img.naturalHeight) {
  57. callback({
  58. width: img.naturalWidth,
  59. height: img.naturalHeight
  60. });
  61. } else {
  62. var image = new Image();
  63. image.src = $(img).attr("src");
  64. image.onload = function() {
  65. callback({
  66. width: this.width,
  67. height: this.height
  68. });
  69. };
  70. }
  71. },
  72.  
  73. getContainerHeight: function($container) {
  74. var height = $container.height();
  75.  
  76. if (height <= 50) {
  77. if (document.documentElement.currentStyle) {
  78. height = $container.get(0).currentStyle.height;
  79. } else {
  80. height = window.getComputedStyle($container.get(0)).height;
  81. }
  82. }
  83.  
  84. return height;
  85. },
  86.  
  87. objectFitToParent: function() {
  88. if ($(window).width > this.ignoreBreakpoint) {
  89. var self = this;
  90. var naturalSize = {};
  91.  
  92. $(this.selector, this.view.$el).each(function() {
  93. var $container = $(this).parents(self.parentSelector);
  94. naturalSize.width = this.naturalWidth;
  95. naturalSize.height = this.naturalHeight;
  96. var containerSize = {
  97. width: $container.width(),
  98. height: self.getContainerHeight($container)
  99. };
  100. var horScale = containerSize.width / naturalSize.width;
  101. var verScale = containerSize.height / naturalSize.height;
  102. var scale = (horScale > verScale) ? horScale : verScale;
  103. var css = {};
  104. css.width = scale * naturalSize.width;
  105. css.height = scale * naturalSize.height;
  106. css.left = (containerSize.width - css.width) / 2;
  107. css.top = (containerSize.height - css.height) / 2;
  108. $(this).css(css).removeClass('invisible');
  109. });
  110. } else {
  111. $(this.selector, this.view.$el).each(function() {
  112. $(this).attr('style', '');
  113. });
  114. }
  115. },
  116.  
  117. hasObjectFit: function() {
  118. return 'objectFit' in document.documentElement.style;
  119. },
  120.  
  121. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement