简体   繁体   English

-webkit-transform在JavaScript旋转动画中不起作用(无JQuery)

[英]-webkit-transform not working in JavaScript rotation animation(no JQuery)

I made a animation function for a simple game I am making, the animation rotates some text 360degrees 4 times, to give it the appearance that the text is spinning. 我为正在制作的简单游戏制作了动画功能,动画将某些文本旋转360度4次,使文本看起来像在旋转。 I looked for similar issues to the one I'm experiencing, but the suggestions did not solve my problem. 我在寻找与我遇到的问题类似的问题,但是这些建议并不能解决我的问题。 The animation method is part of my view-object: 动画方法是我的视图对象的一部分:

rotateText  :   function(deg, message) {
                    this.gameState.innerHTML = message;

                    var animationStepsLeft = this.STEPS;
                    requestAnimationFrame(setStyle);

                    function setStyle() {
                        if(animationStepsLeft > 0) {
                            Perfection.view.gameState.style = 
                            "-ms-transform: rotate(" + deg + "deg);" +
                            "-webkit-transform: rotate(" + deg + "deg);" + 
                            "-moz-tranfsform: rotate(" + deg + "deg);" +
                            "transform: rotate(" + deg + "deg);";
                            deg += 28.8;
                            console.log("Rotated");
                            animationStepsLeft--;
                            requestAnimationFrame(setStyle);
                        };
                    };
                 },

The "gameState" is a paragraph element in HTML, which is a property of the view object. “ gameState”是HTML中的一个段落元素,它是视图对象的属性。 The method is supposed to take a starting point for the degrees (0) and the message you want to spin - is passed as a string. 该方法应该以度数(0)为起点,并且要旋转的消息以字符串形式传递。 The amount of steps it's executing is 50, and each time the animation is called the text is rotated 28.8 degrees. 它正在执行的步骤数为50,并且每次调用动画时,文本都会旋转28.8度。 My issue is that the animation works flawlessly in Firefox but does not seem to work in webkit based browsers (It's not working in Chrome or Safari). 我的问题是该动画可以在Firefox中完美运行,但似乎不能在基于Webkit的浏览器中运行(它不能在Chrome或Safari中运行)。 I should mention that the method is being called, as I've attached a console.log in the attempt to debug, and it's being logged 50 times, and the text appears on the screen, but does not rotate. 我应该提到该方法正在被调用,因为我在尝试调试时附加了console.log,并且已记录了50次,并且文本显示在屏幕上,但不会旋转。 I read in a similar post that in webkit browsers the elements need to be displayed as an inline-block, so I had my CSS styles set as: 我在类似的帖子中读到,在webkit浏览器中,元素需要显示为嵌入式块,因此我将CSS样式设置为:

#game-state {
display: inline-block;
position: absolute;
margin-left: auto; 
margin-right: auto;
margin-top: 200px;
z-index: 2;
font-family: "Tahoma", Geneva, sans-serif;
text-shadow: -3px 0 black, 0 3px black, 3px 0 black, 0 -3px black;
font-size: 100px;
color: rgb(213,40,8);   

} }

But it is still not working! 但是它仍然无法正常工作! Can anyone help me pls! 谁能帮我!

I suggest that you should open console and see what inside element.style. 我建议您打开控制台并查看element.style内的内容。 Style is a set of properties. 样式是一组属性。 EG: style.height, style.visible... not style="" . EG: style.height, style.visible...不是style=""

You should change your code to <element>.style.transform='rotate(' + deg + 'deg)' . 您应该将代码更改为<element>.style.transform='rotate(' + deg + 'deg)' For -ms-transform you can try to use .style['-ms-transform']. 对于-ms-transform您可以尝试使用.style [' -ms-transform transform']。

Using raw javascript then you must detect your browser for supported style. 使用原始javascript,然后必须检测浏览器是否支持样式。

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

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