【问题标题】:Prevent an element to rotate itself in a circular CSS3 animation防止元素在圆形 CSS3 动画中自行旋转
【发布时间】:2013-03-05 17:58:33
【问题描述】:

好的,所以这对我来说真的很沮丧,首先,如果我的问题框架有误,请编辑它(如果你这么认为)......好吧,我的屏幕会向你解释,但我仍然会像这样我的元素应该保持特定的形状而不是随着动画旋转,我错过了一些非常愚蠢的东西吗?

我想要什么

发生了什么

欢迎使用 jQuery 解决方案(但我更喜欢 CSS3 解决方案)

注意:不要继续元素的不透明度,1是使用Paint和 其他使用 Photoshop,What Matter's Is Square 应该旋转为正方形 形状

HTML

<div><span></span></div>

CSS

@keyframes round_round {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

div {
    width: 50px;
    height: 50px;
    animation: round_round 3s linear infinite;
    margin: 50px auto 0;
    transform-origin: 50% 150px;
    background-color: #8FC1E0;
}

span {
    display: inline-block;
    margin: 5px;
    height: 5px;
    width: 5px;
    background: #c00000;
}

Demo

【问题讨论】:

    标签: jquery html css


    【解决方案1】:

    绝对定位,不要更改transform-origin,留在50% 50%

    然后简单地旋转元素,通过半径值平移它,然后取消第一次旋转 - 你可以看到链式变换的工作原理here

    @keyframes rot {
      0% { transform: rotate(0deg) translate(150px) rotate(0deg); }
      100% { transform: rotate(360deg) translate(150px) rotate(-360deg); }
    }
    

    demo

    【讨论】:

    • 现在可以了,太棒了...顺便说一句,请确保您在答案中添加一个小提琴 (jsfiddle.net/s7Bsn/3),顺便说一句,不需要absolute
    【解决方案2】:

    我刚刚为那些不想使用 CSS 3 动画(例如出于兼容性原因)的人编写了一个纯 JavaScript 实现。

    Demo

    // requestAnim shim layer by Paul Irish
    window.requestAnimFrame = (function(){
      return  window.requestAnimationFrame       || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame    || 
              window.oRequestAnimationFrame      || 
              window.msRequestAnimationFrame     || 
              function(/* function */ callback, /* DOMElement */ element) {
                window.setTimeout(callback, 1000 / 60);
              };
    })();
    
    function CircleAnimater(elem, radius, speed) {
        this.elem = elem;
        this.radius = radius;
        this.angle = 0;
        this.origX = this.elem.offsetLeft;
        this.origY = this.elem.offsetTop;
        
        this.shouldStop = false;
        this.lastFrame = 0;
        this.speed = speed;
    }
    
    CircleAnimater.prototype.start = function () {
        this.lastFrame = +new Date;
        this.shouldStop = false;
        this.animate();
    }
        
    CircleAnimater.prototype.stop = function () {
        this.shouldStop = true;
    }
        
    CircleAnimater.prototype.animate = function () {
        var now    = +new Date,
            deltaT = now - this.lastFrame;
        
        var newY = Math.sin(this.angle) * this.radius;
        var newX = Math.cos(this.angle) * this.radius;
    
        this.elem.style.left = (this.origX + newX) + "px";
        this.elem.style.top = (this.origY + newY) + "px";
        this.angle += (this.speed * deltaT);
    
        this.lastFrame = +new Date;
    
        if (!this.shouldStop) {
            var $this = this;
            requestAnimFrame(function () {
                $this.animate();
            });
        }        
    }
    

    【讨论】:

    • 所有现代浏览器都支持 CSS3 转换(以及 IE 从 9 开始)-caniuse.com/transforms2d。对 RAF 的支持更差(尽管你提供了一个后备):caniuse.com/requestanimationframe
    • @RobW 我写这个只是因为我有点喜欢那个动画。这只是为了好玩。当然,我知道 CSS (3) 的东西总是比用 JS 实现的解决方案更优雅;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多