使用JavaScript的MouseOver CSS3D效果

10

我正在尝试实现像这样的鼠标悬停效果。

enter image description here

我已经能够为每个瓷砖生成所需的css3d矩阵,根据它们的位置。

我已经通过缓慢的鼠标移动实现了这种效果,但是如果我快速从一个瓷砖移动到另一个瓷砖,它就无法正确更新。在瓷砖之间显示间隙。什么是更新所有瓷砖/瓷砖坐标的最佳方法,以便我获得一致的效果?

以下是我的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>

我正在从头开始完成所有这些操作,而不使用任何外部插件来实现特定的效果。请提供一些解决方案。

我已经存储了所有瓷砖的顶点,并在鼠标悬停时进行更新。当我从一个瓷砖悬停到另一个瓷砖时,将顶点值从新值重置为原始值的动画就会停止。如何解决鼠标进入和离开事件中的顶点更新问题。


我已经编辑了问题以指定问题。我正在使用上面的js代码来更新瓷砖,但是有一些问题需要帮助解决。 - Manish Mahajan
你的“工作演示”根本无法运行(404未找到)。 - Oriol
@Dramorian 如果可能的话,请使用你的代码创建一个fiddle - Aminul
@Aminul 当然可以。我会在接下来的几天内完成 ;) - Manish Mahajan
2个回答

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列!


请问您能提供需要调整的详细信息吗?谢谢! - manuel-v
你可以从GitHub上使用插件http://rawgit.com/Manish2612/Inflate/master/inflate.html - Manish Mahajan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接