CSS3 蓮花盛開動畫

來源:互聯網
上載者:User

See the Pen CSS3 Lotus by haiqing wang (@whqet) onCodePen.

大家先來看看效果,純CSS3實現,為了簡化問題,使用LESS和prefix free。

下面來開工,先看html

<header id="header">  <hgrounp class="white blank">    <h1>CSS3製作蓮花開放</h1>  </hgrounp></header><section class="demo">  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div>  <div class="leaf"></div></section>
利用div.leaf這個DIV來實現葉子。

CSS部分,Less的一些變數和Mixin,主要有opacity、animate和transform。

/*定義變數*/@leafWidth:150px;@bgColor:#333;/*定義Mixins*/// OPACITY.opacity(@val){opacity:@val;@cent:@val*100;filter:~"alpha(opacity=@{cent})";}// Animations.animate(@val){    -webkit-animation: @val;    -moz-animation: @val;    -o-animation: @val;    animation: @val;}// Animation Delay.animate-delay(@val){    -webkit-animation-delay: @val;    -moz-animation-delay: @val;    -o-animation-delay: @val;    animation-delay: @val;}// Transform MIXIN.transform (@val;@origin:0 0) {-webkit-transform: @val;-webkit-transform-origin: @origin;-moz-transform: @val;-moz-transform-origin: @origin;-ms-transform: @val;-ms-transform-origin: @origin;-o-transform: @val;-o-transform-origin: @origin;transform: @val;transform-origin: @origin;}
然後是布局的CSS

body{background-color: @bgColor;}.demo{width:@leafWidth*1.5;height: @leafWidth*1.5;margin: 300px auto 0;position: relative;        .transform(rotate(135deg),center center);.leaf{width: @leafWidth;height: @leafWidth;border-radius: @leafWidth 0px;background: linear-gradient(45deg, rgba(188,190,192,1) 8%,rgba(158,31,99,1) 30%,rgba(158,31,99,1) 100%);.opacity(.5);position: absolute;margin-top: 400px;}}

這裡,使用border-radius將矩形div變成葉子形狀,利用linear-gradient實現葉子填充。

然後將葉子位置初始化

@iterations:10;@degrees:360/@iterations * 1deg;// Looping generator.loop (@index) when (@index>0){&:nth-child(@{iterations}n + @{index}){// Create a skew which is a number of degrees from the root element.animate-delay(@index/10 * 1s);@rotate:@index*@degrees+180deg;.transform(rotate(@rotate), top right);}.loop((@index - 1));}
LESS迴圈遍曆.leaf,進行位置初始化,動畫延遲設定。

然後定義主要畫面格動畫

@keyframes openAnimate{to {.transform(rotate(360deg),top right);}}
在.leaf的css裡,調用剛剛的遍曆迴圈和動畫。

.leaf{width: @leafWidth;height: @leafWidth;border-radius: @leafWidth 0px;background: linear-gradient(45deg, rgba(188,190,192,1) 8%,rgba(158,31,99,1) 30%,rgba(158,31,99,1) 100%);.opacity(.5);position: absolute;margin-top: 400px;                //設定動畫和遍曆.animate(openAnimate 6s ease-in-out infinite alternate);.loop(@iterations);}

完成版的CSS如下所示。

/*定義變數*/@leafWidth:150px;@bgColor:#333;/*定義Mixins*/// OPACITY.opacity(@val){opacity:@val;@cent:@val*100;filter:~"alpha(opacity=@{cent})";}// Animations.animate(@val){    -webkit-animation: @val;    -moz-animation: @val;    -o-animation: @val;    animation: @val;}// Animation Delay.animate-delay(@val){    -webkit-animation-delay: @val;    -moz-animation-delay: @val;    -o-animation-delay: @val;    animation-delay: @val;}// Transform MIXIN.transform (@val;@origin:0 0) {-webkit-transform: @val;-webkit-transform-origin: @origin;-moz-transform: @val;-moz-transform-origin: @origin;-ms-transform: @val;-ms-transform-origin: @origin;-o-transform: @val;-o-transform-origin: @origin;transform: @val;transform-origin: @origin;}@iterations:10;@degrees:360/@iterations * 1deg;// Looping generator.loop (@index) when (@index>0){&:nth-child(@{iterations}n + @{index}){// Create a skew which is a number of degrees from the root element.animate-delay(@index/10 * 1s);@rotate:@index*@degrees+180deg;.transform(rotate(@rotate), top right);}.loop((@index - 1));}@keyframes openAnimate{to {.transform(rotate(360deg),top right);}}body{background-color: @bgColor;}.demo{width:@leafWidth*1.5;height: @leafWidth*1.5;margin: 300px auto 0;position: relative;  .transform(rotate(135deg),center center);.leaf{width: @leafWidth;height: @leafWidth;border-radius: @leafWidth 0px;background: linear-gradient(45deg, rgba(188,190,192,1) 8%,rgba(158,31,99,1) 30%,rgba(158,31,99,1) 100%);.opacity(.5);position: absolute;margin-top: 400px;.animate(openAnimate 6s ease-in-out infinite alternate);.loop(@iterations);}}
好了,整個效果就是這樣。

大家可以到我的codepen線上修改、體驗,或是下載收藏本效果。

---------------------------------------------------------------
前端開發whqet,關注web前端開發技術,分享網頁相關資源。
---------------------------------------------------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.