Googleワールドアースデーを模したDoodle

4284 ワード

直接コードをつけましょう.
 
CSS部分:
 
    #doodle {
      position: relative;
      margin-top: 35px;
      height: 182px;
      width: 468px; 
      overflow: hidden;
    }
    #doodle img{
      position:absolute;
      border:none;
      height:364px;
      left:0;
      top:0;
      width:468px
    }

 
HTMLセクション:
 
    <div id="doodle" title="     ">
      <img src="image/a.jpg" id="_a">
      <img src="image/b.jpg" id="_b">
      <img src="image/b.jpg" id="_c">
    </div>

 
JavaScriptセクション:
 
    (function() {
        var divMain, imgA, imgB, imgC,
            timer={}, topMargin,  flag,
            
        //     
        setOpacityFn = function(dom, val) {
          if (dom) {
            dom.style.display = val > 0 ? 'block' : 'none';
            dom.style.filter = val < 100 ? 'alpha(opacity:' + val + ')' : ''; //IE
            val = (val < 100 ? val : 100) / 100;
            dom.style.opacity = val;  //W3C
            dom.style.MozOpacity = val; //Fx
          }
        },
        
        //     
        clearTimerFn = function() {
          window.clearTimeout(timer.show);
          window.clearTimeout(timer.grow);
        },
        
        //    
        mainFn = function() {
          clearTimerFn();
          //     ,  dom   
          divMain = document.getElementById('doodle');
          imgA = document.getElementById('_a');
          imgB = document.getElementById('_b');
          imgC = document.getElementById('_c');
          topMargin = 0;
          flag = !1;
          if (divMain && imgA && imgB && imgC) { //   img
            var img = document.createElement('img');
            img.style.visibility = 'hidden';
            img.onload = imgOnloadFn;
            img.src = 'image/b.jpg';
            divMain.appendChild(img);  //  onload  
          }
        }, 
        imgOnloadFn = function() {
          clearTimerFn();
          imgB.style.height = imgC.style.height = '2912px';
          imgB.src = imgC.src = 'image/b.jpg';
          setOpacityFn(imgB, 0);
          setOpacityFn(imgC, 0);
          imgB.style.display = imgC.style.display = '';
          flowerGrowFn();
        },
        
        //                
        flowerShowFn = function(dom, val) {
          if (dom) {
            setOpacityFn(dom, val);
            if (val < 99)
              // 50ms     11,         
              timer.show = window.setTimeout(function() {
                flowerShowFn(dom, val + 11);
              }, 50);
          }
        }, 
        //      
        flowerGrowFn = function() {
          var a = flag ? imgB : imgC, 
              b = flag ? imgC : imgB;
          a.style.zIndex = 4;
          b.style.zIndex = 3;
          window.clearTimeout(timer.show);
          flowerShowFn(a, 0);
          a.style.top = -topMargin + 'px';
          topMargin = Math.floor(topMargin + 182);
          //       
          timer.gow = topMargin < 2912 
                   ? window.setTimeout(flowerGrowFn, 500) 
                   : window.setTimeout(flowerFullbloomFn, 500);
          flag = !flag;
        }, 
        
        //    ,  ~
        flowerFullbloomFn = function() {
          window.clearTimeout(timer.show);
          imgA.style.top = '-182px';
          imgA.style.zIndex = 5;
          flowerShowFn(imgA, 0);
        };
        
        mainFn();
    })();

今回は1時間半かかりました.