gpt4 book ai didi

javascript - 3d 透视效果只能在 chrome 中正常工作?

转载 作者:行者123 更新时间:2023-11-30 06:22:37 25 4
gpt4 key购买 nike

您好,我在其他浏览器中实现相同的 3d 透视效果时遇到了一些困难。在 Chrome 中一切正常并正确显示。在这里您将看到鼠标移动的真实 3D 透视效果。但是在 Firefox 中,一切看起来都是平淡的,并不是正确的效果。

是否有技巧可以让它在所有现代浏览器上正常工作?

标记 Jquery 也是因为对 Jquery 解决方案开放。

它在 chrome 中的样子:

enter image description here


在 Firefox 中的样子:

enter image description here

如何在所有现代浏览器上实现相同的效果?

!(function ($doc, $win) {
var screenWidth = $win.screen.width / 2,
screenHeight = $win.screen.height / 2,
$elems = $doc.getElementsByClassName("elem"),
validPropertyPrefix = '',
otherProperty = 'perspective(1000px)',
elemStyle = $elems[0].style;

if(typeof elemStyle.webkitTransform == 'string') {
validPropertyPrefix = 'webkitTransform';
} else if (typeof elemStyle.MozTransform == 'string') {
validPropertyPrefix = 'MozTransform';
}

$doc.addEventListener('mousemove', function (e) {
var centroX = e.clientX - screenWidth,
centroY = screenHeight - (e.clientY + 13),
degX = centroX * 0.02,
degY = centroY * 0.01,
$elem

for (var i = 0; i < $elems.length; i++) {
$elem = $elems[i];
$elem.style[validPropertyPrefix] = otherProperty + 'rotateY('+ degX +'deg) rotateX('+ degY +'deg)';
};
});
})(document, window);
html, body{
width:100%;
height:100%;
}


body {
background: #004382;
overflow: hidden;
}
.desk-scene{
height: 100%;
background-size: contain;
max-width: 982px;
display:flex;
flex-wrap:nowrap;
margin: 0 auto;
}

.threed {
height: 100%;
background-size: contain;
max-width: 982px;
margin: 0 auto;
transform-style: preserve-3d;
}
.desk-scene-wrapper{
height: 100%;
background-size: contain;
width: 100%; display:block;
justify-content: center;
margin: 0 auto;
}
.desk-scene-wrapper1{
width: 100%;
display:flex;
justify-content:center;
z-index: 9999;

}
.desk1 {
margin: 0 auto;
max-width:982px;
width:100%;
height:100%;
background: url('http://portalpacific.net/img/desk/ian-xing.png') no-repeat center center;
background-size: contain;
position:absolute;
-webkit-transform: translateZ(180px) scale(1);
z-index: 1;
}
.wrapper {
transform-style: preserve-3d;
margin: 0 auto;
max-width:982px;
width:100%;
height:100%;
background: url('http://portalpacific.net/img/desk/cloud.png') no-repeat center center;
background-size: contain;
}

.bloc {
background-size: contain;
background-position: center;
background: url('http://portalpacific.net/img/desk/icon-circles.png') no-repeat;
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:relative;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;

}

.content {
-webkit-transform: translateZ(80px) scale(1);
height: 100%;
width: 100%;
max-width: 720px;
position: absolute;
margin:auto;
color: #fff;
z-index: 3;
}

.content1 {
transform: translateZ(80px) scale(1);
background: url('http://portalpacific.net/img/desk/Website.png') no-repeat;
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:relative;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;
}

.content2 {
background: url('http://portalpacific.net/img/desk/webicons.png') no-repeat;
transform: translateZ(30px) scale(1);
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:absolute;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;
}
<div class="threed">
<div class="desk-scene-wrapper1">
<div class="desk1"> </div>
</div>

<div class="desk-scene-wrapper">

<div class="desk-scene">

<div class="wrapper elem" style="transform: perspective(700px) rotateY(0deg) rotateX(0deg);">

<div class="bloc">

<div class="content content1">
</div>

<div class="content content2">
</div>

</div>

</div>
</div>
</div>

最佳答案

鉴于其他浏览器似乎以不同方式处理 3D 转换,可以移动中心来伪造效果:

!(function($doc, $win) {
var screenWidth = $win.innerWidth / 2,
screenHeight = $win.innerHeight / 2,
$elems = $doc.getElementsByClassName("elem"),
otherProperty = "perspective(500px)",
elemStyle = $elems[0].style;

$doc.addEventListener("mousemove", function(e) {
var centroX = e.clientX - screenWidth,
centroY = screenHeight - (e.clientY + 13),
degX = centroX * 0.02,
degY = centroY * 0.01,
$elem;

for (var i = 0; i < $elems.length; i++) {
$elem = $elems[i];
$elem.style.transform =
otherProperty + "rotateY(" + degX + "deg) rotateX(" + degY + "deg)";
}
$doc.getElementsByClassName("content1")[0].style.left = (centroX / 25) + "px"
$doc.getElementsByClassName("content1")[0].style.top = -(centroY / 25) + "px"
$doc.getElementsByClassName("content2")[0].style.left = (centroX / 50) + "px"
$doc.getElementsByClassName("content2")[0].style.top = -(centroY / 50) + "px"
});
})(document, window);
html, body{
width:100%;
height:100%;
}


body {
background: #004382;
overflow: hidden;
}
.desk-scene{
height: 100%;
background-size: contain;
max-width: 982px;
display:flex;
flex-wrap:nowrap;
margin: 0 auto;
}

.threed {
height: 100%;
background-size: contain;
max-width: 982px;
margin: 0 auto;
transform-style: preserve-3d;
}
.desk-scene-wrapper{
height: 100%;
background-size: contain;
width: 100%; display:block;
justify-content: center;
margin: 0 auto;
}
.desk-scene-wrapper1{
width: 100%;
display:flex;
justify-content:center;
z-index: 9999;

}
.desk1 {
margin: 0 auto;
max-width:982px;
width:100%;
height:100%;
background: url('http://portalpacific.net/img/desk/ian-xing.png') no-repeat center center;
background-size: contain;
position:absolute;
transform: translateZ(180px) scale(1);
z-index: 1;
}
.wrapper {
transform-style: preserve-3d;
margin: 0 auto;
max-width:982px;
width:100%;
height:100%;
background: url('http://portalpacific.net/img/desk/cloud.png') no-repeat center center;
background-size: contain;
}

.bloc {
background-size: contain;
background-position: center;
background: url('http://portalpacific.net/img/desk/icon-circles.png') no-repeat;
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:relative;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;

}

.content {
height: 100%;
width: 100%;
max-width: 720px;
position: absolute;
margin:auto;
color: #fff;
z-index: 3;
}

.content1 {
background: url('http://portalpacific.net/img/desk/Website.png') no-repeat;
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:relative;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;
}

.content2 {
background: url('http://portalpacific.net/img/desk/webicons.png') no-repeat;
background-position: center;
max-width:982px;
width:100%;
height:100%;
position:absolute;
display:flex;
justify-content: center;
align-self:center;
background-size: contain;
}
<div class="threed">
<div class="desk-scene-wrapper1">
<div class="desk1"> </div>
</div>

<div class="desk-scene-wrapper">

<div class="desk-scene">

<div class="wrapper elem" style="transform: perspective(2000px) rotateY(0deg) rotateX(0deg);">

<div class="bloc">

<div class="content content2"></div>
<div class="content content1"></div>

</div>

</div>
</div>
</div>

关键部分是:

$doc.getElementsByClassName("content1")[0].style.left = (centroX / 25) + "px"    
$doc.getElementsByClassName("content1")[0].style.top = -(centroY / 25) + "px"
$doc.getElementsByClassName("content2")[0].style.left = (centroX / 50) + "px"
$doc.getElementsByClassName("content2")[0].style.top = -(centroY / 50) + "px"

关于javascript - 3d 透视效果只能在 chrome 中正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com