内容与装载机一起旋转

时间:2018-04-06 16:50:06

标签: javascript css css3

我正在尝试在我的应用程序中创建一个加载程序组件,因为我使用了以下代码。

CSS

#app {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
}
#maincontainer {
    display: block;
    position: relative;
    left: 50%;
    top: 50%;
    width: 150px;
    height: 150px;
    margin: -75px 0 0 -75px;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #3498db;

    -webkit-animation: spin 2s linear infinite; 
    animation: spin 2s linear infinite; 
}

    #maincontainer:before {
        content: "";
        position: absolute;
        top: 5px;
        left: 5px;
        right: 5px;
        bottom: 5px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: #e74c3c;

        -webkit-animation: spin 3s linear infinite; 
        animation: spin 3s linear infinite; 
    }

    #maincontainer:after {
        content: "";
        position: absolute;
        top: 15px;
        left: 15px;
        right: 15px;
        bottom: 15px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: #f9c922;

        -webkit-animation: spin 1.5s linear infinite; 
          animation: spin 1.5s linear infinite;
    }

    @-webkit-keyframes spin {
        0%   { 
            -webkit-transform: rotate(0deg);  
            -ms-transform: rotate(0deg);  
            transform: rotate(0deg); 
        }
        100% {
            -webkit-transform: rotate(360deg);  
            -ms-transform: rotate(360deg); 
            transform: rotate(360deg);  
        }
    }
    @keyframes spin {
        0%   { 
            -webkit-transform: rotate(0deg);  
            -ms-transform: rotate(0deg);  
            transform: rotate(0deg);  
        }
        100% {
            -webkit-transform: rotate(360deg);  
            -ms-transform: rotate(360deg); 
            transform: rotate(360deg);  
        }
    }

HTML

<div id="app">
        <div id="maincontainer">

        I have some contents here 
        </div>
    </div>

Loader工作正常,但问题是内部div中的内容也随着加载器一起旋转。我希望内容在页面中保持稳定,只旋转加载器。 Jsfiddle link

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

到目前为止,最简单的解决方案是将动画放在与您不想要动画的内容不同的元素上。

(顺便说一下:不再需要包含那些特定于供应商的-webkit-ms前缀CSS元素; animationtransform已得到广泛支持多年来。)

&#13;
&#13;
#app {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
}

#spin, #maincontainer {
  display: block;
  position: relative;
  left: 50%;
  top: 50%;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
}

/* Combining the repeated declarations: */
#spin, #spin:before, #spin:after {
  content: "";
  position: absolute;
  border-radius: 50%;
  border: 3px solid transparent;
}

#spin {
  border-top-color: #3498db;
  animation: spin 2s linear infinite;
}

#spin:before {
  top: 5px;
  left: 5px;
  right: 5px;
  bottom: 5px;
  border-top-color: #e74c3c;
  animation: spin 3s linear infinite;
}

#spin:after {
  top: 15px;
  left: 15px;
  right: 15px;
  bottom: 15px;
  border-top-color: #f9c922;
  animation: spin 1.5s linear infinite;
}


@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
&#13;
<div id="app">
  <div id="spin"></div>
  <div id="maincontainer">
    I have some contents here
  </div>
</div>
&#13;
&#13;
&#13;