1 回答
TA贡献1876条经验 获得超7个赞
我假设你的 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>
添加回答
举报