【问题标题】:MouseOver CSS3D effect with javascript带有 javascript 的 MouseOver CSS3D 效果
【发布时间】:2015-02-20 16:24:39
【问题描述】:

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

我能够根据每个图块的位置生成所需的 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>

我从一开始就做这一切,而没有使用任何外部插件来实现特定效果。请提出一些解决方案。

我已经存储了所有图块的所有顶点并在鼠标悬停时更新它。只要我将鼠标从一个图块悬停到另一个动画,以将顶点值从新的点重置为原始点。如何解决 mouseenter 和 mouseleave 事件的顶点更新问题。

【问题讨论】:

  • 我已编辑问题以指定问题。我正在使用上面的 js 代码来更新磁贴,但有一些问题需要帮助。
  • 您的“工作演示”根本不工作(404 Not Found)。
  • @Dramorian 如果可能的话,用你的代码创建一个fiddle
  • @Aminul 是的,当然。我会在接下来的几天内;)

标签: javascript jquery css gsap


【解决方案1】:

我解决了这个问题。问题在于未在 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
                    })

【讨论】:

    【解决方案2】:

    您是否有此功能适用于灵活数量的列。目前它似乎只适用于 4!

    【讨论】:

    猜你喜欢
    • 2016-08-04
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多