javascript - jQuery scaling effects, scale in 1.1 and back to 1 when background position changes -
i'm little stuck, tried css route new me , learning still cannot seem work out. js below switches background position show new image in sprite every 1 second wondering if knew how can kind of give small scale effects when changes grows little normal size before change next background position?
hope can me pulling hair out here haha
js:
// avatar animations var avatarinterval; function startavataranimation() { var = 0; var avatarspeed = 500; var avatarcount= 11; var avatarheight = 250; var avatartotalheight = 2750; avatarinterval = setinterval(function(){ i++; if(i > 11){ = 0; } $(".avatars").css({'background-position' : '0 -' + (i*avatarheight) + 'px' }); $(".avatars").toggleclass('scalein', 'scaleout'); }, avatarspeed); return false; } function stopavataranimation(){ clearinterval(avatarinterval); $(".avatars").css({'background-position' : '0 0' }); return false; }
js below switches background position show new image in sprite every 1 second wondering if knew how can kind of give small scale effects when changes grows little normal size before change next background position?
try utilizing transition
@ css
, setting duration half of avatarspeed
or half of total duration of background-position
effect ; setting transitionend
event @ .one()
prevent recursive call transitionend
handler , .removeclass()
, .addclass()
toggle scale effect defined @ css
css
.avatars { transition: transform 500ms ease-in-out; width: 100px; height: 100px; background-image: url(/path/to/background-image); } .avatars.scalein { transform: scale(1.1, 1.1); } .avatars.scaleout { transform: scale(1.0, 1.0); }
js
// avatar animations var avatarinterval; function startavataranimation() { var = 0; var avatarspeed = 500; var avatarcount= 11; var avatarheight = 250; var avatartotalheight = 2750; avatarinterval = setinterval(function(){ i++; if(i > 11){ = 0; } $(".avatars").css({'background-position' : '0 -' + (i*avatarheight) + 'px' }) .removeclass("scaleout").addclass("scalein") .one("transitionend", function() { $(this).removeclass("scalein").addclass("scaleout"); }); }, avatarspeed); return false; }
$(".avatars").on("click", function() { $(this).removeclass("scaleout").addclass("scalein") .one("transitionend", function() { $(this).removeclass("scalein").addclass("scaleout"); }) })
.avatars { transition: transform 500ms ease-in-out; width: 100px; height: 100px; background-image: url(http://lorempixel.com/100/100/nature); } .avatars.scalein { transform: scale(1.1, 1.1); } .avatars.scaleout { transform: scale(1.0, 1.0); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="avatars"></div>
Comments
Post a Comment