2014-12-22 29 views
10

我想實現這樣的mouseOver效果。MouseOver與javascript的CSS3D效果

enter image description here

我能夠生成每瓦根據自己的位置所需css3d矩陣。

我已經實現用鼠標移動遲緩這種效果,但如果我快從一個瓦移動到另一個瓦其notupdating正常。它在瓷磚之間顯示出差距。更新鼠標懸停上的所有圖塊/圖塊座標以獲得一致效果的最佳方法是什麼?

這裏是我的js代碼:在我的代碼

$('.box').each(function() { 
      $(this).css('height', '284px'); 
      $(this).css('width', '284px'); 
     }); 

     generateGrid = function(w, h) { 
      var t = this; 
      this.p = []; 
      var d = 30; 


      var c = Math.floor($('.w').outerWidth()/284 + 1); 
      var r = Math.ceil($('.w').outerHeight()/284) + 1; 
      var vc = c * r; 
      for (i = 0; i < vc; i++) { 
       var l = { 
        x: Math.floor(i % c) * 284, 
        y: Math.floor(i/c) * 284 
       }; 
       this.p.push(l); 
      } 
      var m = m || {}; 

      m.Grid = function() { 
       this.elms = []; 
       this.init(); 
      }, m.Grid.prototype = { 
       init: function() { 
        this.createTiles(); 
       }, 
       animateTile: function() { 
        var e = this; 
        for (i = 0; i < e.elms.length; i++) { 
         console.log(i); 
         e.elms[i].update(); 
        } 
        requestAnimationFrame($.proxy(this.animateTile, this)); 
       }, 
       createTiles: function() { 
        var c = this; 
        for (i = 0; i < $('.box').length; i++) { 
         c.elms.push(new m.tile($('.box:eq(' + i + ')'), i)); 
        } 

        c.animateTile(); 
       } 
      }, m.tile = function(e, i, pt) { 
       this.el = e; 
       this.i = i; 
       var p = t.p; 
       this.elX = Math.floor(i % 4) * 284, 
         this.elY = Math.floor(i/4) * 284, 
         this.p1 = p[i + Math.floor(i/4)], 
         this.p2 = p[i + Math.floor(i/4) + 1], 
         this.p3 = p[i + Math.floor(i/4) + 6] 
       this.p4 = p[i + Math.floor(i/4) + 5]; 


       this.init(); 
      }, m.tile.prototype = { 
       init: function() { 

        this.initEvents(); 
       }, 
       initEvents: function() { 
        var e = this; 
        var pts = t.p; 
        var p1 = pts[e.i + Math.floor(i/4)], 
          p2 = pts[e.i + Math.floor(i/4) + 1], 
          p3 = pts[e.i + Math.floor(i/4) + 6], 
          p4 = pts[e.i + Math.floor(i/4) + 5]; 


        $(e.el).hover(function() { 
         TweenMax.killTweensOf(p1), 
           TweenMax.killTweensOf(p2), 
           TweenMax.killTweensOf(p3), 
           TweenMax.killTweensOf(p4), 
           TweenMax.to(p1, .3, { 
          x: p1.x - d, 
          y: p1.y - d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p2, .3, { 
          x: p2.x + d, 
          y: p2.y - d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p3, .3, { 
          x: p3.x + d, 
          y: p3.y + d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p4, .3, { 
          x: p4.x - d, 
          y: p4.y + d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(e.el, .3, { 
          zIndex: 10, 
          ease: Back.easeOut 
         }); 

        }, function() { 
         TweenMax.killTweensOf(p1), 
           TweenMax.killTweensOf(p2), 
           TweenMax.killTweensOf(p3), 
           TweenMax.killTweensOf(p4); 

         TweenMax.to(p1, .7, { 
          x: p1.x + d, 
          y: p1.y + d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p2, .7, { 
          x: p2.x - d, 
          y: p2.y + d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p3, .7, { 
          x: p3.x - d, 
          y: p3.y - d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(p4, .7, { 
          x: p4.x + d, 
          y: p4.y - d, 
          ease: Back.easeOut 
         }), 
         TweenMax.to(e.el, .7, { 
          zIndex: 0, 
          ease: Back.easeOut 
         }); 
        }); 
       }, 
       update: function() { 
        var e = this; 
        var pts = t.p; 
        var p1 = pts[e.i + Math.floor(i/4)], 
          p2 = pts[e.i + Math.floor(i/4) + 1], 
          p3 = pts[e.i + Math.floor(i/4) + 6], 
          p4 = pts[e.i + Math.floor(i/4) + 5]; 
        BLEND.TransformElement(
          { 
           el: e.el[0], 
           src: [{x: 0, y: 0}, {x: w, y: 0}, {x: w, y: h}, {x: 0, y: h}], 
           dest: [ 
            {x: p1.x - e.elX, 
             y: p1.y - e.elY}, 
            {x: p2.x - e.elX, 
             y: p2.y - e.elY}, 
            {x: p3.x - e.elX, 
             y: p3.y - e.elY}, 
            {x: p4.x - e.elX, 
             y: p4.y - e.elY}, 
           ] 
          }); 
       } 
      }; 

      t.grid = new m.Grid(); 
     }; 
     generateGrid(284, 284); 

BLEND.TransformElement(EL,SRC,DEST)給出CSS3D矩陣,它的正常工作。我需要正確更新頂點。

這是我的HTML和CSS:

<style> 
     .box{ 
      float: left; 
      background: #2b5349; 
      transform-origin: 0px 0px; 
     } 
    </style> 

    <div class="w" style=" margin-bottom:190px;display:inline-block;width: calc(284px * 4); margin:100px auto;"> 
     <div class="box" style="background: red"></div> 
     <div class="box" style="background: #2b5349"></div> 
     <div class="box" style="background: green"></div> 
     <div class="box" style="background: blue"></div> 
     <div class="box" style="background: darkgoldenrod"></div> 
     <div class="box" style="background: fuchsia"></div> 
     <div class="box" style="background: lightpink"></div> 
     <div class="box" style="background: mediumspringgreen"></div> 
     <div class="box" style="background: burlywood"></div> 
     <div class="box" style="background: orange"></div> 
     <div class="box" style="background: gold"></div> 
     <div class="box" ></div> 
    </div> 

我做的這一切,從這個開始,而無需使用任何外部插件特定的效果。請提出一些解決方案。

我已經存儲了所有圖塊的所有頂點並在mouseover上更新它。只要將鼠標從一個圖塊滑動到另一個動畫,以將頂點值從新圖塊重置爲原始圖塊。我如何解決mouseenter和mouseleave envent上的頂點更新問題。

+0

我編輯了問題來指定問題。我正在使用上面的js代碼更新瓷磚,但有一些問題需要幫助。 – Dramorian

+0

您的「工作演示」根本無法工作(404 Not Found)。 – Oriol

+0

@Dramorian如果可能的話,用你的代碼 – Aminul

回答

4

我解決了這個問題。問題在於在mouseout事件中沒有將頂點值更新爲原始值。將頂點值恢復爲原始問題,我必須保持頂點的額外值。

var l = { 
       x: Math.floor(i % c) * 284, 
       y: Math.floor(i/c) * 284, 
       x2: Math.floor(i % c) * 284, 
       y2: Math.floor(i/c) * 284, 

      }; 

鼠標懸停改變這樣的頂點值的每個座標

TweenMax.to(p1, .3, { 
         x: p1.x2 + d, 
         y: p1.y2 - d, 
         ease: Back.easeOut 
        }) 

和鼠標移開時恢復原來的位置

TweenMax.to(p2, .3, { 
         x: p2.x, 
         y: p2.y, 
         ease: Back.easeOut 
        }) 
0

你有這樣的工作列的靈活的數字。目前它似乎只適用於4!

+0

代碼正在爲n個列的工作而沒有多少調整。 – Dramorian

+0

你能否提供一些需要調整的細節?謝謝! –

+0

你可以使用gitHub的插件http://rawgit.com/Manish2612/Inflate/master/inflate.html – Dramorian