让交互更加生动!有意思的鼠标跟随 3D 旋转动效
作者:chokcoco
来源:SegmentFault 思否社区
今天,群友问了这样一个问题,如下所示的鼠标跟随交互效果,如何实现:
简单分析一下,这个交互效果主要有两个核心:
借助了 CSS 3D 的能力
元素的旋转需要和鼠标的移动相结合
纯 CSS 实现元素的 3D 旋转
{
transform: rotate(90deg);
}
<div class="reverseRotate">
<div class="rotate">
<div class="content">正负旋转相消3D动画</div>
</div>
</div>

.rotate {
animation: rotate 5s linear infinite;
}
.reverseRotate {
animation: reverseRotate 5s linear infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
@keyframes reverseRotate {
100% {
transform: rotate(-360deg);
}
}


div {
transform-style: preserve-3d;
perspective: 100px;
}
@keyframes rotate {
0% {
transform: rotateX(0deg) rotateZ(0deg);
}
50% {
transform: rotateX(40deg) rotateZ(180deg);
}
100% {
transform: rotateX(0deg) rotateZ(360deg);
}
}

借助 Javascript 实现鼠标跟随 3D 旋转动效

鼠标活动区域
旋转物体本身
<body>
<div id="element"></div>
</body>

body {
width: 100vw;
height: 100vh;
transform-style: preserve-3d;
perspective: 500px;
}
div {
width: 200px;
height: 200px;
background: #000;
transform-style: preserve-3d;
}

div {
transform: rotateX(15deg) rotateY(30deg);
}

控制 X 方向的移动

当鼠标在中心右侧连续移动,元素绕 Y 轴移动,并且值从 0 开始,越来越大,范围为(0, +∞)deg
反之,当鼠标在中心左侧连续移动,元素绕 Y 轴移动,并且值从 0 开始,越来越小,范围为(-∞, 0)deg
const mouseOverContainer = document.getElementsByTagName("body")[0];
const element = document.getElementById("element");
mouseOverContainer.onmousemove = function(e) {
let box = element.getBoundingClientRect();
let calcY = e.clientX - box.x - (box.width / 2);
element.style.transform = "rotateY(" + calcY + "deg) ";
}

const multiple = 20;
const mouseOverContainer = document.getElementsByTagName("body")[0];
const element = document.getElementById("element");
mouseOverContainer.onmousemove = function(e) {
let box = element.getBoundingClientRect();
let calcY = (e.clientX - box.x - (box.width / 2)) / multiple;
element.style.transform = "rotateY(" + calcY + "deg) ";
}

控制 Y 方向的移动
const multiple = 20;
const mouseOverContainer = document.getElementsByTagName("body")[0];
const element = document.getElementById("element");
mouseOverContainer.onmousemove = function(e) {
let box = element.getBoundingClientRect();
let calcX = (e.clientY - box.y - (box.height / 2)) / multiple;
element.style.transform = "rotateX(" + calcX + "deg) ";
};

let calcX = (e.clientY - box.y - (box.height / 2)) / multiple * -1;
结合 X、Y 方向的移动
const multiple = 20;
const mouseOverContainer = document.getElementsByTagName("body")[0];
const element = document.getElementById("element");
function transformElement(x, y) {
let box = element.getBoundingClientRect();
let calcX = -(y - box.y - (box.height / 2)) / multiple;
let calcY = (x - box.x - (box.width / 2)) / multiple;
element.style.transform = "rotateX("+ calcX +"deg) "
+ "rotateY("+ calcY +"deg)";
}
mouseOverContainer.addEventListener('mousemove', (e) => {
window.requestAnimationFrame(function(){
transformElement(e.clientX, e.clientY);
});
});

设置平滑出入
div {
// 与上述保持一致...
transition: all .1s;
}
mouseOverContainer.addEventListener('mouseleave', (e) => {
window.requestAnimationFrame(function(){
element.style.transform = "rotateX(0) rotateY(0)";
});
});



关注公众号:拾黑(shiheibook)了解更多
赞助链接:
关注数据与安全,洞悉企业级服务市场:https://www.ijiandao.com/
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

随时掌握互联网精彩
- 美元兑人民币汇率2023年9月21日
- nginxconfig自动生成nginx工具
- Azure OpenAI又放大招?学习不断,能量加码!
- 欧元人民币汇率2023年7月24日
- 泰铢兑换人民币2023年6月5日
- “我用 ChatGPT 造了一个零日漏洞,成功逃脱了 69 家安全机构的检测!”
- 同心赋能 铸就未来 | 统信软件《非关系型数据库典型应用》培训报名中
- 习近平回信勉励广大航天青年 弘扬“两弹一星”精神载人航天精神 为航天科技实现高水平自立自强再立新功
- 征集一下,你会用手机上的量角器干什么用?
- AWE 2021在上海开幕;神州优车终止在新三板挂牌|Do早报
- 【杂谈快报】鹏博士再次推进数据中心资产转让
- 你对国产操作系统UOS有何期望?
赞助链接