博客一直缺一个 favicon
,在标签栏里和其他网页放在一起,就显得很丑,一看就是半成品网页。
想放一个标新立异些的 icon,问了 ChatGPT 也没有什么好的建议,就想放一张动图。
可是现代浏览器里 favicon
如果是 gif
格式,通常只会展示它的第一帧,并不能动起来。
于是想到了这个 favicon-pong 项目:在 favicon 里打乒乓球,网页里上下滚动,favicon 里球拍和球就会做出变化。
link 标签
要实现 favicon
,可以用一个 rel
为 icon
的 link
标签。不过不仅是 icon
,rel
也可以是其他值,这个后边再说。
1 2 3 4 5
| <head> <link rel="icon" type="image/png" /> </head>
|
动态修改 href
在 link
上通过 href
设置图片的路径。
1 2
| const link = document.querySelector("head > link[rel*='icon']"); link.setAttribute("href", "some_path");
|
然后找一张 gif,用预览工具拆分每一帧,转成 base64,写入 href
就好了。
这样做也有缺点,图片数据是打包到代码里的,会增加包体积。可以尝试用 canvas 或者 svg 去动态的画图片,然后转成 data url 来解决,也更灵活。favicon-pong 就是手动画 svg 的。
link[rel*='icon']
前文说到 rel 不仅是 icon
,所以选择器里用了 *=
的写法,星号表示通配符,只要含有 "icon"
的都选中。
rel="shortcut icon"
已经被废弃的非标准方法,老的网站可能还在用。
rel="mask-icon"
Safari 用于 svg 格式 favicon
,除了 Safari,其他浏览器直接用 "icon"
应该就支持 svg 了。文档
rel="apple-touch-icon"
在 iOS 把网页添加到主屏幕(称为 Web Clip,不是“小程序” App Clips,哈哈),用到的 icon。文档
使用 apple-touch-icon
在 iPhone 主屏幕添加网站 icon 的例子:
1 2 3
| <head> <link rel="apple-touch-icon" sizes="180x180" href="https://p8.itc.cn/images01/20220406/bf5efc87481c47bca041ed20d64e4a01.jpeg"> </head>
|
用 Safari 添加到主屏幕:
最终效果
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <head> <link rel="icon" type="image/png" /> <script> const go = async () => { const gif = { gifList: [ `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADgAAAAkCAY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sPuqJgROIukfxBnUXY8W/wgxHMZSX+TNz/BNmcOLOAhbpd4fBRuj2wwadmApBPI007ULDanpJPIg910Biomj63abiwvHlYtLubujN7Bee6cs8n8k7j1svEkvetZPED5XP8vcgZJJ/w33MuOQuZ4U5lX3Ey3N9/xvHiYLXkWGN84ZI2NxE1H5uRZoLdN7c17uDXLnDwLdN10+9aedCv9zBzviPPKExzHHVyAxY5G0mv6Kfn78u3WPrg7qpv4/UlSt5wKvxJ0nYuPuzOqu7iuhQJ+YCZCriFeoFdA3cUb1FmwMC/ZeLwriRd4Cw2XRwgFOjcr3qm3ljy7aNYshjsY4155WvGwseKXui4z3rZVY8PQqImiP4tHrWv/oCm4AAAAAElFTkSuQmCC`, ], index: -1, next() { this.index += 1; if (this.index > this.gifList.length - 1) { this.index = 0; } return this.gifList[this.index]; }, };
setInterval(() => { const link = document.querySelector("head > link[rel*='icon']"); const url = gif.next(); link.setAttribute("href", url); }, 500); }; go(); </script> </head>
|
参考资料
张鑫旭
How to Favicon in 2021