当前位置: 代码网 > it编程>编程语言>Javascript > 前端iconfont两种使用方式举例详解

前端iconfont两种使用方式举例详解

2025年02月13日 Javascript 我要评论
iconfont 是一种非常流行的网页图标解决方案,它将图标字体化,使得图标可以像文字一样方便地使用和样式化。步骤 1:创建项目并添加图标1.创建项目:在 iconfont 网站上,点击“

iconfont 是一种非常流行的网页图标解决方案,它将图标字体化,使得图标可以像文字一样方便地使用和样式化。

步骤 1:创建项目并添加图标

1.创建项目

  • 在 iconfont 网站上,点击“我的项目”。
  • 点击“新建项目”,为你的项目命名并创建。

2.添加图标

  • 在 iconfont 网站上搜索你需要的图标。
  • 将你需要的图标添加到购物车。
  • 进入购物车,选择“加入项目”,选择你刚刚创建的项目

步骤 2:下载项目代码

  • 下载代码
  • 进入“我的项目”页面,选择你创建的项目。
  • 点击“下载代码”,选择“font class”下载格式(推荐)。
  • 下载并解压文件,你会得到一个包含 css 文件和字体文件的压缩包。

步骤 3:在项目中使用 iconfont

  • 引入 css 文件
  • 将下载的 css 文件(通常名为 iconfont.css)和字体文件(通常在 font 文件夹中)放入你的项目目录中。
  • 在你的 html 文件中引入 iconfont.css 文件。
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <link rel="stylesheet" href="path/to/iconfont.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
</head>
<body>
    <!-- 图标示例 -->
    <i class="iconfont icon-example"></i>
</body>
</html>

2.使用图标

  • 在 html 文件中使用 <i> 标签并添加相应的类名来显示图标。
  • 类名格式通常为 iconfont icon-图标名称
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <link rel="stylesheet" href="path/to/iconfont.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
    <style>
        .icon {
            font-size: 24px; /* 设置图标大小 */
            color: #333;    /* 设置图标颜色 */
        }
    </style>
</head>
<body>
    <!-- 使用图标 -->
    <i class="iconfont icon-example icon"></i>
    <i class="iconfont icon-user icon"></i>
    <i class="iconfont icon-settings icon"></i>
</body>
</html>

步骤 4:自定义图标样式

  • 更改图标大小和颜色
  • 通过 css 可以轻松更改图标的大小和颜色。
.icon {
    font-size: 32px; /* 图标大小 */
    color: #ff6600;  /* 图标颜色 */
}

2.悬停效果

  • 你可以添加悬停效果来增强用户体验。
.icon:hover {
    color: #ff0000; /* 悬停时的颜色 */
}

css使用整体示例

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <link rel="stylesheet" href="path/to/iconfont.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
    <style>
        .icon {
            font-size: 32px; /* 图标大小 */
            color: #333;    /* 图标颜色 */
            margin: 10px;
        }
        .icon:hover {
            color: #ff0000; /* 悬停时的颜色 */
        }
    </style>
</head>
<body>
    <!-- 使用图标 -->
    <i class="iconfont icon-example icon"></i>
    <i class="iconfont icon-user icon"></i>
    <i class="iconfont icon-settings icon"></i>
</body>
</html>

下面是iconfont.js ,也就是js的使用方式

添加图标步骤同上

步骤 1:获取 iconfont.js 链接

  • 进入项目页面:
  • 在“我的项目”页面,选择你创建的项目。

2.获取 iconfont.js 链接

步骤 2:在项目中使用 iconfont.js

  • 引入iconfont.js文件:
  • 在你的 html 文件中引入 iconfont.js 文件。
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <script src="path/to/iconfont.js"></script>
</head>
<body>
    <!-- 图标示例 -->
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-example" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
</body>
</html>

2.使用图标

  • 在 html 文件中使用 <svg> 标签和 <use> 标签来引用图标。
  • xlink:href 的值格式为 #icon-图标名称
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <script src="https://at.alicdn.com/t/font_1234567_abcd1234.js"></script> <!-- 替换为你的 iconfont.js 链接 -->
    <style>
        .icon {
            width: 32px;  /* 图标宽度 */
            height: 32px; /* 图标高度 */
            fill: #333;   /* 图标颜色 */
            margin: 10px;
        }
        .icon:hover {
            fill: #ff0000; /* 悬停时的颜色 */
        }
    </style>
</head>
<body>
    <!-- 使用图标 -->
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-example" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-user" rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-settings" rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
</body>
</html>

步骤 3:自定义图标样式

  • 更改图标大小和颜色
  • 通过 css 可以轻松更改图标的大小和颜色。
.icon {
    width: 48px;  /* 图标宽度 */
    height: 48px; /* 图标高度 */
    fill: #ff6600;  /* 图标颜色 */
}

2.悬停效果

  • 你可以添加悬停效果来增强用户体验。
.icon:hover {
    fill: #ff0000; /* 悬停时的颜色 */
}

js使用方式完整示例

通过上述步骤,你可以在前端项目中轻松使用 iconfont.js 图标。以下是完整的示例代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iconfont example</title>
    <script src="https://at.alicdn.com/t/font_1234567_abcd1234.js"></script> <!-- 替换为你的 iconfont.js 链接 -->
    <style>
        .icon {
            width: 48px;  /* 图标宽度 */
            height: 48px; /* 图标高度 */
            fill: #333;   /* 图标颜色 */
            margin: 10px;
        }
        .icon:hover {
            fill: #ff0000; /* 悬停时的颜色 */
        }
    </style>
</head>
<body>
    <!-- 使用图标 -->
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-example" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-user" rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
    <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-settings" rel="external nofollow"  rel="external nofollow" ></use>
    </svg>
</body>
</html>

总结 

到此这篇关于前端iconfont两种使用方式的文章就介绍到这了,更多相关前端iconfont两种方式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com