1. 程式人生 > >給你的網站新增炫酷的動畫註釋

給你的網站新增炫酷的動畫註釋

## 前置 **rough-notation** 用於在網頁上建立註釋並設定註釋動畫的小型 JavaScript 庫。它還可以應用在一些常見前端框架中,比如 Vue、React、 Svelte、Angular 甚至 Web Component。我把它應用在我建立的部落格園面板中,比如你可以看見頭部導航條中的部落格暱稱有一個下劃線。下面是它可以實現的基本效果,點選按鈕試一試吧。 ## 使用 ```javascript npm install --save rough-notation ``` 通過將元素傳遞到 `annotation` 來建立物件,以及通過配置來描述樣式。擁有 `annotation` 物件後,可以呼叫`annotation.show()`顯示。 ```javascript import { annotate } from 'rough-notation'; const e = document.querySelector('#myElement'); const annotation = annotate(e, { type: 'underline' }); annotation.show(); ``` 通過 Group 建立多個動畫註釋: ```javascript import { annotate, annotationGroup } from 'rough-notation'; const a1 = annotate(document.querySelector('#e1'), { type: 'underline' }); const a2 = annotate(document.querySelector('#e3'), { type: 'box' }); const a3 = annotate(document.querySelector('#e3'), { type: 'circle' }); const ag = annotationGroup([a3, a1, a2]); ag.show(); ``` ## 引數 建立 `annotation` 時,將傳遞一個配置。配置只有一個必填欄位,即註釋的欄位。但是,您可以通過多種方式配置。type **型別** 這是一個必填欄位,設定註釋樣式。 - underline:此樣式在元素下方建立粗略下劃線。 - box:此樣式在元素周圍繪製一個框。 - corcle:此樣式在元素周圍繪製一個圓圈。 - highlight:此樣式建立高光效果,就像用熒光筆標記一樣。 - strike-through:此樣式通過元素繪製水平線。 - crossed-off:此樣式在元素上繪製一個"X"。 - bracket:此樣式圍繞元素(通常是文字段落)繪製一個括號。預設情況下,在右側,但可以配置為任何或全部的左,右,上,下。 **animate** 在註釋時開啟/關閉動畫的布林屬性。預設值為 `true`。 **animationDuration** 動畫的持續時間(以毫秒為單位)預設值為 `800ms`。 **color** 表示註釋草圖顏色的字串值,預設值為 `currentColor`。 **strokeWidth** 註釋描邊寬度。預設值為 1。 **padding** 在元素和繪製註釋的大致地點之間填充。可以將值設定為類似於 CSS 樣式填充或只是陣列。`5 top left right bottom``[top, right, bottom, left]` `[top & bottom, left & right]`。 **multiline** 僅適用於內聯文字。若要註釋多行文字,請將此屬性設定為 `true`。 **iterations** 配置迭代次數。預設情況下,註釋在兩次迭代中繪製,例如,下劃線時,從左到右繪製,然後從右到左繪製。 **brackets** 配置元素的哪一側為支架。值可以是字串或字串陣列,每個字串都是這些值之一:`top left right bottom`。繪製支架時,預設值為 `right`。 > 它還提供一些事件,供您靈活呼叫,這裡不展開描述,可以去 Github 看一看。 ## 我的使用 notation/index.js ```javascript import { annotate, annotationGroup } from 'rough-notation' import { pageName as currentPage } from '@tools' import './index.scss' const pageName = currentPage() const group = [] const annotateList = [ { page: 'all', selector: '#Header1_HeaderTitle', config: { type: 'underline', color: '#2196F3', }, }, { page: 'post', selector: '#cb_post_title_url', config: { type: 'highlight', color: '#FFF176', }, }, { page: 'post', selector: 'mark', config: { type: 'highlight', color: '#FFD54F', }, }, { page: 'post', selector: 's', config: { type: 'strike-through', color: '#FF0000', }, }, { page: 'post', selector: 'u', config: { type: 'underline', color: '#2196F3', }, }, { page: 'post', selector: 'strong', config: { type: 'circle', color: '#F44336', }, }, ] const buildGroup = items => { for (let { selector, page, config } of items) { if (page === 'all' || pageName === page) { const element = document.querySelectorAll(selector) if (!element.length) return if (element.length === 1) group.push(annotate(document.querySelector(selector), config)) if (element.length > 1) { element.forEach(function(item) { group.push(annotate(item, config)) }) } } } } const notation = (customList = annotateList) => { buildGroup(customList) const ag = annotationGroup(group) setTimeout(() => { ag.show() }, 1000) } export default notation ``` 在建立的部落格園面板使用時,只需要 import notation,可以傳入一些元素列表或者使用預設的