| @@ 8-225 (lines=218) @@ | ||
| 5 | ||
| 6 | var elementResizeDetectorMaker = require('element-resize-detector'); |
|
| 7 | ||
| 8 | var CoverImage = function () { |
|
| 9 | function CoverImage(el, cb) { |
|
| 10 | _classCallCheck(this, CoverImage); |
|
| 11 | ||
| 12 | var _this = this; |
|
| 13 | ||
| 14 | _this.$el = el ? el : window; |
|
| 15 | _this.$img = _this.getElementForSizing(); |
|
| 16 | _this.disableOnMobile = _this.$el.dataset['cover-image-mobile'] === 'false'; |
|
| 17 | _this.cb = cb || function () { |
|
| 18 | //DEBUG console.log("Default callback"); |
|
| 19 | }; |
|
| 20 | ||
| 21 | _this.positioning = { |
|
| 22 | x: 0.5, |
|
| 23 | y: 0.5 |
|
| 24 | }; |
|
| 25 | _this.options = { |
|
| 26 | parallax: _this.$el.dataset.coverImageParallax === 'true' |
|
| 27 | }; |
|
| 28 | ||
| 29 | if (!_this.$img) { |
|
| 30 | console.log('Error:', 'no image found', _this.$img); |
|
| 31 | return; |
|
| 32 | } |
|
| 33 | ||
| 34 | _this.imageWidth = _this.$img.width; |
|
| 35 | _this.imageHeight = _this.$img.height; |
|
| 36 | ||
| 37 | // If the image doesn't have harcoded width|height |
|
| 38 | // attributes then load the image to calculate |
|
| 39 | // the dimensions |
|
| 40 | if (!_this.imageWidth || !_this.imageHeight) { |
|
| 41 | console.log('No dimensions found. Generating image:', _this.$img.attr('src')); |
|
| 42 | _this.img = new Image(); |
|
| 43 | _this.img.src = _this.$img.attr('src'); |
|
| 44 | ||
| 45 | _this.imageWidth = _this.img.width; |
|
| 46 | _this.imageHeight = _this.img.height; |
|
| 47 | } |
|
| 48 | ||
| 49 | if (_this.disableOnMobile && window.innerWidth < 480) { |
|
| 50 | return; |
|
| 51 | } |
|
| 52 | ||
| 53 | _this.elementDimensions = { |
|
| 54 | height: _this.$el.clientHeight, |
|
| 55 | width: _this.$el.clientWidth |
|
| 56 | }; |
|
| 57 | ||
| 58 | _this.$el.style = '\n\t\t\toverflow : hidden;\n\t\t\tposition : relative;\n\t\t'; |
|
| 59 | ||
| 60 | if (!_this.$img) { |
|
| 61 | // TODO: Implement load |
|
| 62 | setTimeout(function () { |
|
| 63 | new CoverImage(_this.$el); |
|
| 64 | }, 1000); |
|
| 65 | } else { |
|
| 66 | _this.resizeImage(); |
|
| 67 | } |
|
| 68 | ||
| 69 | _this.$img.addEventListener('load', function () { |
|
| 70 | _this.resizeImage(); |
|
| 71 | }, false); |
|
| 72 | ||
| 73 | _this.$el.addEventListener('transitionend', function () { |
|
| 74 | _this.resizeImage(); |
|
| 75 | }, false); |
|
| 76 | ||
| 77 | var erd = elementResizeDetectorMaker({ |
|
| 78 | strategy: 'scroll' |
|
| 79 | }); |
|
| 80 | ||
| 81 | erd.listenTo(_this.$el, function () { |
|
| 82 | _this.resizeImage(); |
|
| 83 | }); |
|
| 84 | ||
| 85 | _this.$el.addEventListener('animationend', function () { |
|
| 86 | _this.resizeImage(); |
|
| 87 | }, false); |
|
| 88 | ||
| 89 | window.addEventListener('resize', function () { |
|
| 90 | _this.resizeImage(); |
|
| 91 | }, true); |
|
| 92 | ||
| 93 | window.addEventListener('ci.resize', function () { |
|
| 94 | _this.resizeImage(); |
|
| 95 | }, true); |
|
| 96 | ||
| 97 | if (_this.options.parallax) { |
|
| 98 | _this.draw(); |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||
| 102 | /** |
|
| 103 | * Parallax FX |
|
| 104 | * |
|
| 105 | */ |
|
| 106 | ||
| 107 | ||
| 108 | _createClass(CoverImage, [{ |
|
| 109 | key: 'draw', |
|
| 110 | value: function draw() { |
|
| 111 | var _this = this; |
|
| 112 | var friction = 0.5; |
|
| 113 | var imageOffsetX = document.body.scrollTop * friction; |
|
| 114 | var imageOffsetY = document.body.scrollTop * friction; |
|
| 115 | var maximumMovementY = (_this.imageDimensions.height - _this.elementDimensions.height) * _this.positioning.y; |
|
| 116 | var maximumMovementX = (_this.imageDimensions.width - _this.elementDimensions.width) * _this.positioning.x; |
|
| 117 | ||
| 118 | if (maximumMovementX > 0) { |
|
| 119 | if (imageOffsetX < maximumMovementX) { |
|
| 120 | // console.log('New position:', maximumMovementX - imageOffsetX); |
|
| 121 | _this.$img.css({ |
|
| 122 | 'transform': 'translateX(' + (maximumMovementX - imageOffsetX) + 'px)' |
|
| 123 | }); |
|
| 124 | } |
|
| 125 | } else { |
|
| 126 | if (imageOffsetY < maximumMovementY) { |
|
| 127 | _this.$img.css('transform', 'translateY(' + (maximumMovementY - imageOffsetY) + 'px)'); |
|
| 128 | } |
|
| 129 | } |
|
| 130 | ||
| 131 | window.requestAnimationFrame(function () { |
|
| 132 | _this.draw(); |
|
| 133 | }); |
|
| 134 | } |
|
| 135 | ||
| 136 | /** |
|
| 137 | * |
|
| 138 | * @return DOM Element |
|
| 139 | */ |
|
| 140 | ||
| 141 | }, { |
|
| 142 | key: 'getElementForSizing', |
|
| 143 | value: function getElementForSizing() { |
|
| 144 | var _this = this; |
|
| 145 | var selector = _this.$el.dataset['coverImageEl']; |
|
| 146 | ||
| 147 | if (selector) { |
|
| 148 | ||
| 149 | console.log('Element selector Present', _this.$el.find(selector)); |
|
| 150 | ||
| 151 | return _this.$el.find(selector) ? _this.$el.find(selector) : _this.$el.find('img'); |
|
| 152 | } |
|
| 153 | ||
| 154 | return _this.$el.querySelector('img'); |
|
| 155 | } |
|
| 156 | }, { |
|
| 157 | key: 'resizeImage', |
|
| 158 | value: function resizeImage() { |
|
| 159 | var _this = this; |
|
| 160 | var elementWidth = _this.$el.clientWidth; |
|
| 161 | var elementHeight = _this.$el.clientHeight; |
|
| 162 | var dimensions = _this.coverDimensions(_this.imageWidth, _this.imageHeight, elementWidth, elementHeight); |
|
| 163 | ||
| 164 | _this.imageDimensions = dimensions; |
|
| 165 | ||
| 166 | if (isNaN(dimensions.width)) { |
|
| 167 | console.log('Failed to calculate image sizes.'); |
|
| 168 | } |
|
| 169 | ||
| 170 | _this.setImageSize(); |
|
| 171 | } |
|
| 172 | }, { |
|
| 173 | key: 'setImageSize', |
|
| 174 | value: function setImageSize() { |
|
| 175 | var _this = this; |
|
| 176 | ||
| 177 | _this.$img.width = _this.imageDimensions.width; |
|
| 178 | _this.$img.height = _this.imageDimensions.height; |
|
| 179 | ||
| 180 | var transform = _this.getTransform((_this.$el.clientWidth - _this.imageDimensions.width) * _this.positioning.y, (_this.$el.clientHeight - _this.imageDimensions.height) * _this.positioning.x); |
|
| 181 | ||
| 182 | _this.$img.style = '\n\t\t\tposition: absolute;\n\t\t\twidth: ' + _this.imageDimensions.width + ';\n\t\t\theight: ' + _this.imageDimensions.height + ';\n\t\t\ttransform: ' + transform + ';\n\t\t\tmax-width: none;\n\t\t'; |
|
| 183 | ||
| 184 | _this.$img.classList.add('ci--sized'); |
|
| 185 | _this.cb(); |
|
| 186 | } |
|
| 187 | }, { |
|
| 188 | key: 'getTransform', |
|
| 189 | value: function getTransform(x, y) { |
|
| 190 | return 'translate3d(' + x + 'px,' + y + 'px,0)'; |
|
| 191 | } |
|
| 192 | }, { |
|
| 193 | key: 'coverDimensions', |
|
| 194 | value: function coverDimensions(child_w, child_h, container_w, container_h) { |
|
| 195 | var scale_factor = this.max(container_w / child_w, container_h / child_h); |
|
| 196 | ||
| 197 | return { |
|
| 198 | width: Math.ceil(child_w * scale_factor), |
|
| 199 | height: Math.ceil(child_h * scale_factor) |
|
| 200 | }; |
|
| 201 | } |
|
| 202 | }, { |
|
| 203 | key: 'containDimensions', |
|
| 204 | value: function containDimensions(child_w, child_h, container_w, container_h) { |
|
| 205 | var scale_factor = this.min(container_w / child_w, container_h / child_h); |
|
| 206 | ||
| 207 | return { |
|
| 208 | width: child_w * scale_factor, |
|
| 209 | height: child_h * scale_factor |
|
| 210 | }; |
|
| 211 | } |
|
| 212 | }, { |
|
| 213 | key: 'min', |
|
| 214 | value: function min(a, b) { |
|
| 215 | return a > b ? b : a; |
|
| 216 | } |
|
| 217 | }, { |
|
| 218 | key: 'max', |
|
| 219 | value: function max(a, b) { |
|
| 220 | return a > b ? a : b; |
|
| 221 | } |
|
| 222 | }]); |
|
| 223 | ||
| 224 | return CoverImage; |
|
| 225 | }(); |
|
| 226 | ||
| 227 | var elems = document.body.querySelectorAll('[data-cover-image]'); |
|
| 228 | ||
| @@ 7-224 (lines=218) @@ | ||
| 4 | ||
| 5 | var elementResizeDetectorMaker = require('element-resize-detector'); |
|
| 6 | ||
| 7 | var CoverImage = function () { |
|
| 8 | function CoverImage(el, cb) { |
|
| 9 | _classCallCheck(this, CoverImage); |
|
| 10 | ||
| 11 | var _this = this; |
|
| 12 | ||
| 13 | _this.$el = el ? el : window; |
|
| 14 | _this.$img = _this.getElementForSizing(); |
|
| 15 | _this.disableOnMobile = _this.$el.dataset['cover-image-mobile'] === 'false'; |
|
| 16 | _this.cb = cb || function () { |
|
| 17 | //DEBUG console.log("Default callback"); |
|
| 18 | }; |
|
| 19 | ||
| 20 | _this.positioning = { |
|
| 21 | x: 0.5, |
|
| 22 | y: 0.5 |
|
| 23 | }; |
|
| 24 | _this.options = { |
|
| 25 | parallax: _this.$el.dataset.coverImageParallax === 'true' |
|
| 26 | }; |
|
| 27 | ||
| 28 | if (!_this.$img) { |
|
| 29 | console.log('Error:', 'no image found', _this.$img); |
|
| 30 | return; |
|
| 31 | } |
|
| 32 | ||
| 33 | _this.imageWidth = _this.$img.width; |
|
| 34 | _this.imageHeight = _this.$img.height; |
|
| 35 | ||
| 36 | // If the image doesn't have harcoded width|height |
|
| 37 | // attributes then load the image to calculate |
|
| 38 | // the dimensions |
|
| 39 | if (!_this.imageWidth || !_this.imageHeight) { |
|
| 40 | console.log('No dimensions found. Generating image:', _this.$img.attr('src')); |
|
| 41 | _this.img = new Image(); |
|
| 42 | _this.img.src = _this.$img.attr('src'); |
|
| 43 | ||
| 44 | _this.imageWidth = _this.img.width; |
|
| 45 | _this.imageHeight = _this.img.height; |
|
| 46 | } |
|
| 47 | ||
| 48 | if (_this.disableOnMobile && window.innerWidth < 480) { |
|
| 49 | return; |
|
| 50 | } |
|
| 51 | ||
| 52 | _this.elementDimensions = { |
|
| 53 | height: _this.$el.clientHeight, |
|
| 54 | width: _this.$el.clientWidth |
|
| 55 | }; |
|
| 56 | ||
| 57 | _this.$el.style = '\n\t\t\toverflow : hidden;\n\t\t\tposition : relative;\n\t\t'; |
|
| 58 | ||
| 59 | if (!_this.$img) { |
|
| 60 | // TODO: Implement load |
|
| 61 | setTimeout(function () { |
|
| 62 | new CoverImage(_this.$el); |
|
| 63 | }, 1000); |
|
| 64 | } else { |
|
| 65 | _this.resizeImage(); |
|
| 66 | } |
|
| 67 | ||
| 68 | _this.$img.addEventListener('load', function () { |
|
| 69 | _this.resizeImage(); |
|
| 70 | }, false); |
|
| 71 | ||
| 72 | _this.$el.addEventListener('transitionend', function () { |
|
| 73 | _this.resizeImage(); |
|
| 74 | }, false); |
|
| 75 | ||
| 76 | var erd = elementResizeDetectorMaker({ |
|
| 77 | strategy: 'scroll' |
|
| 78 | }); |
|
| 79 | ||
| 80 | erd.listenTo(_this.$el, function () { |
|
| 81 | _this.resizeImage(); |
|
| 82 | }); |
|
| 83 | ||
| 84 | _this.$el.addEventListener('animationend', function () { |
|
| 85 | _this.resizeImage(); |
|
| 86 | }, false); |
|
| 87 | ||
| 88 | window.addEventListener('resize', function () { |
|
| 89 | _this.resizeImage(); |
|
| 90 | }, true); |
|
| 91 | ||
| 92 | window.addEventListener('ci.resize', function () { |
|
| 93 | _this.resizeImage(); |
|
| 94 | }, true); |
|
| 95 | ||
| 96 | if (_this.options.parallax) { |
|
| 97 | _this.draw(); |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| 101 | /** |
|
| 102 | * Parallax FX |
|
| 103 | * |
|
| 104 | */ |
|
| 105 | ||
| 106 | ||
| 107 | _createClass(CoverImage, [{ |
|
| 108 | key: 'draw', |
|
| 109 | value: function draw() { |
|
| 110 | var _this = this; |
|
| 111 | var friction = 0.5; |
|
| 112 | var imageOffsetX = document.body.scrollTop * friction; |
|
| 113 | var imageOffsetY = document.body.scrollTop * friction; |
|
| 114 | var maximumMovementY = (_this.imageDimensions.height - _this.elementDimensions.height) * _this.positioning.y; |
|
| 115 | var maximumMovementX = (_this.imageDimensions.width - _this.elementDimensions.width) * _this.positioning.x; |
|
| 116 | ||
| 117 | if (maximumMovementX > 0) { |
|
| 118 | if (imageOffsetX < maximumMovementX) { |
|
| 119 | // console.log('New position:', maximumMovementX - imageOffsetX); |
|
| 120 | _this.$img.css({ |
|
| 121 | 'transform': 'translateX(' + (maximumMovementX - imageOffsetX) + 'px)' |
|
| 122 | }); |
|
| 123 | } |
|
| 124 | } else { |
|
| 125 | if (imageOffsetY < maximumMovementY) { |
|
| 126 | _this.$img.css('transform', 'translateY(' + (maximumMovementY - imageOffsetY) + 'px)'); |
|
| 127 | } |
|
| 128 | } |
|
| 129 | ||
| 130 | window.requestAnimationFrame(function () { |
|
| 131 | _this.draw(); |
|
| 132 | }); |
|
| 133 | } |
|
| 134 | ||
| 135 | /** |
|
| 136 | * |
|
| 137 | * @return DOM Element |
|
| 138 | */ |
|
| 139 | ||
| 140 | }, { |
|
| 141 | key: 'getElementForSizing', |
|
| 142 | value: function getElementForSizing() { |
|
| 143 | var _this = this; |
|
| 144 | var selector = _this.$el.dataset['coverImageEl']; |
|
| 145 | ||
| 146 | if (selector) { |
|
| 147 | ||
| 148 | console.log('Element selector Present', _this.$el.find(selector)); |
|
| 149 | ||
| 150 | return _this.$el.find(selector) ? _this.$el.find(selector) : _this.$el.find('img'); |
|
| 151 | } |
|
| 152 | ||
| 153 | return _this.$el.querySelector('img'); |
|
| 154 | } |
|
| 155 | }, { |
|
| 156 | key: 'resizeImage', |
|
| 157 | value: function resizeImage() { |
|
| 158 | var _this = this; |
|
| 159 | var elementWidth = _this.$el.clientWidth; |
|
| 160 | var elementHeight = _this.$el.clientHeight; |
|
| 161 | var dimensions = _this.coverDimensions(_this.imageWidth, _this.imageHeight, elementWidth, elementHeight); |
|
| 162 | ||
| 163 | _this.imageDimensions = dimensions; |
|
| 164 | ||
| 165 | if (isNaN(dimensions.width)) { |
|
| 166 | console.log('Failed to calculate image sizes.'); |
|
| 167 | } |
|
| 168 | ||
| 169 | _this.setImageSize(); |
|
| 170 | } |
|
| 171 | }, { |
|
| 172 | key: 'setImageSize', |
|
| 173 | value: function setImageSize() { |
|
| 174 | var _this = this; |
|
| 175 | ||
| 176 | _this.$img.width = _this.imageDimensions.width; |
|
| 177 | _this.$img.height = _this.imageDimensions.height; |
|
| 178 | ||
| 179 | var transform = _this.getTransform((_this.$el.clientWidth - _this.imageDimensions.width) * _this.positioning.y, (_this.$el.clientHeight - _this.imageDimensions.height) * _this.positioning.x); |
|
| 180 | ||
| 181 | _this.$img.style = '\n\t\t\tposition: absolute;\n\t\t\twidth: ' + _this.imageDimensions.width + ';\n\t\t\theight: ' + _this.imageDimensions.height + ';\n\t\t\ttransform: ' + transform + ';\n\t\t\tmax-width: none;\n\t\t'; |
|
| 182 | ||
| 183 | _this.$img.classList.add('ci--sized'); |
|
| 184 | _this.cb(); |
|
| 185 | } |
|
| 186 | }, { |
|
| 187 | key: 'getTransform', |
|
| 188 | value: function getTransform(x, y) { |
|
| 189 | return 'translate3d(' + x + 'px,' + y + 'px,0)'; |
|
| 190 | } |
|
| 191 | }, { |
|
| 192 | key: 'coverDimensions', |
|
| 193 | value: function coverDimensions(child_w, child_h, container_w, container_h) { |
|
| 194 | var scale_factor = this.max(container_w / child_w, container_h / child_h); |
|
| 195 | ||
| 196 | return { |
|
| 197 | width: Math.ceil(child_w * scale_factor), |
|
| 198 | height: Math.ceil(child_h * scale_factor) |
|
| 199 | }; |
|
| 200 | } |
|
| 201 | }, { |
|
| 202 | key: 'containDimensions', |
|
| 203 | value: function containDimensions(child_w, child_h, container_w, container_h) { |
|
| 204 | var scale_factor = this.min(container_w / child_w, container_h / child_h); |
|
| 205 | ||
| 206 | return { |
|
| 207 | width: child_w * scale_factor, |
|
| 208 | height: child_h * scale_factor |
|
| 209 | }; |
|
| 210 | } |
|
| 211 | }, { |
|
| 212 | key: 'min', |
|
| 213 | value: function min(a, b) { |
|
| 214 | return a > b ? b : a; |
|
| 215 | } |
|
| 216 | }, { |
|
| 217 | key: 'max', |
|
| 218 | value: function max(a, b) { |
|
| 219 | return a > b ? a : b; |
|
| 220 | } |
|
| 221 | }]); |
|
| 222 | ||
| 223 | return CoverImage; |
|
| 224 | }(); |
|
| 225 | ||
| 226 | var elems = document.body.querySelectorAll('[data-cover-image]'); |
|
| 227 | ||