飞翔梧桐 發表於 2021-8-17 17:24:51

通过CSS实现逼真水滴动效

<p style="text-align: left">哈喽哈喽!CSS真的好好玩啊,哈哈,反正我是爱了,空闲写着玩。画画不好的我乐了,下面就是一个用CSS3动画完成的模仿水珠的动效,其中主要就是会使用CSS设置阴影效果以及@keyframes关键帧和一些选择器的技术,快来学习吧!!!&#128044;<br />
</p>
<p style="text-align: center"><img alt="在这里插入图片描述" src="https://img.jbzj.com/file_images/article/202108/2021081717030190.jpg" /></p>
<p>实现效果:就很nice<br />
你也通过一下网址进行访问<a rel="external nofollow" target="_blank" href="http://zhouql.vip/1/%E6%B0%B4%E6%BB%B4.html">水滴点击进入</a></p>
<p style="text-align: center"><img alt="在这里插入图片描述" src="https://img.jbzj.com/file_images/article/202108/2021081717030191.gif" /><br />
</p>
<p style="text-align: left">灵感:看到了这张图阴影高亮,这属于美术吧,哈哈,我是小菜鸡<br />
</p>
<p style="text-align: center"><img alt="在这里插入图片描述" width="400" src="https://img.jbzj.com/file_images/article/202108/2021081717030292.jpg" /></p>
<hr />
<p>这里强烈安利GitHub上一个大牛的开源:<a rel="external nofollow" target="_blank" href="https://9elements.github.io/fancy-border-radius/">花式边框半径生成器</a>利用这个可以使这个效果实现的事半功倍,好开始coding</p>
<h2>1.html</h2>
<p>很简单,只需要一个盒子就OK了</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;水滴&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="shui"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
<h2>2.CSS</h2>
<p>注释已经写在代码中,这里主要学习一下伪元素选择器的使用,box-shadow这个设置阴影的属性,关键帧 @keyframes以及关键帧的使用 animation,和 border-radius: 30% 70% 70% 30% / 30% 35% 65% 70%;这个属性的使用</p>
<div class="jb51code">
<pre class="brush:css;">
                /*清除body的影响*/
      *{
            margin: 0;
            padding: 0;
      }
      /*设置背景颜色*/
      body{
            background-color: rgba(40, 134, 241, 0.925);
      }
      /* 初始一下水,大小,弯曲,阴影*/
      .shui{
            width: 400px;
            height: 400px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            /* 测试用的边框 */
            /* border: 1px solid; */
            box-sizing: border-box;
            /* 设置弯曲 */
            border-radius: 30% 70% 70% 30% / 30% 35% 65% 70%;
            /* 设置box-shadow :水平方向阴影垂直方向阴影模糊距离阴影尺寸阴影颜色内/外阴影(inset/outset(默认))
            盒子阴影可以有多组值,之间用逗号隔开
            水平阴影和垂直阴影必须写,其余4个是可选的*/
            box-shadow: inset 10px 20px 30px rgba(0, 0, 0, 0.5), 10px 10px 20px rgba(0, 0, 0, 0.3), 15px 15px 30px rgba(0, 0, 0, 0.05),
            inset -10px -10px 15px rgba(255, 255, 254, 0.83);
            /*使用关键帧watermove9s播放匀速 无限循环*/
            animation: watermove 9s linear infinite;
      }
      /* 伪元素选择器:在^之后插入 */
      .shui::after{
            content: "";
            position: absolute;
            width: 35px;
            height: 35px;
            background: rgba(255, 255, 255, 0.82);
            border-radius: 50%;
            left: 60px;
            top: 80px;
            /*使用关键帧watermove4s播放匀速 无限循环*/
            animation: watermove 4s linear infinite;
      }
      /* 伪元素选择器:在当前盒子最前插入一个东西 */
      .shui::before{
            content: "";
            position: absolute;
            width: 20px;
            height: 20px;
            background: rgba(255, 255, 255, 0.82);
            border-radius: 50%;
            left: 120px;
            top: 55px;
            /*使用关键帧watermove4s播放匀速 无限循环*/
            animation: watermove 4s linear infinite;
      }
      /* 关键帧 */
      @keyframes watermove{
            20%{
                border-radius: 30% 70% 53% 47% / 28% 44% 56% 72%;
            }
         
            40%{
                border-radius: 30% 70% 39% 61% / 34% 39% 61% 66%;
            }
         
            60%{
                border-radius: 25% 75% 45% 55% / 40% 55% 45% 60%;
            }
         
            80%{
                border-radius: 28% 72% 31% 69% / 32% 39% 61% 68%;
            }
      }
</pre>
</div>
<h2>3.完整代码</h2>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;水滴&lt;/title&gt;
    &lt;style&gt;
      /*清除body的影响*/
      *{
            margin: 0;
            padding: 0;
      }
      /*设置背景颜色*/
      body{
            background-color: rgba(40, 134, 241, 0.925);
      }
      /* 初始一下水,大小,弯曲,阴影*/
      .shui{
            width: 400px;
            height: 400px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            /* 测试用的边框 */
            /* border: 1px solid; */
            box-sizing: border-box;
            /* 设置弯曲 */
            border-radius: 30% 70% 70% 30% / 30% 35% 65% 70%;
            /* 设置box-shadow :水平方向阴影垂直方向阴影模糊距离阴影尺寸阴影颜色内/外阴影(inset/outset(默认))
            盒子阴影可以有多组值,之间用逗号隔开
            水平阴影和垂直阴影必须写,其余4个是可选的*/
            box-shadow: inset 10px 20px 30px rgba(0, 0, 0, 0.5), 10px 10px 20px rgba(0, 0, 0, 0.3), 15px 15px 30px rgba(0, 0, 0, 0.05),
            inset -10px -10px 15px rgba(255, 255, 254, 0.83);
            /*使用关键帧watermove9s播放匀速 无限循环*/
            animation: watermove 9s linear infinite;
      }
      /* 伪元素选择器:在^之后插入 */
      .shui::after{
            content: "";
            position: absolute;
            width: 35px;
            height: 35px;
            background: rgba(255, 255, 255, 0.82);
            border-radius: 50%;
            left: 60px;
            top: 80px;
            /*使用关键帧watermove4s播放匀速 无限循环*/
            animation: watermove 4s linear infinite;
      }
      /* 伪元素选择器:在当前盒子最前插入一个东西 */
      .shui::before{
            content: "";
            position: absolute;
            width: 20px;
            height: 20px;
            background: rgba(255, 255, 255, 0.82);
            border-radius: 50%;
            left: 120px;
            top: 55px;
            /*使用关键帧watermove4s播放匀速 无限循环*/
            animation: watermove 4s linear infinite;
      }
      /* 关键帧 */
      @keyframes watermove{
            20%{
                border-radius: 30% 70% 53% 47% / 28% 44% 56% 72%;
            }
         
            40%{
                border-radius: 30% 70% 39% 61% / 34% 39% 61% 66%;
            }
         
            60%{
                border-radius: 25% 75% 45% 55% / 40% 55% 45% 60%;
            }
         
            80%{
                border-radius: 28% 72% 31% 69% / 32% 39% 61% 68%;
            }
      }

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class="shui"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
<p>OK,简简单单,快快乐乐,欢迎交流探讨,白白了你</p>
頁: [1]
查看完整版本: 通过CSS实现逼真水滴动效