javascript
  • jquery
  • 2014-04-24 4 views 0 likes 
    0

    나는이 같은 배너 광고를 복제하려고하지만 애니메이션 GIF 세 개를 반복해야합니다.gif를 통한 반복

    // randomization 
    var index = Math.floor(Math.random() * 3); 
    
    var images = new Array("<div class='ad'><a href = 'https://www.poundstopocket.co.uk/buildapp' target='_blank'> 
    <img src='https://www.poundstopocket.co.uk/pound-place/wp-content/themes/shaken-grid-premium/images/1.gif' alt='Visit Computer Hope'></a></div>", 
    "<div class='ad'><a href = 'https://www.poundstopocket.co.uk/straightforwardapp' target='_blank'><img src='https://www.poundstopocket.co.uk/pound-place/wp-content/themes/shaken-grid-premium/images/2.gif' alt='Computer History'></a></div>", 
    "<div class='ad'><a href = 'https://www.poundstopocket.co.uk/nohiddenfeesapp' target='_blank'><img src='https://www.poundstopocket.co.uk/pound-place/wp-content/themes/shaken-grid-premium/images/3.gif' alt='Visit Computer Hope'></a></div>") 
    // loop 
    setInterval(function() { 
        $('.ad').html(images[index]) 
        if(index == 2){ 
         index = 0; 
        }else{ 
         index++; 
        } 
    }, 5000); 
    

    의 JScript와 웹 사이트에서 이미지의 대부분을 포함 : 여기

    내가 여기 수정 된 샘플 코드입니다.

    이 루프는 내 자신의 목적을 위해 필요한 것처럼 작동하지 않습니다. 루프를 재설정하고 0,1, 2에서 다시 시작해야합니다. 무작위로 초기 스크립트를 작성 했으므로 그 부분을 계속 유지하고 싶습니다. 3 5 또는 6 초 gif.

    +0

    , 당신이 당신의 HTML의 DIV를 넣어 시도하고'페이드 아웃()'및'fadeIn()'플레이 했습니까? –

    +0

    감사합니다 이것은

    입니다. 루프가 먼저 작동하기를 원했기 때문에 페이드 인/아웃을 시도하지 않았으며 여기서는 보이지 않습니다. http://jsfiddle.net/2ZgG3/1/ – amarama

    답변

    0

    훨씬 쉬운 방법입니다. 먼저 배열의 순서를 임의로 지정하십시오.

    images.sort(function() { return Math.floor(Math.random() * 2); }); 
    

    그럼 왜 그런 배열을 사용하는

    var ad = $('.ad'), next = 0; 
    setInterval(function() { 
        ad.html(images[next]); 
        next = images[next+1] ? next+1 : 0; 
    }, 5000); 
    
    +0

    감사합니다. 여기 내 이미지 배열 스크립트에 아무것도 작성하지 않았지만 코드가 훨씬 깨끗해 보입니다. http://jsfiddle.net/2ZgG3/1/ – amarama

    +0

    죄송합니다. 지금 잘못된 순서로 배치됩니다. http://jsfiddle.net/2ZgG3/1/ – amarama

    +0

    첫 번째 gif로드가 필요하기 전에 일시 중지가 있습니다. 첫 번째 임의의 gif의 미리로드 된 수 있습니까? – amarama

    관련 문제