CSS 变换旋转 3D - 将鼠标悬停在卡片的每个角上
CSS Transform Rotate 3D - Hover on each corner of a card
我正在尝试使用 css 3d 旋转来模拟当鼠标悬停在分为 4 个象限的卡片上时按下卡片。左上、右上、左下、右下。我让顶部 left/right 工作,但无论我尝试什么组合,我都无法使底部效果与顶部效果相同。知道如何让底部 left/right 看起来正确,就像它们被向下推一样,就像顶部角落看起来正确一样吗?此外,如果在 css/js 中有更好的方法来完全做到这一点,请告诉我我只是第一次搞乱这些 css 转换。
我添加了阴影来尝试帮助 "sell" 底部被推入而顶部弹出的效果,但它看起来仍然不对。可能只是我,眼睛的把戏。
function ThzhotspotPosition(evt, el, percent) {
var left = el.offset().left;
var top = el.offset().top;
if (percent) {
x = (evt.pageX - left) / el.outerWidth() * 100;
y = (evt.pageY - top) / el.outerHeight() * 100;
} else {
x = (evt.pageX - left);
y = (evt.pageY - top);
}
return {
x: Math.round(x),
y: Math.round(y)
};
}
$(".card").mousemove(function(e) {
var hp = ThzhotspotPosition(e, $(this), true); // true = percent | false or no attr = px
if (hp.x >= 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BR");
} else if (hp.x >= 50 && hp.y < 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TR");
} else if (hp.x < 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BL");
} else {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TL");
}
$('#debug').text(hp.x + '%x' + ' ' + hp.y + '%y');
});
$(".card").hover(
function(e) {
//$( this ).addClass( "roll-left" );
},
function() {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
});
}
);
.card {
width: 100px;
height: 150px;
background: red;
position: relative;
transition: transform 1s;
transform-style: preserve-3d;
}
.card.roll-TL {
transform: rotate3d(1, -1, 0, 20deg);
-webkit-box-shadow: 5px 5px 13px 2px rgba(0,0,0,0.41);
box-shadow: 5px 5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-TR {
transform: rotate3d(-1, -1, 0, -20deg);
-webkit-box-shadow: -5px 5px 13px 2px rgba(0,0,0,0.41);
box-shadow: -5px 5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-BL {
transform: rotate3d(-1, -1, 0, 20deg);
-webkit-box-shadow: 5px -5px 13px 2px rgba(0,0,0,0.41);
box-shadow: 5px -5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-BR {
transform: rotate3d(1, -1, 0, -20deg);
-webkit-box-shadow: -5px -5px 13px 2px rgba(0,0,0,0.41);
box-shadow: -5px -5px 13px 2px rgba(0,0,0,0.41);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">Testing</div>
<div id="debug"></div>
我假设你的 js 代码是正确的(因为我在 运行 它时没有看到问题)并且只是调整了一些 css.
function ThzhotspotPosition(evt, el, percent) {
var left = el.offset().left;
var top = el.offset().top;
if (percent) {
x = (evt.pageX - left) / el.outerWidth() * 100;
y = (evt.pageY - top) / el.outerHeight() * 100;
} else {
x = (evt.pageX - left);
y = (evt.pageY - top);
}
return {
x: Math.round(x),
y: Math.round(y)
};
}
$(".card").mousemove(function(e) {
var hp = ThzhotspotPosition(e, $(this), true); // true = percent | false or no attr = px
if (hp.x >= 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BR");
} else if (hp.x >= 50 && hp.y < 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TR");
} else if (hp.x < 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BL");
} else {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TL");
}
$('#debug').text(hp.x + '%x' + ' ' + hp.y + '%y');
});
$(".card").hover(
function(e) {
//$( this ).addClass( "roll-left" );
},
function() {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
});
}
);
.card {
width: 200px;
height: 280px;
background: red;
position: relative;
transition: transform 1s;
transform-style: preserve-3d;
}
/*the backside*/
.card:after{
content:'';
position:absolute;
left:0;top:0;right:0;bottom:0;
background: gray;
transform: translateZ(-10px);
}
.card.roll-TL {
transform: rotate3d(1, -1, 0, 20deg);
}
.card.roll-TR {
transform: rotate3d(-1, -1, 0, -20deg);
}
.card.roll-BL {
transform: rotate3d(-1, -1, 0, 20deg);
}
.card.roll-BR {
transform: rotate3d(1, -1, 0, -20deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">Testing</div>
<div id="debug"></div>
我正在尝试使用 css 3d 旋转来模拟当鼠标悬停在分为 4 个象限的卡片上时按下卡片。左上、右上、左下、右下。我让顶部 left/right 工作,但无论我尝试什么组合,我都无法使底部效果与顶部效果相同。知道如何让底部 left/right 看起来正确,就像它们被向下推一样,就像顶部角落看起来正确一样吗?此外,如果在 css/js 中有更好的方法来完全做到这一点,请告诉我我只是第一次搞乱这些 css 转换。
我添加了阴影来尝试帮助 "sell" 底部被推入而顶部弹出的效果,但它看起来仍然不对。可能只是我,眼睛的把戏。
function ThzhotspotPosition(evt, el, percent) {
var left = el.offset().left;
var top = el.offset().top;
if (percent) {
x = (evt.pageX - left) / el.outerWidth() * 100;
y = (evt.pageY - top) / el.outerHeight() * 100;
} else {
x = (evt.pageX - left);
y = (evt.pageY - top);
}
return {
x: Math.round(x),
y: Math.round(y)
};
}
$(".card").mousemove(function(e) {
var hp = ThzhotspotPosition(e, $(this), true); // true = percent | false or no attr = px
if (hp.x >= 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BR");
} else if (hp.x >= 50 && hp.y < 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TR");
} else if (hp.x < 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BL");
} else {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TL");
}
$('#debug').text(hp.x + '%x' + ' ' + hp.y + '%y');
});
$(".card").hover(
function(e) {
//$( this ).addClass( "roll-left" );
},
function() {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
});
}
);
.card {
width: 100px;
height: 150px;
background: red;
position: relative;
transition: transform 1s;
transform-style: preserve-3d;
}
.card.roll-TL {
transform: rotate3d(1, -1, 0, 20deg);
-webkit-box-shadow: 5px 5px 13px 2px rgba(0,0,0,0.41);
box-shadow: 5px 5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-TR {
transform: rotate3d(-1, -1, 0, -20deg);
-webkit-box-shadow: -5px 5px 13px 2px rgba(0,0,0,0.41);
box-shadow: -5px 5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-BL {
transform: rotate3d(-1, -1, 0, 20deg);
-webkit-box-shadow: 5px -5px 13px 2px rgba(0,0,0,0.41);
box-shadow: 5px -5px 13px 2px rgba(0,0,0,0.41);
}
.card.roll-BR {
transform: rotate3d(1, -1, 0, -20deg);
-webkit-box-shadow: -5px -5px 13px 2px rgba(0,0,0,0.41);
box-shadow: -5px -5px 13px 2px rgba(0,0,0,0.41);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">Testing</div>
<div id="debug"></div>
我假设你的 js 代码是正确的(因为我在 运行 它时没有看到问题)并且只是调整了一些 css.
function ThzhotspotPosition(evt, el, percent) {
var left = el.offset().left;
var top = el.offset().top;
if (percent) {
x = (evt.pageX - left) / el.outerWidth() * 100;
y = (evt.pageY - top) / el.outerHeight() * 100;
} else {
x = (evt.pageX - left);
y = (evt.pageY - top);
}
return {
x: Math.round(x),
y: Math.round(y)
};
}
$(".card").mousemove(function(e) {
var hp = ThzhotspotPosition(e, $(this), true); // true = percent | false or no attr = px
if (hp.x >= 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BR");
} else if (hp.x >= 50 && hp.y < 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TR");
} else if (hp.x < 50 && hp.y >= 50) {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-BL");
} else {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
}).addClass("roll-TL");
}
$('#debug').text(hp.x + '%x' + ' ' + hp.y + '%y');
});
$(".card").hover(
function(e) {
//$( this ).addClass( "roll-left" );
},
function() {
$(this).removeClass(function(index, className) {
return (className.match(/(^|\s)roll-\S+/g) || []).join(' ');
});
}
);
.card {
width: 200px;
height: 280px;
background: red;
position: relative;
transition: transform 1s;
transform-style: preserve-3d;
}
/*the backside*/
.card:after{
content:'';
position:absolute;
left:0;top:0;right:0;bottom:0;
background: gray;
transform: translateZ(-10px);
}
.card.roll-TL {
transform: rotate3d(1, -1, 0, 20deg);
}
.card.roll-TR {
transform: rotate3d(-1, -1, 0, -20deg);
}
.card.roll-BL {
transform: rotate3d(-1, -1, 0, 20deg);
}
.card.roll-BR {
transform: rotate3d(1, -1, 0, -20deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card">Testing</div>
<div id="debug"></div>