關於使用svg構建六邊形蜂巢列表的方式

来源:https://www.cnblogs.com/chenshuipeng/archive/2020/04/07/svg-path.html
-Advertisement-
Play Games

由於項目需求,需要在頁面中畫出一套蜂巢排版格式的列表。在經過網上的一番查閱資料,發現大多六邊形的畫法都是通過div覆蓋由旋轉的菱形遮掩為六邊形。這種六邊形雖然可以滿足一些基本的需求,但我暫時沒有想到如何滿足我想要的邊框動態效果。最後尋覓下發現了一款適合的樣式,在此基礎上做出樣式方面的改動,變為符合我 ...


由於項目需求,需要在頁面中畫出一套蜂巢排版格式的列表。在經過網上的一番查閱資料,發現大多六邊形的畫法都是通過div覆蓋由旋轉的菱形遮掩為六邊形。這種六邊形雖然可以滿足一些基本的需求,但我暫時沒有想到如何滿足我想要的邊框動態效果。最後尋覓下發現了一款適合的樣式,在此基礎上做出樣式方面的改動,變為符合我需求的蜂巢列表。作為一個前端新手,內容如有錯誤請多指正,謝謝大家。

 

參考地址:https://wow.techbrood.com/fiddle/13533

 

這裡主要是通過 svg 的 <path> 標簽去繪製邊框路徑,然後使用 <text> 插入單行文本,使用 <foreignObject> 插入可換行文本。

1.通過svg的x y width height四個參數構建svg實際占據的面積和位置。比如

<svg x="0" y="0" height="100%" width="100%">

就是占據了外層 <li> 的全部面積,這樣做的好處是可以後續通過控制 <li> 的大小來直接控制svg的整體大小,也可以寫具體的px來定位位置。

2.通過svg的viewBox屬性來設置內部的具體尺寸比例。比如

  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
      <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
      <text class="time" x='104' y='60' style='dominant-baseline:middle;text-anchor:middle;'>
        10h
      </text>
      <foreignObject x='4' y='80' width="200" height="85">
          <p class="title">abc-activiti-server</p>
      </foreignObject>
      <text class="num" x='104' y='180' style='dominant-baseline:middle;text-anchor:middle;'>
        1個實例
      </text>
    </svg>
  </li>

這裡的viewBox=“0 0 208 240”說白了其實就是從svg的最左上(0,0)坐標的位置到最右下(208,240)坐標的,內部橫向劃分207次縱向劃分239次的一個網格狀區域。這樣當我們想要將svg的任意子組件設置位置時,只需要給與指定的x y 即可。同時 <path> 的d屬性也是通過這個網格區域來定位描點位置的。

3.通過 <path> 的d屬性來進行邊框描繪。d屬性的寫法有多種,不必糾結於某種寫法,能實現即可。比如本次實現我使用的是 d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" 即M代表開始,每一個x,y代表一個點,預設是直線連接,最後Z結尾,會自動形成閉環路徑。

4.通過 <text> 寫一些簡短的問題,其中行內樣式 dominant-baseline 設置文本垂直居中, text-anchor 設置文本水平居中。過長的文本不會換行,所以要使用下麵這個標簽。

5.通過 <foreignObject> 嵌入XHTML元素,用以防止較長的文本內容,對標簽設置了寬高後,內部元素可進行換行。

html代碼部分

<ul>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
      <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
      <text class="time" x='104' y='60' style='dominant-baseline:middle;text-anchor:middle;'>
        10h
      </text>
      <foreignObject x='4' y='80' width="200" height="85">
          <p class="title">abc-activiti-server</p>
      </foreignObject>
      <text class="num" x='104' y='180' style='dominant-baseline:middle;text-anchor:middle;'>
        1個實例
      </text>
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg class="red" x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg class="yellow" x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
  <li class="hex">
    <svg x="0" y="0" viewBox="0 0 208 240" height="100%" width="100%">
        <path class="path" d="M 104,1 205,60 205,180 104,237 3,180 3,60 Z" />
    </svg>
  </li>
<ul>
View Code

 

然後就是css部分,裡面用到的不常見樣式的具體用法做了簡單的文字描述,不太明白的可以嘗試百度一下,大多都有專門的圖文描述,這裡就不再做贅述了

    ul,p {
      margin: 0;
      padding: 0;
    }
    ul {
      list-style: none;
      border: 1px solid #333333;
      overflow: hidden;
    }
    li.hex {
      float: left;
      font-size: 20px;
    }
    svg {
      width: 100%;

      color: #3bc292;
    }
    svg path {
      fill: transparent; /* 內部透明填充色 */
      stroke: #3bc292; /* 邊框色 */
      stroke-width: 6; /* 邊框寬度 */
      stroke-linecap: round; /* 描邊端點表現形式 */
      stroke-linejoin: round; /* 描邊轉角的表現方式 */
      stroke-miterlimit: 4;
      stroke-dasharray: 20; /* 創建虛線,20px實線,20px虛線重覆 */
      animation: dash 5s linear infinite; /* 動畫名 每次速度 勻速 無限次 */

      transition: 0.3s;
      -moz-transition: 0.3s;    /* Firefox 4 */
      -webkit-transition: 0.3s;    /* Safari 和 Chrome */
      -o-transition: 0.3s;    /* Opera */
    }
    svg text {
      fill: #3bc292; /* 文本填充色 */

      transition: 0.3s;
      -moz-transition: 0.3s;    /* Firefox 4 */
      -webkit-transition: 0.3s;    /* Safari 和 Chrome */
      -o-transition: 0.3s;    /* Opera */
    }
    svg text.time {
      font-size: 0.8em;
    }
    svg p.title {
      text-align: center;
      font-size: 1.5em;

      transition: 0.3s;
      -moz-transition: 0.3s;    /* Firefox 4 */
      -webkit-transition: 0.3s;    /* Safari 和 Chrome */
      -o-transition: 0.3s;    /* Opera */
    }
    svg text.num {
      font-size: 0.8em;
    }
    svg:hover path {
      stroke: #3bc292;
      fill: #69c2a3;
    }
    svg:hover text, svg:hover p.title {
      fill: #ffffff;
      color: #ffffff;
    }
    svg.red path {
      stroke: #f56f6f;
      animation-play-state: paused;
    }
    svg.red:hover path {
      fill: #f56f6f;
    }
    svg.yellow path {
      stroke: #f5b140;
      animation-play-state: paused;
    }
    svg.yellow:hover path {
      fill: #f5b140;
    }
    @keyframes dash {
      to {
        stroke-dashoffset: 120; /* 虛線在原路徑下的偏移量 */
      }
    }

    /*** SPACING AND SIZING *****************************************************************/
    @media (min-width: 1401px) {
      .hex {
        width: 12.5%;
        margin: 0 0.5%;
      }
      .hex:nth-child(13n+1) {
        margin-left: 2.5%;
      }
      .hex:nth-child(13n+8),.hex:nth-child(13n+9),.hex:nth-child(13n+10),.hex:nth-child(13n+11),.hex:nth-child(13n+12),.hex:nth-child(13n+13) {
        margin-top: -3%;
        margin-bottom: -3%;
      }
      .hex:nth-child(13n+8) {
        margin-left: 9.25%;
      }
    }

    @media (max-width: 1400px) and (min-width: 1201px){
      .hex {
        width: 15%;
        margin: 0 0.5%;
      }
      .hex:nth-child(11n+1) {
        margin-left: 2.5%;
      }
      .hex:nth-child(11n+7),.hex:nth-child(11n+8),.hex:nth-child(11n+9),.hex:nth-child(11n+10),.hex:nth-child(11n+11) {
        margin-top: -3.6%;
        margin-bottom: -3.6%;
      }
      .hex:nth-child(11n+7) {
        margin-left: 10.5%;
      }
    }

    @media (max-width: 1200px) and (min-width: 901px){
      .hex {
        width: 19%;
        margin: 0 0.3%;
      }
      .hex:nth-child(9n+1) {
        margin-left: 1%;
      }
      .hex:nth-child(9n+6),.hex:nth-child(9n+7),.hex:nth-child(9n+8),.hex:nth-child(9n+9) {
        margin-top: -4.6%;
        margin-bottom: -4.6%;
      }
      .hex:nth-child(9n+6) {
        margin-left: 10.5%;
      }
    }

    @media (max-width: 900px) and (min-width: 601px) {
      .hex {
        width: 24%;
        margin: 0 0.3%;
      }
      .hex:nth-child(7n+1) {
        margin-left: 0.8%;
      }
      .hex:nth-child(7n+5),.hex:nth-child(7n+6),.hex:nth-child(7n+7) {
        margin-top: -6%;
        margin-bottom: -6%;
      }
      .hex:nth-child(7n+5) {
        margin-left: 13.1%;
      }
    }

    @media (max-width: 600px) {
      .hex {
        width: 30%;
        margin: 0 1%;
      }
      .hex:nth-child(5n+1) {
        margin-left: 3%;
      }
      .hex:nth-child(5n+4),.hex:nth-child(5n+5) {
        margin-top: -6.5%;
        margin-bottom: -6.5%;
      }
      .hex:nth-child(5n+4) {
        margin-left: 19%;
      }
    }
View Code

 


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

-Advertisement-
Play Games
更多相關文章
  • 現在前端工程師的工作已經不想幾年前那樣,僅僅寫一些頁面調整一下樣式就可以拿到高薪了。由於前端技術的發展,以及企業對前端工程師的要求越來越高,現在前端工作也越來越複雜。不過只要掌握目前比較流行的主流框架,完全可以輕鬆應對老闆提出的各種需求。今天就和大家聊聊前端工程師必須瞭解的主流前端框架。 已經從事前 ...
  • 以前寫的,找不到效果圖了,就不上效果圖了,直接上代碼好啦,免得自己忘了怎麼寫 .login-box { //login-box就是 需要修改樣式的 盒子 width: 200px; margin-top: 10px; height: calc(100% - 88px); overflow-y: au ...
  • 數組 1. 基本常識 數組是引用類型的一種,什麼是引用類型呢? 和原始類型不同,引用類型變數中存放的僅僅是 引用對象的記憶體地址 。 所以,當我們複製引用類型的時候,複製的僅僅是它的地址罷了。 也就是說,任意一個變數發生變化,地址上的對象也隨之變化。 舉個例子: 2. 常用屬性與方法 屬性:Array ...
  • CSS基本選擇器(對指定的標簽設置樣式,要把指定的標簽選擇出來):元素選擇器、類選擇器、id選擇器、組合選擇器、通用選擇器 1. 元素選擇器(類型選擇器或標記選擇器):聲明哪些元素採用css樣式 2. 類選擇器(class):應用樣式而不考慮具體設計的元素,為了將類選擇器的樣式與元素進行關聯,必須將 ...
  • HTML5開發工具有哪些?下麵就跟著一起來看看吧! 互聯網時代的到來,HTML5前端開發作為搭建Web網站便捷有效的編程語言,越來越受到大眾的認識和喜愛。而且,HTML5編程語言相對Java、C++等編程語言要簡單易學,吸引了很多人想要加入HTML5的學習之中。那麼,HTML5開發工具有哪些呢?1、 ...
  • Web前端必須掌握的三種技能:HTML/CSS/JavaScript。其中JavaScript相對有一定的難度,甚至部分企業專門設置的JavaScript開發工程師的崗位。今天和大家分享一下JavaScript部分基礎知識。 1、執行環境:有時也叫環境,是JavaScript中最為重要的一個概念,執 ...
  • 如何得到一個數據流中的中位數?如果從數據流中讀出奇數個數值,那麼中位數就是所有數值排序之後位於中間的數值。如果從數據流中讀出偶數個數值,那麼中位數就是所有數值排序之後中間兩個數的平均值。我們使用Insert()方法讀取數據流,使用GetMedian()方法獲取當前讀取數據的中位數。 //排序的數組 ...
  • 給定一棵二叉搜索樹,請找出其中的第k小的結點。例如, (5,3,7,2,4,6,8) 中,按結點數值大小順序第三小結點的值為4。 分析:二叉搜索樹就是每個節點X,大於其左子樹的值,小於其右子樹的值,其中序排序是遞增的。使用中序遍歷,每遍歷一個節點,k-1,直到k減到1,即為第K小的節點 /* fun ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...