天天看點

純CSS實作水波紋效果

純CSS實作水波紋效果

首先我們從結構和樣式兩個方面來講解以上動圖的實作過程:

Html結構:

<div class="square">
  <span></span>
  <span></span>
  <span></span>
  <div class="content">
    <h2>Post Title</h2>
    <p>In order to better understand how to create your own 
    Vue.js plugins, we will create a very simplified version 
    of a plugin that displays i18n ready strings.</p>
    <a href="#" target="_blank" rel="external nofollow" >Read More</a>
  </div>
</div>
           

最外層類名為square元素包裹着以類名為content裡面是文字和按鈕,3個span元素組成了水波紋

CSS樣式:

以下是效果圖的基本樣式,設定body黑色背景,高度為浏覽器100%高度,body裡面的元素垂直居中

@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,600,700&display=swap');
* {
  margin: 0;
  padding: 0;
  box-shadow: border-box;
  font-family: 'Open Sans', sans-serif;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #000;
}
           

接下來設定body下最外層元素的樣式,長寬為400像素,子元素垂直水準居中

.square {
  position: relative;
  width: 400px;
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
}
           

這裡的span元素就是效果圖中的波紋,有3層,分别相對父元素絕對定位,長寬等于父元素,然後設定寬度為2個像素顔色為白色的的border,之是以為不規則的橢圓,是因為設定了四個角都不相等的角的弧度值。

.square span {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #fff;
  border-radius: 38% 62% 63% 37% / 41% 44% 56% 59%;
}
           

然後我們要讓波紋轉動起來,添加animation動畫屬性,動畫持續時間0.5s,線性循環

.square span {
  transition: 0.5s;
  animation: animate 6s linear infinite;
}
           

animate動畫均勻旋轉360度,另一個相反方向旋轉

@keyframes animate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes animate2 {
  0% {
    transform: rotate(360deg);
  }
  100% {
    transform: rotate(0deg);
  }
}
           

三個span元素分别設定不同的動畫和持續時間

.square span:nth-child(1) {
  animation: animate 6s linear infinite;
}
.square span:nth-child(2) {
  animation: animate 4s linear infinite;
}
.square span:nth-child(3) {
  animation: animate2 10s linear infinite;
}
           

設定類名content裡面文字樣式白色居中,以及設定“Read More”按鈕樣式,不規則邊框

.content {
  position: relative;
  padding: 40px 60px;
  color: #fff;
  text-align: center;
  transition: 0.5s;
  z-index: 1000;
}
.content a {
  position: relative;
  display: inline-block;
  margin-top: 10px;
  border: 2px solid #fff;
  padding: 6px 18px;
  text-decoration: none;
  color: #fff;
  font-weight: 600;
  border-radius: 73% 27% 44% 56% / 49% 44% 56% 51%;
}
.content a:hover {
  background: #fff;
  color: #333;
}