Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
點贊 + 關注 + 收藏=學會了
技術一直在演變,在網頁中使用 SVG 的方法也層出不窮。每個時期都有對應的最優解。
所以我打算把我知道的 7種 SVG 的使用方法列舉出來,有備無患~
如果你還知道其他方法,可以在評論區補充~
<?xml version="1.0" ?>
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
<title>雷猴</title>
<circle cx="50" cy="50" r="50" fill="hotpink"></circle>
</svg>
xml 是瀏覽器能讀取的格式,但如果希望 svg 能在瀏覽器中渲染出來,需要使用 xmlns 聲明渲染規則。
所以必須使用 xmlns="http://www.w3.org/2000/svg"。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>svg demo</title>
</head>
<body>
<div>
<!-- 內嵌到 HTML 中 -->
<svg width="100%" height="100%" version="1.1">
<circle cx="50" cy="50" r="50" fill="hotpink"></circle>
</svg>
</div>
</body>
</html>
可以看到上面的代碼中,<svg> 標簽并沒有使用 xmlns="http://www.w3.org/2000/svg" 聲明命名空間,這是因為 HTML 5 文檔使用 <!DOCTYPE html> 標記,它允許跳過 SVG 命名空間聲明,HTML 解析器會自動識別 SVG 元素和它的子元素,除了 <foreignObject> 元素的子元素。
在寫本文時,將 SVG 內嵌到 HTML 中 的做法是最常見的,也是比較推薦的方式之一。
做特效時,這種使用方式也是比較輕松的。
<style>
.svg_bg_img {
width: 100px;
height: 100px;
background: url('./case1.svg') no-repeat;
background-size: 100px 100px;
}
</style>
<div class="svg_bg_img"></div>
SVG 也是一種圖片格式,所以按理說是能當做背景圖來使用的。
一試,果然可以~
<img src="./case1.svg" width="100" height="100">
既然 SVG 可以在 CSS 中當背景圖使用,那也可以在 <img> 標簽里使用咯~
<iframe
src="./case1.svg"
width="100"
height="100"
></iframe>
iframe 可以在網頁里再嵌套一個網頁,既然 SVG 可以直接在瀏覽器打開,那使用 <iframe> 引用 SVG 同樣也是可以的。
需要注意的是,<iframe> 默認是有個邊框樣式的,如果你使用這種方式引入 SVG 可能還需要自己手動調節一下樣式。
<embed
src="./case1.svg"
width="100"
height="100"
/>
<embed> 標簽定義了一個容器,用來嵌入外部應用或者互動程序。它也可以插入各種媒體資源。
<embed> 標簽已經被標準采用了。但它不能包含任何子內容,因此如果嵌入失敗就沒有備用選項了。所以現階段來看,我不太推薦使用 embed 的方式引入 SVG 。
<object
data="./case1.svg"
type="image/svg+xml"
codebase="http://www.adobe.com/svg/viewer/install"
></object>
<object> 是通過 data 屬性引入資源的。它可以用于嵌入圖像,類似 <img> ,也可以用于嵌入獨立的 HTML 文檔,類似 <iframe> 。
使用 <object> 嵌入 SVG 可以讓那些不能直接顯示 SVG 但又有 SVG 插件的老舊瀏覽器展示 SVG。
需要注意的是,在某些現代瀏覽器中,type 和 codebase 是可以不寫的。
type 用來聲明當前引入的資源是屬于什么類型。
在寫本時,我推薦使用 內嵌到 HTML 的方式來做日常開發。
其他方式按照你實際需求去使用即可。
最后的 embed 和 object 這兩種方式可以當做備用方案去使用。
?雷猴 SVG
《Canvas 從入門到勸朋友放棄(圖解版)》
《Fabric.js 從入門到膨脹》
《『Three.js』起飛!》
《console.log也能插圖!!!》
《純css實現117個Loading效果》
《視差特效的原理和實現方法》
《這18個網站能讓你的頁面背景炫酷起來》
繪圖 Drawing Shapes
SVG stands for Scalable Vector Graphics, and is used to draw shapes with HTML-style markup.
It offers several methods for drawing paths, boxes, circles, text, and graphic images.
SVG is not pixel-based, so it can be magnified infinitely with no loss of quality.
SVG代表可伸縮矢量圖形,用于繪制具有HTML風格標記的形狀。
它提供了幾種繪制路徑,框,圓,文本和圖形圖像的方法。
SVG不是基于像素的,所以它可以無限放大,沒有質量損失。
2 插入SVG圖像 Inserting SVG Images
An SVG image can be added to HTML code with just a basic image tag that includes a source attribute pointing to the image:
只需一個基本的圖像標簽
3 繪制
To draw shapes with SVG, you first need to create an SVGelement tag with two attributes: width and height.
要使用SVG繪制形狀,您首先需要創建一個SVG元素標簽,其中包含兩個屬性:width和height。
To create a circle, add a <circle> tag:
- cxpushes the center of the circle further to the right of the screen- cypushes the center of the circle further down from the top of the screen- r defines the radius- filldetermines the color of our circle- strokeadds an outline to the circle
天給大家推薦一款非常值得一用的開源矢量圖標庫CssGG。
css.gg 又一款供設計和前端開發的工具圖標庫,star高達6.5K+。擁有超過700+個矢量圖標,可被用于.css|.svg|.tsx|.fig|.xd|.json|.xml等多種格式。
作為前端開發,平時需要使用圖標都會去阿里iconfont圖標庫查找。
今天就多了一種新的選擇css.gg。只要是優秀的,我們就有理由去使用它。
$ npm i css.gg -S
包含以下目錄及文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- You can add this link or any other CDN alternatives as mentioned above -->
<link href='https://css.gg/css' rel='stylesheet'>
</head>
<body>
<!-- Using i tag -->
<i class="gg- {ICONNAME} "></i>
<!-- Using div tag -->
<div class="gg- {ICONNAME} "></div>
<!-- Using custom tag -->
<gg-icon class="gg- {ICONNAME} "></gg-icon>
</body>
</html>
另外還可 CSS @import引入,SVG、JSON、XML、React等其它方式。
純CSS、SVG、Figma UI Icons 可用在 SVG雪碧圖,styled-components等。
so perfect,非常不錯的一款開源圖標庫。
# 官網地址
https://css.gg/
# 倉庫地址
https://github.com/astrit/css.gg
ok,今天就介紹到這里。如果小伙伴們有興趣的話,可以去試一試哈!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。