jQuery - rotateY取决于元素向右或向左悬停

时间:2016-11-13 21:59:52

标签: javascript jquery css3 rotatetransform

这是我的代码的小提琴 - https://jsfiddle.net/agon024/o9sa7nyh/5/

:hover{
  transform: perspective(800px) rotateY(25deg);
  transition-timing-function: ease-in;
  transition: all, 0.5s;
}

我要做的是将div元素旋转25度或-25度,具体取决于您是否悬停在图像的左侧或右侧。

正如你在小提琴上看到的那样,当你将鼠标悬停在它上面时,只需旋转25度即可。我怎么能用jQuery做到这一点?我发现这个小提琴(http://jsfiddle.net/HeFqh/11/)与我正在寻找的相似(就无缝过渡而言,我需要它来旋转。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

你可以这样做:

$( ".billMurray img" ).mousemove(
  function(e) {
 var offset = $(this).offset();
  var relativeX = (e.pageX - offset.left);
if(relativeX< $(this).width()/2) {

 $( this ).addClass( "hover-left" );
  $( this ).removeClass( "hover-right" );
   $(this).removeClass('start');
}
else {
 $( this ).addClass( "hover-right" );
  $( this ).removeClass( "hover-left" );
     $(this).removeClass('start');
}



  }
);

$( ".billMurray img" ).mouseleave(function(){
 $( this ).removeClass( "hover-right" );
  $( this ).removeClass( "hover-left" );
  $(this).addClass('start');
});

所以,想法是:找到左右鼠标位置(首先获得中心):https://css-tricks.com/snippets/jquery/get-x-y-mouse-coordinates/用于图像,并添加和删除适当的类。由于鼠标离开图像时无缝转换,所以“开始”类就在那里......

DEMO,稍加修改的CSS: https://jsfiddle.net/o9sa7nyh/6/

答案 1 :(得分:0)

在每个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/