简体   繁体   中英

Animating an image sprite using jQuery Animate in steps?

I am attempting to code an image sprite using jQuery and am wondering if it's possible to animate in steps instead of linear. Basically, I attempted using CSS3 animations, but IE9 is a requirement and I could not get the CSS3 animations to function correctly in IE.

I have a sprite which has 12 frames, each frame being 280px X 280px. I would like to use jQuery to animate the sprite on mouseover and mouseout in steps instead of sliding the background image in a linear fashion.

Here is my current code:

<div class="window"></div>

CSS:

.window {
    width: 280px;
    height: 280px;
    background: url("http://s27.postimg.org/neu6mvztf/single_hung_door.png");
}

JS:

$(".window").hover(
    function () {
        $(this).animate({
            'background-position-x': '0',
            'background-position-y': '-3080px'
        }, 1500, 'linear');
    },
    function () {
        $(this).animate({
            'background-position-x': '0',
            'background-position-y': '0'
        }, 1500, 'linear');
    }
);

JSFiddle with sample of desired effect

Would it be possible to accomplish this in this way? Thanks!

您可以使用名为velocity.js的库进行此操作。您可以在此处查看操作方法: http ://css-tricks.com/clever-uses-step-easing/

It isn't the most elegant code, but basically, I think you need an animating loop running continuously that only shifts the frame animation by one frame every x milliseconds. The fiddle you had continuously animates the whole strip in a linear fashion. Forked jsFiddle link below.

var currentY = 0;
var isAnimating = false;
var $window = $('.window');
var windowWidth = 280;
var frameRateMs = 60;

function updateWindow() {
    if (isAnimating) {
        $window.css({'background-position-y': currentY + 'px'});
        currentY -= windowWidth;
        if (currentY == -3080) {
            windowWidth = -280;
        } else if (currentY == 0) {
            windowWidth = 280;
        }
    }
    setTimeout(updateWindow, frameRateMs);
}

$window.hover(function() {
    isAnimating = true;
}, function() {
    isAnimating = false;
});

updateWindow();

http://jsfiddle.net/64nu4h4w/10/

Keeping your code structure, I created a .animateSteps() function:

$(".window").hover(
    function () {
        $(".window").animateSteps(280,-280,20);
    },
    function () {
        $(".window").animateSteps(3080,280,20);
    }
);


$.fn.animateSteps = function(final,step,duration) {
    if(typeof t != "undefined") clearTimeout(t);
    var self = $(this);
    if(parseInt(self.css('background-position-y'))==final) return;
    self.css('background-position-y', '+='+step+'px');
    t=setTimeout(function(){self.animateSteps(final,step,duration);},duration);
}

It takes 3 parameters:

  • final : the final position in pixels

  • step : the number of pixels to increment at each step

  • duration : the duration between 2 iterations

JS Fiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM