jQuery随鼠标旋转的图形效果
分类:实例代码
本章节分享一段代码实例,它实现了随鼠标旋转的图形效果。
当然效果并不仅限于标题中的介绍,比如文字还具有凸起效果,里面应用了css3属性。
代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.pipipi.net/" /> <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>
图示如下:
一线大厂高级前端编写,前端初中阶面试题,帮助初学者应聘,需要联系微信:javadudu