jQuery combine a selection of pngs to one image timeline animation

爷,独闯天下 提交于 2019-12-23 04:44:05

问题


Animation is difficult with GIF images due to white borders on text and other pixelation and dithering issues. So I have decided despite the lack of support for PNG on IE especially fadeIn or fadeOut jQuery effects on PNG causing black borders to do it.

I have a folder like

/cdn.com/ui/photo/1.png
/cdn.com/ui/photo/2.png
/cdn.com/ui/photo/3.png

This is my HTML code, is there anway to add a delay or somehow loop and change the source of say 1-38 and keep looping it - maybe even customize the delay? I just want it to keep looping smoothly like a fully load gif..

<div class="m2m_badge">
    <a href="#"><img src="///cdn.com/ui/photo/1.png"/></a>
</div>

This is my jQuery

$(function()
{
    var i = 0;
    var interval = setInterval(function()
    {
        $('div.m2m_badge a img').attr({ src: '//gc-cdn.com/ui/m2m/' + i + '.png' });
        i++;
        if(i === 38)
            clearInterval(interval);
    }, 250);
});  

Demo - http://jsfiddle.net/tXvuY/13/


回答1:


$(function()
{
    var i = 1;
    var interval = setInterval(function()
    {
        $('div.m2m_badge a img').attr({ src: 'http://www.gc-cdn.com/ui/m2m/' + i + '.png' }); 
        i++;
        if(i === 38)
            clearInterval(interval); //38 images has been shown, stop the interval
    }, 50); //50ms between each swap
});    

Here's a demo

Final working

To have a continuous loop just add i=1; within the interval if

$(function()
{
    var i = 1;
    var interval = setInterval(function()
    {
        $('img').attr({ src: 'http://www.gc-cdn.com/ui/m2m/' + i + '.png' });
        i++;
        if(i === 38)
            i=1; // SOLUTION
    }, 250); 
});   



来源:https://stackoverflow.com/questions/12599648/jquery-combine-a-selection-of-pngs-to-one-image-timeline-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!