jQuery隨滑鼠旋轉的圖形效果

antzone發表於2017-04-08

本章節分享一段程式碼例項,它實現了隨滑鼠旋轉的圖形效果。

當然效果並不僅限於標題中的介紹,比如文字還具有凸起效果,裡面應用了css3屬性。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
*{
  padding: 0;
  margin: 0;
}
body{
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #f0f0f0;
}
.stage{
  position: absolute;
  width: 220px;
  height: 140px;
  top: 50%;
  left: 50%;
  margin: -70px -110px;
  perspective: 600px;
}
.card {
  position: absolute;
  width: 220px;
  height: 140px;
  border: solid 1px #f0f;
  border-radius: 6px;
  pointer-events: none;
  background-color: rgba(255, 0, 235, 0.13);
  transition: all 0.1s;
  transform-style: preserve-3d;
  -webkit-filter: dorp-shadow();
}
.card p {
  width: 100%;
  text-align: center;
  line-height: 140px;
  font-size: 40px;
  -webkit-transform: translateZ(40px);
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(function () {
  var totalDeg = 30;
  var centerPoint = {
    centerX: $('.stage').width() / 2,
    centerY: $('.stage').height() / 2
  }
  $('.stage').on('mousemove', function (e) {
    var yDeg = (centerPoint.centerX - e.offsetX) * totalDeg / 90;
    var xDeg = -(centerPoint.centerY - e.offsetY) * totalDeg / 90;
    $(this).find('.card').css({
      '-webkit-transform': ' rotateX(' + xDeg + 'deg) rotateY(' + yDeg + 'deg)'
    })
  })
  $('.stage').on('mouseleave', function (e) {
    $(this).find('.card').removeAttr('style');
  })
})
</script>
</head>
<body>
<div class="stage">
  <div class="card">
    <p>螞蟻部落</p>
  </div>
</div>
</body>
</html>

圖示如下:

a:3:{s:3:\"pic\";s:43:\"portal/201704/08/123618jkkzv3mcpec6c3mo.gif\";s:5:\"thumb\";s:0:\"\";s:6:\"remote\";N;}

相關文章