“等風來不如追風去,追逐的過程就是人生的意義”。
借朋友吉言,“2018在頭條,2019成為頭條”,這就是我2019的目標,我已經在追風的路上。你呢?不要停下腳步,繼續前行吧。
今天來個實用的小知識,看下圖:
CSS3徑向漸變實現優惠券波浪造型
很多人看到左右的波浪邊框,第一想法,應該是用圖片實現。現在我們就打破這一想法,用CSS搞定這個效果。
radial-gradient() 函數用徑向漸變創建 "圖像"。徑向漸變由中心點定義。為了創建徑向漸變你必須設置兩個終止色。
語法: background: radial-gradient(shape size at position, start-color, ..., last-color);
CSS3徑向漸變實現優惠券波浪造型
<div class="coupon"></div>
這里用radial-gradient繪制一個圓,設置left為1px,top為8px,形成半圓。
.coupon { position: relative; width: 400px; height: 160px; margin: 50px auto; background-image: radial-gradient( circle at 1px 8px, transparent 6px, #ff9e6d 6px, #ff9e6d 0px); }
CSS3徑向漸變實現優惠券波浪造型
看看原本是這樣,這里的left是8px
.coupon { ... background-image: radial-gradient( circle at 8px 8px, transparent 6px, #ff9e6d 6px, #ff9e6d 0px); ... }
CSS3徑向漸變實現優惠券波浪造型
設置背景大小,y軸默認平鋪,x軸不允許平鋪,形成多個半圓,造就波浪造型。
.coupon { ... background-image: radial-gradient( circle at 1px 8px, transparent 6px, #ff9e6d 6px, #ff9e6d 0px); background-size: 200px 18px; background-repeat-x: no-repeat; ... }
CSS3徑向漸變實現優惠券波浪造型
同理,我們添加右邊波浪,
.coupon { ... background-image: radial-gradient( circle at 1px 8px, transparent 6px, #ff9e6d 6px, #ff9e6d 0px), radial-gradient( circle at 199px 8px, transparent 6px, #ff9e6d 6px, #ff9e6d 0px); background-size: 200px 18px; background-position: 0 0, 200px 0; background-repeat-x: no-repeat; }
CSS3徑向漸變實現優惠券波浪造型
<div class="coupon">50元</div>
用:before偽類,制作中間的虛線,:after偽類,添加“立即領取”文字。同時添加金額(50元)樣式。
.coupon { ... font-size: 60px; color: #fff; font-weight: bold; line-height: 160px; padding-left: 60px; box-sizing: border-box; cursor: pointer; } .coupon::before { position: absolute; content: ""; left: 240px; top: 0; bottom: 0; width: 0; border-left: 1px dashed #fff; } .coupon::after { position: absolute; content: "立即領取"; font-size: 26px; width: 70px; top: 50%; right: 2%; transform: translate(-50%, -50%); line-height: 40px; letter-spacing: 5px; }
CSS3徑向漸變實現優惠券波浪造型
演示地址:CSS3徑向漸變實現優惠券波浪造型
CSS3 box-shadow實現背景動畫
從淺到深的學習 CSS3陰影(box-shadow)
CSS3線性漸變、陰影、縮放實現動畫下雨效果
擊右上方紅色按鈕關注“web秀”,讓你真正秀起來
在日常項目開發中,在布局方面有遇到哪些問題了?今天來一起看看CSS布局有哪些小技巧,后續開發更輕松。本文主要通過簡單的示例,講述開發中遇到的布局等問題,但不僅限于布局相關,會有其他相關知識點。
CSS實用技巧第二講:布局處理
移動端使用rem布局需要通過JS設置不同屏幕寬高比的font-size,結合vw單位和calc()可脫離JS的控制,代碼如下:
/* 基于UI width=750px DPR=2的頁面 */ html { font-size: calc(100vw / 7.5); }
rem 頁面布局, 不兼容低版本移動端,使用需謹慎。
通過flexbox或inline-block的形式橫向排列元素,對父元素設置overflow-x:auto橫向滾動查看。注意場景: 橫向滾動列表、元素過多但位置有限的導航欄。
CSS實用技巧第二講:布局處理
<div class="horizontal-list flex"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節跳動</li> <li>騰訊</li> <li>百度</li> <li>美團</li> </ul> </div> <div class="horizontal-list inline"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節跳動</li> <li>騰訊</li> <li>百度</li> <li>美團</li> </ul> </div>
scss樣式
.horizontal-list { width: 300px; height: 100px; ul { overflow-x: scroll; cursor: pointer; margin: 0; padding: 0; &::-webkit-scrollbar { height: 6px; } &::-webkit-scrollbar-track { background-color: #f0f0f0; } &::-webkit-scrollbar-thumb { border-radius: 5px; background: linear-gradient(to right, #32bbff, #008cf4); } } li { overflow: hidden; margin-left: 10px; height: 90px; background-color: #00b7a3; line-height: 90px; text-align: center; font-size: 16px; color: #fff; &:first-child { margin-left: 0; } } } .flex { ul { display: flex; flex-wrap: nowrap; justify-content: space-between; } li { flex-shrink: 0; flex-basis: 90px; } } .inline { margin-top: 10px; height: 102px; ul { overflow-y: hidden; white-space: nowrap; } li { display: inline-block; width: 90px; } }
知識點解析:
1、display: flex布局:又名“彈性布局”,任何一個容器都可以指定為Flex布局。詳細內容請點擊
《CSS3中Flex彈性布局該如何靈活運用?》
2、滾動條樣式美化。
如何自定義滾動條樣式了? 掌握這3個選擇器即可。
(1)、::-webkit-scrollbar 定義了滾動條整體的樣式;
(2)、::-webkit-scrollbar-thumb 滑塊部分;
(3)、::-webkit-scrollbar-track 軌道部分;
所以上面scss代碼中,我們書寫了這3個選擇器的樣式,改變了滾動條的樣式。
3、linear-gradient線性漸變。語法如下:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction用角度值指定漸變的方向(或角度), color-stop1, color-stop2,...用于指定漸變的起止顏色。
to right的意思就是從左到右,從一個顏色過渡到另外一個顏色。
請看示例:
CSS實用技巧第二講:布局處理
更多詳細內容請點擊:
《CSS3線性漸變、陰影、縮放實現動畫下雨效果》
《CSS3線性徑向漸變、旋轉、縮放動畫實現王者榮耀匹配人員加載頁面》
《CSS3徑向漸變實現優惠券波浪造型》
在retina屏中,像素比為2(iPhone6/7/8)或3(iPhone6Plus/7Plus/8Plus),1px的邊框看起來比真的1px更寬。
我們可以通過偽類加transform的方式解決。
CSS實用技巧第二講:布局處理
transform:用于元素進行旋轉、縮放、移動或傾斜,和設置樣式的動畫并沒有什么關系,就相當于color一樣用來設置元素的“外表”。
詳細transform了解,請點擊:
《CSS3最容易混淆屬性transition transform animation translate》
非常簡單的方式,flexbox橫向布局時,最后一個元素通過margin-left:auto實現向右對齊。
請看示例:
<ul class="nav-list"> <li>HTML5</li> <li>CSS3</li> <li>JAVASCRIPT</li> <li>TYPESCRIPT</li> <li>THREE.JS</li> <li>NODE</li> </ul>
css樣式
.nav-list { display: flex; align-items: center; padding: 0 10px; width: 700px; height: 60px; background-color: #00b7a3; } .nav-list li { padding: 0 10px; height: 40px; line-height: 40px; font-size: 16px; color: #fff; list-style: none; } .nav-list li + li { margin-left: 10px; } .nav-list li:last-child { margin-left: auto; }
CSS實用技巧第二講:布局處理
<div class="accordion"> <input type="checkbox" id="collapse1"> <input type="checkbox" id="collapse2"> <input type="checkbox" id="collapse3"> <article> <label for="collapse1">web秀</label> <p>html<br>javascript<br>css<br>uni app</p> </article> <article> <label for="collapse2"></label> <p>新聞<br>圖片<br>視頻<br>養生</p> </article> <article> <label for="collapse3">阿里巴巴</label> <p>淘寶<br>阿里云<br>閑魚<br>天貓</p> </article> </div>
scss樣式
.accordion { width: 300px; article { margin-top: 5px; cursor: pointer; &:first-child { margin-top: 0; } } input { display: none; padding: 0; margin: 0; &:nth-child(1):checked ~ article:nth-of-type(1) p, &:nth-child(2):checked ~ article:nth-of-type(2) p, &:nth-child(3):checked ~ article:nth-of-type(3) p { border-bottom-width: 1px; max-height: 600px; } } label { display: block; padding: 0 20px; height: 40px; background-color: #00b7a3; cursor: pointer; line-height: 40px; font-size: 16px; color: #fff; } p { overflow: hidden; padding: 0 20px; margin: 0; border: 1px solid #00b7a3; border-top: none; border-bottom-width: 0; max-height: 0; line-height: 30px; transition: all 500ms; } }
CSS實用技巧第二講:布局處理
<div class="tab-navbar"> <input type="radio" name="tab" id="tab1" checked> <input type="radio" name="tab" id="tab2"> <input type="radio" name="tab" id="tab3"> <input type="radio" name="tab" id="tab4"> <nav> <label for="tab1">首頁</label> <label for="tab2">列表</label> <label for="tab3">其他</label> <label for="tab4">我的</label> </nav> <main> <ul> <li>首頁</li> <li>列表</li> <li>其他</li> <li>我的</li> </ul> </main> </div>
scss樣式
*{ padding: 0; margin: 0; } .active { background-color: #00b7a3; color: #fff; } .tab-navbar { display: flex; overflow: hidden; flex-direction: column-reverse; border-radius: 10px; width: 300px; height: 400px; margin: 100px auto; input { display: none; &:nth-child(1):checked { & ~ nav label:nth-child(1) { @extend .active; } & ~ main ul { background-color: #f13f84; transform: translate3d(0, 0, 0); } } &:nth-child(2):checked { & ~ nav label:nth-child(2) { @extend .active; } & ~ main ul { background-color: #44bb44; transform: translate3d(-25%, 0, 0); } } &:nth-child(3):checked { & ~ nav label:nth-child(3) { @extend .active; } & ~ main ul { background-color: #ff7632; transform: translate3d(-50%, 0, 0); } } &:nth-child(4):checked { & ~ nav label:nth-child(4) { @extend .active; } & ~ main ul { background-color: #00bbdd; transform: translate3d(-75%, 0, 0); } } } nav { display: flex; height: 40px; background-color: #f0f0f0; line-height: 40px; text-align: center; label { flex: 1; cursor: pointer; transition: all 300ms; } } main { flex: 1; ul { display: flex; flex-wrap: nowrap; width: 400%; height: 100%; transition: all 300ms; } li { display: flex; justify-content: center; align-items: center; flex: 1; font-weight: bold; font-size: 20px; color: #fff; } } }
CSS實用技巧第二講:布局處理
喜歡小編或者覺得小編文章對你有幫助的,可以點擊一波關注哦!同時,要源碼的小伙伴可以點擊下方“了解更多”。
最后推薦一個專欄文章,感謝小伙伴們多多支持,謝謝大家!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。