Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
Update checkMediaConstraints() to check for pre-set positioning and m…
Browse files Browse the repository at this point in the history
…argin CSS, which can also throw off image calcs or end results
  • Loading branch information
cee-chen committed Apr 28, 2017
1 parent ff890b8 commit 1932a85
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/objectFitPolyfill.basic.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/objectFitPolyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/objectFitPolyfill.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@
};

/**
* Check for pre-set max-width/height or min-width/height,
* which can mess up image calculations
* Check for pre-set max-width/height, min-width/height,
* positioning, or margins, which can mess up image calculations
*
* @param {node} $media - img/video element
*/
var checkMediaConstraints = function($media) {
var checkMediaProperties = function($media) {
var styles = window.getComputedStyle($media, null);
var constraints = {
"max-width": "none",
"max-height": "none",
"min-width": "0px",
"min-height": "0px"
"min-height": "0px",
"top": "auto",
"right": "auto",
"bottom": "auto",
"left": "auto",
"margin-top": "0px",
"margin-right": "0px",
"margin-bottom": "0px",
"margin-left": "0px",
};

for (var property in constraints) {
Expand All @@ -86,8 +94,8 @@
var $container = $media.parentNode;
checkParentContainer($container);

// Check for max-width/height or min-width/height, which can mess up image calculations
checkMediaConstraints($media);
// Check for any pre-set CSS which could mess up image calculations
checkMediaProperties($media);

// Mathematically figure out which side needs covering, and add CSS positioning & centering
$media.style.position = "absolute";
Expand Down
20 changes: 14 additions & 6 deletions src/objectFitPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,26 @@
};

/**
* Check for pre-set max-width/height or min-width/height,
* which can mess up image calculations
* Check for pre-set max-width/height, min-width/height,
* positioning, or margins, which can mess up image calculations
*
* @param {node} $media - img/video element
*/
var checkMediaConstraints = function($media) {
var checkMediaProperties = function($media) {
var styles = window.getComputedStyle($media, null);
var constraints = {
"max-width": "none",
"max-height": "none",
"min-width": "0px",
"min-height": "0px"
"min-height": "0px",
"top": "auto",
"right": "auto",
"bottom": "auto",
"left": "auto",
"margin-top": "0px",
"margin-right": "0px",
"margin-bottom": "0px",
"margin-left": "0px",
};

for (var property in constraints) {
Expand Down Expand Up @@ -161,8 +169,8 @@
var $container = $media.parentNode;
checkParentContainer($container);

// Check for max-width/height or min-width/height, which can mess up image calculations
checkMediaConstraints($media);
// Check for any pre-set CSS which could mess up image calculations
checkMediaProperties($media);

// Mathematically figure out which side needs covering, and add CSS positioning & centering
$media.style.position = "absolute";
Expand Down

0 comments on commit 1932a85

Please sign in to comment.