在每个hover
事件计算父元素的宽度;计算元素沿着x
轴的元素的开始和存储位置;检查当前的event.clientX
是否小于当前元素加当前元素位置除以父容器内元素的.length
的位置。如果true
,鼠标指针位于元素的左侧,则设置-
符号为rotate()
函数,否则鼠标指针位于元素的右侧,设置+
符号为rotate()
函数。
var elems = $(".billMurray").hover(function(e) {
var len = elems.length;
// parent `.width()` divided by selected element `.length`
var width = elems.parent().width()/len;
// calculate beginning position of each element
var res = $.map(Array(len), function(_, i) {
return !i ? width/2 : width * i
});
// mouse position
var x = e.clientX;
// select array index using element `.index()`
var index = elems.index(this);
// check if `x` is less than start of element
// position plus element position divided by
// selected element collection `.length` along x axis
// if `true` set sign to `-`, else set sign to `+`
var sign = x < res[index] + (res[index]/len) ? "-" : "+";
$(this).css("transform", "perspective(800px) rotateY(" + sign + "25deg)")
}, function() {
$(this).css("transform", "perspective(800px) rotateY(0deg)")
})
.philWrap {
margin-top: 50px;
}
.billMurray {
width: 25%;
float: left;
text-align: center;
-webkit-transform: perspective(800px) rotateY(0deg);
-moz-transform: perspective(800px) rotateY(0deg);
-o-transform: perspective(800px) rotateY(0deg);
-ms-transform: perspective(800px) rotateY(0deg);
transform: perspective(800px) rotateY(0deg);
-webkit-transition-timing-function: ease-in;
transition-timing-function: ease-in;
-moz-transition: all, 0.5s;
-o-transition: all, 0.5s;
-webkit-transition: all, 0.5s;
transition: all, 0.5s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="philWrap">
<div class="billMurray"><img src="http://fillmurray.com/200/300" alt=""></div>
<div class="billMurray"><img src="http://fillmurray.com/g/200/300" alt=""></div>
<div class="billMurray"><img src="http://fillmurray.com/200/300" alt=""></div>
<div class="billMurray"><img src="http://fillmurray.com/g/200/300" alt=""></div>
</div>
的jsfiddle https://jsfiddle.net/o9sa7nyh/10/