成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

前端:使用純css實現超實用的圖標庫(附源碼)

開發 前端
今天我們來復盤一下前端中css偽元素的知識以及如何用css偽元素來減輕javascript的壓力,做出一些腦洞大開的圖形。

今天我們來復盤一下前端中css偽元素的知識以及如何用css偽元素來減輕javascript的壓力,做出一些腦洞大開的圖形。

[[398694]]

預備知識

偽元素

偽元素是一個附加至選擇器末的關鍵詞,允許你對被選擇元素的特定部分修改樣式。偽元素主要有:

  • ::first-letter 第一個字母的樣式
  • ::first-line 首行文字的樣式
  • ::before 元素頭部添加的修飾
  • ::after 元素尾部添加的修飾
  • ::placeholder input的占位符樣式
  • ::selection 被選中元素的樣式

我個人覺得偽元素可以解釋為元素的修飾,可以為元素帶來額外的附加樣式,屬于額外的文檔結構。

偽類

用來表示無法在CSS中輕松或者可靠檢測到的某個元素的狀態或屬性,比如a標簽的hover表示鼠標經過的樣式,visited表示訪問過的鏈接的樣式,更多的用來描述元素狀態變化時的樣式,偽類主要有:

  • :link
  • :visited
  • :hover
  • :active
  • :focus
  • :lang(fr)
  • :not(s)
  • :root
  • :first-child
  • :last-child
  • :only-child
  • :nth-child(n)
  • :nth-last-child(n)
  • :first-of-type
  • :last-of-type
  • :only-of-type
  • :nth-of-type(n)
  • :nth-last-of-type(n)
  • :empty
  • :checked
  • :enabled
  • :disabled
  • :target

我們利用css偽類和偽元素可以實現很多強大的視覺效果,這里我主要介紹偽元素,如果對偽類或其他css特性感興趣,可以看看我之前的css文章,寫的很全面。

正文

先看看我們用純css實現的圖標庫:

當然,如果我們知道了做出如上圖標的css原理,那么我們就可以實現更加豐富復雜的圖形,甚至可以打造自己的圖標庫。接下來我會介紹實現如上圖標的方式和方法,并給出部分源碼,方便大家學習。

原理

我們實現如上css圖標是基于偽元素的,可以利用偽元素的::before和::after和content屬性來為元素添加額外視覺效果,我們在上文中也介紹了偽元素的概念和類型,接下來讓我們來實現它吧~

實現箭頭

思路其實就是利用元素的::before偽元素畫一個三角形,用css設置span樣式為一條線并進行布局定位:

  1. // less 
  2. .arrow { 
  3.     position: relative
  4.     display: inline-block; 
  5.     line-height: 0; 
  6.     background-color: #ccc; 
  7.     &.arrow-hor { 
  8.         width: 16px; 
  9.         height: 1px; 
  10.     } 
  11.     &.arrow-hor.right::before { 
  12.         content: ''
  13.         position: absolute
  14.         top: -4px; 
  15.         right: -8px; 
  16.         border: 4px solid transparent; 
  17.         border-left: 4px solid #ccc; 
  18.     } 
  19.  
  20. // html 
  21. <span class="arrow arrow-hor right"></span> 

這樣就實現了一個指向右的箭頭,我們用類似的方法也可以實現左箭頭,上下箭頭,實現雙向箭頭只需要加一個::after偽類并做適當定位就好了。

實現搜索圖標

實現搜索圖標實際上只需要一個圓和一根線,然后我們會用transform:ratate來實現角度傾斜,具體實現如下:

  1. // less 
  2. .search { 
  3.     position: relative
  4.     display: inline-block; 
  5.     width: 12px; 
  6.     height: 12px; 
  7.     border-radius: 50%; 
  8.     border: 1px solid #ccc; 
  9.     text-align: center; 
  10.     transform: rotate(-45deg); 
  11.     &::after { 
  12.         content: ''
  13.         display: inline-block; 
  14.         width: 1px; 
  15.         height: 4px; 
  16.         background-color: #ccc; 
  17.     } 
  18. // html 
  19. <span class="search"></span> 

實現頭像

實現頭像我們要利用before和after偽元素,分別做人物頭部和身體,身體我們會用css畫一個橢圓來做:

  1. // less 
  2. .dot-pan { 
  3.     position: relative
  4.     display: inline-flex; 
  5.     width: 60px; 
  6.     height: 60px; 
  7.     line-height: 0; 
  8.     align-items: center; 
  9.     border-radius: 50%; 
  10.     background-color: #06c; 
  11.     transform: rotate(-90deg); 
  12.     &::before { 
  13.         content: ''
  14.         display: inline-block; 
  15.         width: 28px; 
  16.         height: 40px; 
  17.         margin-left: -3px; 
  18.         border-radius: 50% 50%; 
  19.         flex-shrink: 0; 
  20.         background-color: #fff; 
  21.     } 
  22.  
  23.     &::after { 
  24.         content: ''
  25.         display: inline-block; 
  26.         width: 20px; 
  27.         height: 20px; 
  28.         flex-shrink: 0; 
  29.         border-radius: 50%; 
  30.         background-color: #fff; 
  31.     } 
  32. // html 
  33. <span class="search"></span> 

實現地點圖標

地點圖標由一個圓和一個三角形組成,如果要做的精致一點,我們可以再用一個偽元素來做一個定點:

  1. // less 
  2. .locate-icon { 
  3.     position: relative
  4.     display: inline-block; 
  5.     width: 50px; 
  6.     height: 50px; 
  7.     border-radius: 50%; 
  8.     background-color: #06c; 
  9.     &::before { 
  10.         content: ''
  11.         position: absolute
  12.         display: inline-block; 
  13.         width: 12px; 
  14.         height: 12px; 
  15.         border-radius: 50%; 
  16.         left: 50%; 
  17.         top: 50%; 
  18.         transform: translate(-50%, -50%); 
  19.         background-color: #fff; 
  20.     } 
  21.     &::after { 
  22.         content: ''
  23.         margin-top: 45px; 
  24.         display: inline-block; 
  25.         border: 15px solid transparent; 
  26.         border-top-color: #06c; 
  27.     } 
  28. // html 
  29. <span class="locate-icon mr-20"></span> 

實現微信圖標

圖中2個眼睛主要是用到一個偽元素加上box-shadow來實現,這樣可以節約一個偽元素用來做小尾巴,至于如何實現不同形狀的三角形,如果有不懂的可以和我交流,具體實現如下:

  1. // less 
  2. .wechat-icon { 
  3.     display: inline-block; 
  4.     width: 50px; 
  5.     height: 50px; 
  6.     border-radius: 50%; 
  7.     background-color: rgb(68, 170, 59); 
  8.     &::before { 
  9.         content: ''
  10.         margin-top: 14px; 
  11.         position: absolute
  12.         width: 4px; 
  13.         height: 4px; 
  14.         border-radius: 50%; 
  15.         background-color: #fff; 
  16.         box-shadow: 16px 0 0 #fff; 
  17.     } 
  18.     &::after { 
  19.         content: ''
  20.         margin-top: 42px; 
  21.         display: inline-block; 
  22.         border-width: 6px 10px 6px 10px; 
  23.         border-style: solid; 
  24.         border-color: transparent; 
  25.         border-top-color: rgb(68, 170, 59); 
  26.         transform: rotate(-147deg); 
  27.     } 
  28. // html 
  29. <span class="wechat-icon mr-20"></span> 

實現對勾圖標

這里也很簡單,我們用偽元素的content里放置一個勾號,然后設置顏色大小就好啦:

  1. // less 
  2. .yes-icon { 
  3.     display: inline-flex; 
  4.     justify-content: center; 
  5.     align-items: center; 
  6.     width: 48px; 
  7.     height: 48px; 
  8.     background-color: green; 
  9.     border-radius: 50%; 
  10.     &::after { 
  11.         content: '✓'
  12.         color: #fff; 
  13.         font-size: 30px; 
  14.         font-weight: bold; 
  15.     } 
  16. // html 
  17. <span class="yes-icon mr-20"></span> 

實現眼睛(一般用于網站訪問量圖標)

主要是做橢圓加上一個圓形的偽元素:

  1. .view-icon { 
  2.     display: inline-flex; 
  3.     justify-content: center; 
  4.     align-items: center; 
  5.     width: 58px; 
  6.     height: 36px; 
  7.     background-color: #06c; 
  8.     border-radius: 50%; 
  9.     &::after { 
  10.         content: ''
  11.         display: inline-block; 
  12.         width: 20px; 
  13.         height: 20px; 
  14.         border-radius: 50%; 
  15.         background-color: #fff; 
  16.     } 

導航圖標

原理類似,主要思想是畫兩個三較形,用偽元素的三角形遮住主元素底部即可:

  1. .gps-icon { 
  2.     position: relative
  3.     display: inline-flex; 
  4.     justify-content: center; 
  5.     align-items: center; 
  6.     border-width: 30px 15px 30px 15px; 
  7.     border-color: transparent; 
  8.     border-style: solid; 
  9.     border-bottom-color: #06c; 
  10.     transform: translate(10px, -16px) rotate(32deg); 
  11.     &::after { 
  12.         position: absolute
  13.         bottom: -48px; 
  14.         content: ''
  15.         display: inline-block; 
  16.         border-width: 10px 38px 30px 38px; 
  17.         border-color: transparent; 
  18.         border-style: solid; 
  19.         border-bottom-color: #fff; 
  20.     } 

實現心形圖標

原理就是用兩個偽元素實現兩個橢圓,旋轉重合即可:

  1. .logo-x { 
  2.     position: relative
  3.     display: inline-flex; 
  4.     width: 50px; 
  5.     height: 50px; 
  6.     border-radius: 50%; 
  7.     background-color: rgb(212, 73, 17); 
  8.     &::after { 
  9.         position: absolute
  10.         content: ''
  11.         left: 10px; 
  12.         top: 12px; 
  13.         display: inline-block; 
  14.         width: 20px; 
  15.         height: 30px; 
  16.         border-radius: 50%; 
  17.         background-color: #fff; 
  18.         transform: rotate(135deg); 
  19.     } 
  20.     &::before { 
  21.         position: absolute
  22.         content: ''
  23.         right: 10px; 
  24.         top: 12px; 
  25.         display: inline-block; 
  26.         width: 20px; 
  27.         height: 30px; 
  28.         border-radius: 50%; 
  29.         background-color: #fff; 
  30.         transform: rotate(-135deg); 
  31.     } 

ps圖標

這個也是用偽元素,一個偽元素用來做文字圖形,一個用來做遮罩,來形成偽立體感:

  1. .logo-ps { 
  2.     position: relative
  3.     display: inline-flex; 
  4.     justify-content: center; 
  5.     align-items: center; 
  6.     width: 50px; 
  7.     height: 50px; 
  8.     border-radius: 8px; 
  9.     background-color: rgb(15, 102, 184); 
  10.     &::before { 
  11.         position: absolute
  12.         content: ''
  13.         display: inline-block; 
  14.         width: 50px; 
  15.         height: 40px; 
  16.         background-color: rgba(255, 255, 255, .1); 
  17.     } 
  18.     &::after { 
  19.         position: absolute
  20.         content: 'PS'
  21.         font-size: 24px; 
  22.         display: inline-block; 
  23.         color: #fff; 
  24.     } 

實現氣泡對話框

和微信圖標類似:

  1. .logo-pp { 
  2.     display: inline-block; 
  3.     width: 150px; 
  4.     height: 50px; 
  5.     border-radius: 8px; 
  6.     background-color: rgb(68, 170, 59); 
  7.     &::before { 
  8.         content: '等你下課哦!'
  9.         margin-top: 14px; 
  10.         margin-left: -33px; 
  11.         position: absolute
  12.         color: #fff; 
  13.     } 
  14.     &::after { 
  15.         content: ''
  16.         margin-top: 42px; 
  17.         display: inline-block; 
  18.         border-width: 6px 10px 6px 10px; 
  19.         border-style: solid; 
  20.         border-color: transparent; 
  21.         border-top-color: rgb(68, 170, 59); 
  22.         transform: rotate(-147deg) translate(-12px, 6px); 
  23.     } 

由于篇幅原因,其他的圖標就不一一實現了,原理都類似,筆者之前曾利用這個方案做過一套100個圖標的庫,成功應用于各個項目中,由于體積小不會造成額外請求,所以更加實用,但更復雜的圖形就為了方便還是建議用字體圖標或者svg,base64等。

 

責任編輯:姜華 來源: 趣談前端
相關推薦

2016-11-01 09:46:04

2013-04-08 14:07:28

CSS

2021-09-17 06:56:54

前端Iconfont圖標庫

2011-04-18 17:17:45

CSSweb開發

2022-03-28 08:44:15

css3水波動畫

2021-02-09 07:26:38

前端css技術熱點

2023-05-08 09:08:33

CSS前端

2023-05-05 17:11:11

前端Mousetrap方式

2023-11-02 10:14:50

TinykeysWeb應用

2015-12-01 09:52:03

CSS3動畫源碼

2021-06-15 15:21:34

開發技能代碼

2021-10-19 22:23:47

CSSBeautiful按鈕

2024-08-29 08:13:58

2021-01-19 12:16:10

CSS前端UI

2023-07-24 15:24:00

前端CSS 技巧

2017-04-27 14:05:59

CSS動畫前端

2011-01-21 07:22:48

jQuerywebJavaScript

2015-07-14 09:29:44

圖標設計

2022-02-21 07:02:16

CSSbeautiful按鈕

2022-08-10 16:08:38

鴻蒙CSS
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品极品美女在线观看免费 | 亚洲欧洲色视频 | 欧美国产中文 | 一级片aaa| 精品国产乱码久久久久久影片 | 中文字幕av在线 | 国产情侣一区 | 国产精品日日夜夜 | 日韩三级在线观看 | www久久爱 | 91文字幕巨乱亚洲香蕉 | 亚洲福利av | 91久久视频| 91视在线国内在线播放酒店 | 久久躁日日躁aaaaxxxx | 日韩欧美亚洲 | 精品久久中文字幕 | 特黄色毛片 | 在线一区二区三区 | 久久精品久久久久久 | 久久精品亚洲成在人线av网址 | 99热首页 | www.亚洲一区二区三区 | 免费视频二区 | 日本激情视频网 | 狠狠av | 国产精品激情 | 日本福利在线观看 | 国产精品久久久久久 | 色黄爽 | 91精品国产综合久久久久久漫画 | 超碰人人91 | 一级毛片免费视频观看 | 国产精品夜夜春夜夜爽久久电影 | 91色在线 | 无码日韩精品一区二区免费 | 国产午夜一级 | 欧美日韩在线视频一区二区 | 欧美日韩成人在线 | 亚洲精品自在在线观看 | 欧美福利三区 |