JS和CSS使图片旋转

谁借莪1个温暖的怀抱¢ 2022-06-05 04:25 386阅读 0赞

“秋风宝剑孤臣泪,落日旌旗大将坛”
如题,,我们要使图片可以旋转,,在图片上添加点击事件.
JSP代码:

  1. <img class="photo1" style="width: 350px;height: auto;max-height: 500px" id="facePhotoPath1" src="" onclick="picture_rotate(this)" onerror="notFindPicture()">

JS代码:

  1. /** * 图片旋转 */
  2. function picture_rotate(e){
  3. var pDiv=$("#"+e.id).attr("id");
  4. var step=90;//每次旋转多少度
  5. var angle=getTransformRotate(pDiv);
  6. console.log("pid===" + pDiv);
  7. console.log("angle===" + angle);
  8. var pDivWidth=$("#"+e.id).parent().css("width");
  9. var pDivHeight=$("#"+e.id).parent().css("height");
  10. $("#"+pDiv).css({
  11. 'transform':'rotate('+(step+angle)%360+'deg)'});
  12. }
  13. /** * 获取图片旋转角度 */
  14. function getTransformRotate(id){
  15. var el = document.getElementById(id);
  16. var st = window.getComputedStyle(el, null);
  17. var tr = st.getPropertyValue("-webkit-transform") ||
  18. st.getPropertyValue("-moz-transform") ||
  19. st.getPropertyValue("-ms-transform") ||
  20. st.getPropertyValue("-o-transform") ||
  21. st.getPropertyValue("transform") ||
  22. "FAIL";
  23. // With rotate(30deg)...
  24. // matrix(0.866025, 0.5, -0.5, 0.866025, 0px, 0px)
  25. // rotation matrix - http://en.wikipedia.org/wiki/Rotation_matrix
  26. var values = tr.split('(')[1].split(')')[0].split(',');
  27. var a = values[0];
  28. var b = values[1];
  29. var c = values[2];
  30. var d = values[3];
  31. var scale = Math.sqrt(a * a + b * b);
  32. // arc sin, convert from radians to degrees, round
  33. var sin = b / scale;
  34. // next line works for 30deg but not 130deg (returns 50);
  35. // var angle = Math.round(Math.asin(sin) * (180/Math.PI));
  36. return angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
  37. }

发表评论

表情:
评论列表 (有 0 条评论,386人围观)

还没有评论,来说两句吧...

相关阅读