同事幫我用css做出炫酷的卡券效果,深感崇拜啊

来源:https://www.cnblogs.com/coderhf/archive/2020/07/06/13255260.html
-Advertisement-
Play Games

前言 前幾天,我接到了一個項目,模塊中要寫一個卡券效果,當時沒有圖片,也就是要用css來實現,當時我是懵逼的,也沒有寫過這樣的,一時間不知道怎麼寫,畢竟要寫的像UI設計的一樣美觀。我就只好求救我的大神級別的同事了。不僅css玩的溜,人家JavaScript玩的更溜,閣下實在是佩服。 常見的卡券樣式如 ...


前言

前幾天,我接到了一個項目,模塊中要寫一個卡券效果,當時沒有圖片,也就是要用css來實現,當時我是懵逼的,也沒有寫過這樣的,一時間不知道怎麼寫,畢竟要寫的像UI設計的一樣美觀。我就只好求救我的大神級別的同事了。不僅css玩的溜,人家JavaScript玩的更溜,閣下實在是佩服。

常見的卡券樣式如下:

image.png

 

同事二話沒說,直接給我寫了一種,那真的是快如閃電就給我實現了一個。是用偽元素實現的

使用偽元素實現(Less 版本)

 

image.png

ticket.less

 

 
.ordinary-mixins-ticket-horizontal(@width,@height,@r,@top, @color) {
   position: relative;
   box-sizing: border-box;
   padding: 0 @r;
   width: @width;
   height: @height;
   background-clip: content-box;
   background-color: @color;
 ​
   &::before {
     position: absolute;
     top: 0;
     left: 0;
     content: "";
     display: block;
     width: @r + 1px;
     height: 100%;
     background: radial-gradient(@r circle at left @top, transparent @r, @color @r + 1px);
   }
 ​
   &::after {
     position: absolute;
     top: 0;
     right: 0;
     content: "";
     display: block;
     //這裡的 @r + 1px 是為了避免某些百分百比縮放頁面時,出現空隙
     width: @r + 1px;
     height: 100%;
     //這裡的 @r + 1px 是為了防止出現鋸齒
     background: radial-gradient(@r circle at right @top, transparent @r, @color @r + 1px);
   }
 }
 ​
 .parent {
   .ordinary-mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28);
 }
 .child {
   line-height: 200px;
 }
App.js

 import React from 'react';
 import './App.css';
 import './ticket.less';
 ​
 function App() {
     return (
         <div className="App" style={
             {
                 display: "flex",
                 justifyContent: "center",
                 alignItems: "center",
                 height: 600
             }
         }>
             <div className={'parent'}>
                 <div className="child">666</div>
             </div>
         </div>
     );
 }
 ​
 export default App;

 

 

後面我回去之後,仔細去研究了一下,想把它做得更好看,然後就有了升級版樣式一

升級版樣式一(Less 版本)

 

image.png

 

 .mixins-ticket-vertical(@width, @height, @r, @left, @l-color, @r-color) {
   width: @width;
   height: @height;
   // @left - 1px 是為了避免某些百分百比縮放頁面時,出現空隙
   // @r + 1px 是為了防止出現鋸齒
   // 51% 是為了防止出現元素中間會有一小段空白區域的情況
   background: radial-gradient(circle at left top, transparent @r, @l-color  @r + 1px) @left - 1px top ~'/' 100% 51% no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color  @r + 1px) @left - 1px bottom ~'/' 100% 51% no-repeat,
     radial-gradient(circle at  right top, transparent @r, @r-color  @r + 1px ) -(@width - @left) top ~'/' 100% 51% no-repeat,
     radial-gradient(circle at right bottom , transparent @r, @r-color  @r + 1px )  -(@width - @left) bottom ~'/' 100% 51% no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 }
 ​
 .mixins-ticket-vertical-two(@width, @height, @r, @left, @l-color, @r-color) {
   width: @width;
   height: @height;
   // @left + 1px 是為了避免某些百分百比縮放頁面時,出現空隙
   // @r + 1px 是為了防止出現鋸齒
   // 51% 是為了防止出現元素中間會有一小段空白區域的情況
   background: radial-gradient(circle at left top, transparent @r, @r-color @r + 1px) right top ~'/' (@width - @left) 51% no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) right bottom ~'/' (@width - @left)   51% no-repeat,
   radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) left top ~'/' @left + 1px 51% no-repeat,
   radial-gradient(circle at right bottom, transparent @r, @l-color @r + 1px) left bottom ~'/' @left + 1px   51% no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 }
 ​
 .parent {
   .mixins-ticket-vertical(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
   //.mixins-ticket-vertical-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
 }
 .child {
   line-height: 200px;
 }

 

升級版樣式一(Scss 版本)

 @mixin mixins-ticket-vertical($width, $height, $r, $left, $l-color, $r-color) {
   width: $width;
   height: $height;
   // $left - 1px 是為了避免某些百分百比縮放頁面時,出現空隙
   // $r + 1px 是為了防止出現鋸齒
   // 51% 是為了防止出現元素中間會有一小段空白區域的情況
   background: radial-gradient(circle at left top, transparent $r, $l-color  $r + 1px) $left - 1px top / 100% 51% no-repeat,
   radial-gradient(circle at left bottom, transparent $r, $l-color  $r + 1px) $left - 1px bottom / 100% 51% no-repeat,
   radial-gradient(circle at  right top, transparent $r, $r-color  $r + 1px ) -($width - $left) top / 100% 51% no-repeat,
   radial-gradient(circle at right bottom , transparent $r, $r-color  $r + 1px )  -($width - $left) bottom / 100% 51% no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 }
 ​
 .parent {
   @include mixins-ticket-vertical(200px, 200px, 10px, 150px, #fc3a28, #fc3a28);
 }
 ​
 .child {
   line-height: 200px;
 }

 

後面也接連改了幾次,大家看一看效果怎麼樣

升級版樣式二(Less 版本)

 

image.png

 

 .mixins-ticket-horizontal(@width, @height, @r, @top, @l-color, @r-color) {
   width: @width;
   height: @height;
   background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) left  @top - 1px ~'/' 51% 100% no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left -(@height - @top) ~'/' 51% 100% no-repeat,
   radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right  @top - 1px ~'/' 51% 100% no-repeat,
   radial-gradient(circle at right bottom, transparent @r, @r-color @r + 1px) right -(@height - @top) ~'/' 51% 100% no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 }
 ​
 .mixins-ticket-horizontal-two(@width, @height, @r, @top, @l-color, @r-color) {
   width: @width;
   height: @height;
   background: radial-gradient(circle at left top, transparent @r, @r-color @r + 1px) left bottom ~'/' 51%  (@height - @top) no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left top ~'/' 51%  @top + 1px  no-repeat,
   radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right bottom ~'/' 51%  (@height - @top) no-repeat,
   radial-gradient(circle at right bottom, transparent @r, @l-color @r + 1px) right top ~'/' 51% @top + 1px no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 }
 ​
 .parent {
   .mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
   //.mixins-ticket-horizontal-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
 }
 .child {
   line-height: 200px;
 }

 

升級版樣式三(Less 版本)

 

image.png

 

 .mixins-ticket-horizontal-line(@width, @height, @r, @top, @l-color, @r-color,@border-offset,@border-color) {
   width: @width;
   height: @height;
   background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) left @top - 1px ~'/' 51% 100% no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left -(@height - @top) ~'/' 51% 100% no-repeat,
   radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right @top - 1px ~'/' 51% 100% no-repeat,
   radial-gradient(circle at right bottom, transparent @r, @r-color @r + 1px) right -(@height - @top) ~'/' 51% 100% no-repeat;
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 ​
   &::after {
     position: absolute;
     left: 0;
     right: 0;
     top: @top;
     margin: auto;
     content: '';
     width: calc(~"100%" - 2*@r - @border-offset);
     border-top: 1px dashed @border-color;
   }
 }
 ​
 .parent {
   .mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
   //.mixins-ticket-horizontal-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28);
 }
 .child {
   line-height: 200px;
 }

 

升級版樣式四(Less 版本)

 

image.png

 

 .mixins-ticket-vertical-three(@width, @height, @r, @left, @l-color, @r-color,@sm-r,@sm-offset,@sm-color) {
   width: @width;
   height: @height;
   // @left - 1px 是為了避免某些百分百比縮放頁面時,出現空隙
   // @r + 1px 是為了防止出現鋸齒
   // 51% 是為了防止出現元素中間會有一小段空白區域的情況
   background: radial-gradient(circle at left top, transparent @r, @l-color  @r + 1px) @left - 1px top ~'/' 100% 51% no-repeat,
   radial-gradient(circle at left bottom, transparent @r, @l-color  @r + 1px) @left - 1px bottom ~'/' 100% 51% no-repeat,
     radial-gradient(circle at  right top, transparent @r, @r-color  @r + 1px ) -(@width - @left) top ~'/' 100% 51% no-repeat,
     radial-gradient(circle at right bottom , transparent @r, @r-color  @r + 1px )  -(@width - @left) bottom ~'/' 100% 51% no-repeat;
 ​
   filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2));
 ​
   &::after {
     content: '';
     position: absolute;
     top: 0;
     right: -@sm-r;
     width: @sm-r;
     height: 100%;
     background-image: radial-gradient(circle at @sm-r, transparent @sm-r, @r-color @sm-r + 1px);
     //background-size: @sm-r;
     background-size: @sm-r @sm-offset;
   }
 }
 ​
 .parent {
   .mixins-ticket-vertical-three(200px, 200px, 10px, 150px, #fc3a28, #fc3a28,5px,20px,#fc3a28);
 }
 ​
 .child {
   line-height: 200px;
 }

 

註意事項

 // ticket.less
 ​
 //background: radial-gradient(circle at top right, transparent @r, @lcolor 0) -(@width - @left) top ~'/' 100% 55% no-repeat;
 // 將上面的這行代碼拆解如下:
 //background-image: radial-gradient(circle at top right, transparent @r, @lcolor 0);
 //background-position:-(@width - @left) top ;
 //background-size:100% 55% ;
 //background-repeat: no-repeat;
 ​
 /*註意:這裡先是有 50px 的透明區域,緊接著第二個區域設置了 0 ,可以理解為從這裡開始重新設置樣式區間*/
 /*background: radial-gradient(circle at top right, transparent 50px, red 0, #66a8ff 50%);*//*在Chrome下,如果兩個區域之間顏色直接以 0 偏差過渡,會有比較嚴重的鋸齒*/
 /*background: radial-gradient(10px at left,transparent 50%,#F6327C 50%);*/
 /*background: radial-gradient(10px at left,transparent 50%,#F6327C 55%);*//*加陰影效果*/
 /*filter: drop-shadow(2px 2px 2px rgba(0,0,0,.2));*//*多個徑向漸變累加*/
 /*background: 
 radial-gradient(50px 100px ellipse, transparent 40px, yellow 41px, red 49px, transparent 50px),
 radial-gradient(30px circle, red, red 29px, transparent 30px);*/

 

後面我對照這幾個,最後我選了最後一種樣式,我覺得這個比較酷,最後拿過去給他們看,他們也覺得這個方案非常好看,得到了別人的認可,我也覺得很開心。

如果你覺得這篇文章不錯,請別忘記點個關註哦~,我會繼續努力創作好的文章的

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • iOS行業在經歷了過去幾年的爆發期後,現在到了一個相對冷靜的時期,一個良幣驅逐劣幣、去偽存真的階段。只有持續的專註和付出,才能夠在激烈的競爭中脫穎而出,成為強者。正如狄更斯所言,“這是一個最壞的時代,也是最好的時代” 。 對於這些面試題,不要死記硬背,應該舉一反三,深刻理解實現機制(這也是科班和非科 ...
  • 新聞 Android的電話應用將能夠告訴你為什麼企業要給你打電話 Google Play Store可能會重新開始顯示應用更新通知 谷歌確認將推出新功能 對標蘋果AirDrop 谷歌新版SafetyNet可能會讓root和定製ROM走向終結 Android版Gboard輸入法正測試面向IM應用的自動 ...
  • H分形是由一個字母H演化出迷宮一樣場景的分形圖案,其構造過程是:取一個中心點(x,y),以此中心點繪製一條長為L的水平直線和兩條長為H的豎直直線,構成一個字母“H”的形狀;再以兩條豎直直線的上下共4個端點為中心點,分別繪製一條長為L/2的水平直線和兩條長為H/2的豎直直線;重覆以上操作直至達到要求的 ...
  • 德國數學家David Hilbert在1891年構造了一種曲線,首先把一個正方形等分成四個小正方形,依次從西北角的正方形中心出發往南到西南正方形中心,再往東到東南角的正方形中心,再往北到東北角正方形中心,這是一次迭代;如果對四個小正方形繼續上述過程,往下劃分,反覆進行,最終就得到一條可以填滿整個正方 ...
  • 實現 React Hooks UI 開發有兩個問題: 展示覆用 邏輯復用 展示覆用目前基本使用組件化來解決,邏輯復用一直以來都沒有特別好的解決方案。React 從一開始的 mixin ,到 高階組件 以及 Render Props ,都是在試圖解決這個問題,但是都引入了一些別的問題。 Mixins ...
  • <script type="text/javascript"> function clickIE4() { if (event.button == 2) { return false; } } function clickNS4(e) { if (document.layers || documen ...
  • 最近在教我老婆學習前端,她說想要學習前端,自己在家賺點外快,自己賺點家用。我也拗不過她,而且其實我也挺佩服的。所以就教她了。最近我想考一考她對css中偽類的瞭解,所以就問了她瞭解css多少個偽類,偽類是什麼? 她說css 偽類是用於向某些選擇器添加特殊的效果,是動態的,指當前元素所處的狀態或者特性。 ...
  • 直接複製就能用 wxml <view bindtap="showModal">點這裡</view> <view class="wrap"> <view class="modal modal-bottom-dialog" hidden="{{hideFlag}}"> <view class="moda ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...