You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Simon,
great idea to do this shim, although I have some performance doubts,...
If you don't want to take care about the following points, just close this issue ;o)
So, you make a jQuery plugin that iterates through a lot of elements...
Every time you make a $('.selector').objectFit('contain'); you actually call doObjectFit that iterates through all elements found by the selector. Good so far, but...
with every call you check if object-fit is supported. This is not really needed. If you check it once, it's enough.
vartestForObjectFit=(function(){
...
})();
You should just wrap your function in a self invoking wrapper and it gets called the first time it appears and that's it. Inside your doObjectFit function you don't need var supportsObjectFit = testForObjectFit(); any more but only use testForObjectFit as a variable or you can say var supportsObjectFit = testForObjectFit;
Then you have a return this.each(function() {... inside doObjectFit that then calls doResize(this, type); but you define 2 functions inside this function wich is not a good idea inside a for loop (which each actually is). So you should define function findParentRatio outside doResize, ...
...then you create an image every time the function gets called, with a separate onload function. An images load callback gets called every time you do a image.attr("src", $this.attr("src"));, so you can also keep this whole thing outside doResize wich makes the whole function way more performant and use less memory. So, define the image outside and the onload function also. If you see a problem doing this because of the variables type, hideOverflow, $this, ratio and parent then just define them outside the scope of doResize.
The last concern would be $(window).resize(function() {... wich doesn't make sense to check if objectFit is supported, so you better only install this resize callback if testForObjectFit is false.
I would probably also not use the for(var i=0, len = toResize.length; i<len; i++) {... loop to set a timeout but to call doResize so
...not to mention that your for loop would overwrite resizeTimer for every iteration and your first calls never get cleared... and your var a = toResize[i]; also gets overwritten, so you always only call doResize for the last element in the loop.
Good luck with you plugin and keep the spirit going.
Cheers
The text was updated successfully, but these errors were encountered:
Hi Simon,
great idea to do this shim, although I have some performance doubts,...
If you don't want to take care about the following points, just close this issue ;o)
So, you make a jQuery plugin that iterates through a lot of elements...
Every time you make a
$('.selector').objectFit('contain');
you actually calldoObjectFit
that iterates through all elements found by the selector. Good so far, but...with every call you check if
object-fit
is supported. This is not really needed. If you check it once, it's enough.You should just wrap your function in a self invoking wrapper and it gets called the first time it appears and that's it. Inside your
doObjectFit
function you don't needvar supportsObjectFit = testForObjectFit();
any more but only usetestForObjectFit
as a variable or you can sayvar supportsObjectFit = testForObjectFit;
Then you have a
return this.each(function() {...
insidedoObjectFit
that then callsdoResize(this, type);
but you define 2 functions inside this function wich is not a good idea inside a for loop (which each actually is). So you should definefunction findParentRatio
outsidedoResize
, ......then you create an image every time the function gets called, with a separate onload function. An images load callback gets called every time you do a
image.attr("src", $this.attr("src"));
, so you can also keep this whole thing outsidedoResize
wich makes the whole function way more performant and use less memory. So, define the image outside and the onload function also. If you see a problem doing this because of the variablestype, hideOverflow, $this, ratio and parent
then just define them outside the scope ofdoResize
.The last concern would be
$(window).resize(function() {...
wich doesn't make sense to check if objectFit is supported, so you better only install this resize callback iftestForObjectFit
is false.I would probably also not use the
for(var i=0, len = toResize.length; i<len; i++) {...
loop to set a timeout but to calldoResize
so...not to mention that your for loop would overwrite
resizeTimer
for every iteration and your first calls never get cleared... and yourvar a = toResize[i];
also gets overwritten, so you always only calldoResize
for the last element in the loop.Good luck with you plugin and keep the spirit going.
Cheers
The text was updated successfully, but these errors were encountered: