简体   繁体   English

如何使用jQuery / Javascript旋转单个SVG元素?

[英]How to rotate individual SVG elements with jQuery/Javascript?

I have a SVG with repeated rectangle shapes. 我有一个具有重复矩形形状的SVG。 On scroll, each rectangle should individually rotate. 滚动时,每个矩形应单独旋转。 The problem I'm having is that the entire SVG block rotates, and not individually. 我遇到的问题是整个SVG块旋转,而不是单独旋转。

Is it possible to target the script to treat each rectangle as an individual element to rotate? 是否可以将脚本定位为将每个矩形视为要旋转的单个元素?

Here is my: JSFiddle 这是我的: JSFiddle

Here is a rotation solution I found somewhere, but it affects the entire SVG: 这是我在某处找到的旋转解决方案,但会影响整个SVG:

$(function() {

  var sdegree = 0;

  $(window).scroll(function() {

  sdegree ++ ;
  sdegree = sdegree + 2 ;
  var srotate = "rotate(" + sdegree + "deg)";
  $("rect").css({"-moz-transform" : srotate, "-webkit-transform" : srotate});
  });

});

The rectangles are all rotating around the origin of the page (the top left). 矩形均围绕页面的原点(左上方)旋转。 If you want them to rotate about their own centres, then you will need to include a transform-origin in your CSS rule. 如果希望他们绕自己的中心旋转,则需要在CSS规则中包括一个transform-origin

 $(function() { var sdegree = 0; $(window).scroll(function() { sdegree ++ ; sdegree = sdegree + 2 ; var srotate = "rotate(" + sdegree + "deg)"; $("rect").css({ "-webkit-transform" : srotate, "transform" : srotate, "-webkit-transform-origin" : "50% 50%", "transform-origin" : "50% 50%" }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg width="400" height="1900"> <rect x="100" y="100" width="200" height="200" fill="red"/> <rect x="100" y="400" width="200" height="200" fill="orange"/> <rect x="100" y="700" width="200" height="200" fill="yellow"/> <rect x="100" y="1000" width="200" height="200" fill="green"/> <rect x="100" y="1300" width="200" height="200" fill="blue"/> <rect x="100" y="1600" width="200" height="200" fill="violet"/> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM