css3實現的皮球上下彈動效果程式碼例項

admin發表於2017-02-23
本章節分享一段程式碼例項,它利用css3實現了皮球上下彈動效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
h1 {
  text-align: center;
  font-size: 30px;
  margin-top: 30px;
  margin-bottom: 30px;
  color: #999;
  text-shadow: 0px 1px 3px #000;
}
.box {
  width: 400px;
  height: 300px;
  margin: 30px auto;
  border: 1px solid #ccc;
  position: relative;
}
.box .circle {
  position: absolute;
  left: 50%;
  top: 0px;
  margin-left: -70px;
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: -webkit-linear-gradient(top,#fff,#999);
  background: -moz-linear-gradient(top,#fff,#999);
  background: linear-gradient(to top,#fff,#999);
  box-shadow: inset 0 0 30px #999,inset 0 -15px 70px #999;
  animation: jump 1s ease-in-out infinite;
}
.circle:after {
  position: absolute;
  top: 10px;
  left: 50%;
  margin-left: -35px;
  content: "";
  width: 70px;
  height: 30px;
  border-radius: 50%;
  background: -webkit-linear-gradient(top,#fff,#999);
  background: -moz-linear-gradient(top,#fff,#999);
  background: linear-gradient(to top,#fff,#999);
}
.shade {
  position: absolute;
  left: 50%;
  bottom: 0px;
  width: 90px;
  height: 60px;
  margin-left: -40px;
  border-radius: 50%;
  box-shadow: 0 0 25px 20px rgba(20,20,20,0.1);
  transform: scaleY(.3);
  animation: shadow 1s infinite;
}
@keyframes jump {
  0% {
    top: 0px;
    width: 140px;
    height: 140px;
    animation-timing-function: ease-in;
  }
  65% {
    top: 160px;
    height: 140px;
  }
  75% {
    height: 120px;
  }
  100% {
    top: 0px;
    height: 140px;
  }
}
@keyframes shadow {
  0% {
    animation-timing-function: ease-in;
    width: 90px;
    height: 60px;
    box-shadow: inset 10px 10px 10px rgba(20,20,20,0.1);
  }
  65% {
    width: 10px;
    height: 5px;
    margin-left: -5px;
    background: rgba(20,20,20,.3);
    box-shadow: 0 0 25px 20px rgba(20,20,20,0.3);
  }
  100% {
    width: 90px;
    height: 60px;
    background: rgba(20,20,20,.1);
    box-shadow: 0 0 25px 20px rgba(20,20,20,0.1);
  }
}
</style>
</head>
<body>
  <div class="box">
  <div class="circle"></div>
  <div class="shade"></div>
</div>
</body>
</html>

上面的程式碼實現了我們想要的效果,更多內容可以參參閱相關閱讀。

相關閱讀:

(1).linear-gradient可以參閱css3 linear-gradient一章節。

(2).box-shadow可以參閱CSS3 box-shadow一章節。

(3).animation可以參閱CSS3 animation一章節。

(4).keyframes可以參閱CSS3 @keyframes一章節。

相關文章