段落文字彩條效果,段落文字

來源:互聯網
上載者:User

段落文字彩條效果,段落文字

程式媛and程式猿,兄弟姐妹們,大家周末好,我們今天來研讀一下利用純css實現段落文字的彩條效果。有朋友就說了,不就是css3的漸層嗎,最多加上webkit核心的-webkit-background-clip,嗯,遠不止喲,進來一坐瞭解詳情……

1.案例欣賞



代碼我同樣放到了codepen,大家可以-線上研究-,或-下載收藏-。
需要注意的是,本案例的前面四個效果僅僅支援webkit核心瀏覽器,後面兩個效果支援現代瀏覽器。

2.知識解析

實現本案例您需要:

  • css3漸層
  • -webkit-background-clip: text和-webkit-text-fill-color: transparent
  • sass的function的內涵和自訂
  • 偽對象的使用

我們接下來品味實現過程,強烈建議大家根據上面的原理自行實現,有實現的兄弟歡迎通過評論提交佳作喲。

3.實現過程

我們的html檔案同樣使用了haml,貌似下面代碼這樣。

.container  - 6.times do |i|    %section{class: "lh"}      %p 前端開發whqet,王海慶的技術部落格,關注前端開發,分享相關資源,希望可以對您有所協助。csdn專家部落格http://blog.csdn.net/whqet和個人獨立部落格http://whqet.github.io同步更新,希望可以得到您的支援與肯定,您的支援是我最大的動力!王海慶,浙江郵電職業技術學院軟體技術青椒一枚,其貌不揚、其名不翔,關心技術、熱愛生活,我愛怒放的生命。

然後我們設定下頁面配置和全域css設定,為了簡化問題,本案例使用了sass的scss文法、css reset和-prefix-free。

/*全域變數的設定,行高和彩條顏色*/$lh:1.8em;$colors: #9588DD #DD88C8 #D3DD88 #88B0DD #FFD86F #B4DB6C #0090FB #00965E;/*布局和全域設定*/.container{  counter-reset:counterA;}.container section{  counter-increment:counterA;  position: relative;  padding:10vh 20vw;  background:#2B293D;  border-bottom:1px dashed rgba(255,255,255,.1);  &::before{    content:counter(counterA);    position: absolute;    left:0;    top:0;    font-size:20vw;    color:rgba(255,255,255,.05); } p{   text-indent: 2em;   line-height: $lh;   color:#fff; }}
3.1 彩條效果

案例創意來自Chris Coyier’s Pen One line of Text Dif Color,案例效果如下

我們在實現這個效果時,手動設定漸層的顏色,需要把握的核心在於設定漸層點與段落文字的行高吻合。

/*第一個,彩色條紋,手動指定顏色*/section:nth-of-type(1) {  p {    /*本案例僅僅相容webkit核心瀏覽器*/    -webkit-background-clip: text;    -webkit-text-fill-color: transparent;    /*繁瑣的漸層顏色設定*/    background-image: linear-gradient(      to bottom,      #9588DD,      #9588DD $lh,      #DD88C8 $lh,      #DD88C8 $lh*2,      #D3DD88 $lh*2,      #D3DD88 $lh*3,      #88B0DD $lh*3,      #88B0DD $lh*4,      #FFD86F $lh*4,      #FFD86F $lh*5,      #B4DB6C $lh*5,      #B4DB6C $lh*6,      #0090FB $lh*6,      #0090FB $lh*7,      #00965E $lh*7,      #00965E    );  }}
3.2 函數實現彩條效果

由上面彩條效果實現過程可知,漸層顏色的設定是整個案例實現的關鍵,但是比較不幸的是,這個過程太過danteng,這個煎熬的過程是程式猿都頂不住,於是窮則思變,使用scss的自訂函數來代替繁瑣的人工設定。

/*第二個,彩色條紋,mixins方式讀取預設值*//** * $colors, 漸層顏色序列 * $lh, 段落的行高 *************************************************/@function makestripes($colors,$lh){  $stripes: 'to bottom,';  $i: 1;  $l: length($colors);  @each $color in $colors{    $stripes:       $stripes +       $color + ' ' + ($lh * ($i - 1))       + ',' +       $color + ' ' + ($lh * ($i * 1))    ;    @if ($i < $l){      $stripes: $stripes + ',';    }    $i: $i + 1;  }  @return unquote('repeating-linear-gradient(' + $stripes + ')');}section:nth-of-type(2) {  p {    -webkit-background-clip: text;    -webkit-text-fill-color: transparent;    /*你體會到愉快了嗎?*/    background-image: makestripes($colors,$lh);  }}
3.3 淡出效果

彩條效果多少有點炫技,屬於前端er的玩兒弄,實際應用場合有待探索,但是下面的這個淡出效果應該應用挺廣,比如用在日誌摘要以增加設計的優雅。

本效果的實現,我們同樣從手動設定漸層顏色出發,然後研究sass的函數設定,然後研究相容firefox的實現方式,參見3.3-3.6.

/*第三個,淡出效果,手動方式指定*/section:nth-of-type(3) {  p {    -webkit-background-clip: text;    -webkit-text-fill-color: transparent;    background-image: linear-gradient(      to top,      rgba(white, 0.2),      rgba(white, 0.2) $lh,      rgba(white, 0.4) $lh,      rgba(white, 0.4) $lh*2,      rgba(white, 0.6) $lh*2,      rgba(white, 0.6) $lh*3,      rgba(white, 0.8) $lh*3,      rgba(white, 0.8));    background-position: bottom center;  }}
3.4 函數實現淡入淡出
/*第四個,淡出效果,mixins*//** * $color, 漸層顏色 * $lh, 段落的行高 * $dir, 漸層方向,可接受參數:'to top'表示淡出,'to bottom'表示淡入 * $n, 漸層的快慢,漸層完成的行數,預設為3行 *************************************************/@function makesFade($color:white,$lh:1.5em,$dir:'to top',$n:3){  $stripes:$dir+", ";  @for $i from 1 through $n{    $stripes:       $stripes +       rgba($color,$i/$n) + ' ' + ($lh * ($i - 1))       + ',' +       rgba($color,$i/$n) + ' ' + ($lh * ($i * 1));    @if ($i < $n){      $stripes: $stripes + ',';    }  }  @return unquote('linear-gradient(' + $stripes + ')');}section:nth-of-type(4) {  p {    -webkit-background-clip: text;    -webkit-text-fill-color: transparent;    background-image: makesFade(white,$lh,'to top',4);  }}
3.5 相容firefox的淡出方式

我們知道上面的案例因為使用webkit核心的background-clip,text-fill-color,因此只相容webkit核心的瀏覽器,這不公平。firefox會叫屈不平,所以我們得做點什麼。我們把色彩坡形添加在::after偽對象上覆蓋文字,注意這時,漸層顏色跟背景顏色一樣,透明度從1到0。

/*第五個,相容其他瀏覽器*/section:nth-of-type(5) {  p{    position: relative;  }  p::after{    content:"";    width:100%;    height:100%;    position: absolute;    left:0;    bottom:0;    background-image: linear-gradient(      to top,      rgba(#2B293D, 1),      rgba(#2B293D, 1) $lh,      rgba(#2B293D, 0.8) $lh,      rgba(#2B293D, 0.8) $lh*2,      rgba(#2B293D, 0.6) $lh*2,      rgba(#2B293D, 0.6) $lh*3,      rgba(#2B293D, 0.4) $lh*3,      rgba(#2B293D, 0.4) $lh*4,      rgba(#2B293D, 0.2)    );    background-position: bottom center;  }}
3.6 相容firefox的函數實現淡入淡出
/*第六個,相容其他瀏覽器的mixins*//** * $color, 漸層顏色,這裡就是背景色。 * $lh, 段落的行高 * $dir, 漸層方向,可接受參數:'to top'表示淡出,'to bottom'表示淡入 * $n, 漸層的快慢,漸層完成的行數,預設為3行 *************************************************/@function makesFadeOther($color:white,$lh:1.5em,$dir:'to top',$n:3){  $stripes:$dir+", ";  @for $i from 1 through $n{    $stripes:       $stripes +       rgba($color,1-$i/$n) + ' ' + ($lh * ($i - 1))       + ',' +       rgba($color,1-$i/$n) + ' ' + ($lh * ($i * 1));    @if ($i < $n){      $stripes: $stripes + ',';    }  }  @return unquote('linear-gradient(' + $stripes + ')');}section:nth-of-type(6) {  p{    position: relative;  }  p::after {    content:"";    width:100%;    height:100%;    position: absolute;    left:0;    bottom:0;    background-image: makesFadeOther(#2B293D,$lh,'to top',4);  }}
4.相關閱讀
  • 再說CSS3漸層——線性漸層
  • 再說CSS3漸層——放射狀漸層
  • CSS3 text-fill-color簡介及應用展示
  • CSS3 文字漸層
  • 純CSS3文字效果推薦
  • sass官方文檔
5.著作權說明

前端開發whqet,關注前端開發,分享相關資源。csdn專家部落格,王海慶希望能對您有所協助。
本文歡迎轉載,轉載請註明,保留原文連結。
本文原文連結,http://blog.csdn.net/whqet/article/details/43601051
歡迎大家訪問獨立部落格http://whqet.github.io

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.