javascript - Rotating an image based on scroll value with inline style ReactJS - Stack Overflow

I'm trying to rotate an image with position:fixed, which I'm using to mimic a background imag

I'm trying to rotate an image with position:fixed, which I'm using to mimic a background image, only thing is that I want it to rotate according in some direction depending on how much the used has scrolled down the page. So far everything seems to work, my state gets update whenever i scroll down, hence triggering a re-render of the ponent.

The inline style is the only thing that I can't seem to understand. Code below:

(Also the sizing of the image does not seem to work, since it occupies always the same amount of space (full parent div)).

<img style={{transform: `rotate(${this.state.rotate})`, transformOrigin:'right top'}} 
                className={classes.BluePowder} 
                src={bluePowder} 
                alt='BackgroundImage' />

The element this.state.rotate gets updated correctly at each scroll.

CSS:

.BluePowder {
position: fixed;
top: 0;
left:-10%;
z-index: 1;
}    

.BluePowder img {
height: 700px;
}

I'm trying to rotate an image with position:fixed, which I'm using to mimic a background image, only thing is that I want it to rotate according in some direction depending on how much the used has scrolled down the page. So far everything seems to work, my state gets update whenever i scroll down, hence triggering a re-render of the ponent.

The inline style is the only thing that I can't seem to understand. Code below:

(Also the sizing of the image does not seem to work, since it occupies always the same amount of space (full parent div)).

<img style={{transform: `rotate(${this.state.rotate})`, transformOrigin:'right top'}} 
                className={classes.BluePowder} 
                src={bluePowder} 
                alt='BackgroundImage' />

The element this.state.rotate gets updated correctly at each scroll.

CSS:

.BluePowder {
position: fixed;
top: 0;
left:-10%;
z-index: 1;
}    

.BluePowder img {
height: 700px;
}
Share Improve this question edited Aug 30, 2019 at 23:18 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Aug 29, 2019 at 21:14 Niccolò DianaNiccolò Diana 1592 silver badges13 bronze badges 1
  • Thank you! I read the docs and then modified it so many times that it has gotten lost. Working as expected now, thanks! – Niccolò Diana Commented Aug 29, 2019 at 21:21
Add a ment  | 

2 Answers 2

Reset to default 4

When applying the rotate() function the deg unit is required for the rotation angle. If you update the style as shown below this should resolve the issue:

<img style={
   {  
     transform: `rotate(${this.state.rotate}deg)`, 
     transformOrigin:'right top'}
   } 
   className={classes.BluePowder} 
   src={bluePowder} 
   alt='BackgroundImage' />

As kindly suggested by an user, and present in the docs, the rotate(ndeg) func needs to have the unit of measurament 'deg' afterwards.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647681a4638069.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信