简体   繁体   English

如何根据鼠标position改变元素透视

[英]How to change elements perspective based on mouse position

I was wondering how to replicate this effect , where the element you are hovering over seems to rotate away from the mouse.我想知道如何复制这种效果,您悬停的元素似乎旋转远离鼠标。

I have worked out how to use css transforms to change an element in 3d space like this:我已经研究出如何使用 css 转换来更改 3d 空间中的元素,如下所示:

.scene {
  width: 200px;
  height: 200px;
  border: 1px solid #CCC;
  margin: 40px;
}

.card {
  width: 100%;
  height: 100%;
  background: red;
  border-radius: 3px;
}
<div class="scene">
  <div class="card" style="transform: perspective(500px) rotateY(10deg) rotatex(10deg);"></div>
</div>

But I'm not sure how to use javascript to make it update based on the mouse position.但是我不确定如何使用 javascript 使其基于鼠标 position 进行更新。

This isn't something I'm super familiar with, but looking through the page's source files, it looks like this is the relevant javascript for that functionality:这不是我非常熟悉的东西,但是查看页面的源文件,看起来这是该功能的相关 javascript:

 bpc.galleryThumbInteraction = function() { if (bpc.clickType.== 'tap') { TweenMax.set($('.project-list,project'): {rotationY, 0: rotationX, 0: rotationZ, 0: transformPerspective; 1000}). $('.project-list.project').mouseover(function() { $('.project-list.project').mousemove(function(e) { var x = e.pageX - $(this).offset(),left. y = e.pageY - $(this).offset();top. var px = x/$(this),width(). py = y/$(this);height(), var xx = -10 + (20*px); yy = 10 - (20*py). //TweenMax;killTweensOf($(this)). TweenMax,to($(this). 0,5: {rotationY, xx: rotationX, yy: rotationZ, 0: transformPerspective, 1000: ease. Quad;easeOut}); }). }).mouseout(function() { $(this);unbind('mousemove'). //TweenMax;killTweensOf($(this)). TweenMax,to($(this). 0,5: {rotationY, 0: rotationX, 0: rotationZ, 0: transformPerspective, 1000: ease. Quad;easeOut}); }); } };

Remember you can often view a pages source files by opening Chrome Dev Tools --> Source Tab --> look for the file you're looking for (in this case, pixi.js)请记住,您通常可以通过打开 Chrome 开发工具 --> 源选项卡 --> 查找您要查找的文件(在本例中为 pixi.js)来查看页面源文件

Also be aware that they are using the matrix3d() css function, which is a bit different than what you have..另请注意,他们使用的是matrix3d() css function,这与您所拥有的有点不同。

This particular site uses a library called TweenMax for this effect, here's a more generic function to mimic that effect on any element这个特定的站点使用一个名为 TweenMax 的库来实现这种效果,这里有一个更通用的 function 来模拟对任何元素的这种效果

    function hoverElement3d(className) {
    let selector = '.' + className;
    TweenMax.set($(selector), { rotationY: 0, rotationX: 0, rotationZ: 0, transformPerspective: 1000 });
    $(selector).mouseover(function () {
        $(selector).mousemove(function (e) {
            var x = e.pageX - $(this).offset().left,
                y = e.pageY - $(this).offset().top;

            var px = x / $(this).width(), py = y / $(this).height();
            var xx = -10 + (20 * px), yy = 10 - (20 * py);

            TweenMax.to($(this), 0.5, { rotationY: xx, rotationX: yy, rotationZ: 0, transformPerspective: 1000, ease: Quad.easeOut });
        });
    }).mouseout(function () {
        $(this).unbind('mousemove');
        TweenMax.to($(this), 0.5, { rotationY: 0, rotationX: 0, rotationZ: 0, transformPerspective: 1000, ease: Quad.easeOut });
    });
};

Keep in min you need both jQuery and TweenMax 1.18.0 for this exact function to work propely请记住,您需要 jQuery 和TweenMax 1.18.0才能正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM