- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 vue-fontawesome 和 font-awesome-icon。这非常适合像这样的“独立”图标:
<font-awesome-icon :icon="icon" size="1x" /> .
但是我怎样才能为 <input type="checkbox">
使用 fontawesome 复选标记呢?在vue组件方式?
最佳答案
有一种方法可以查找 css 伪元素:
FontAwesomeConfig = { searchPseudoElements: true };
不推荐使用此方法,因为它会检查现有标记并通过 CSS 样式为伪元素添加内联 SVG,这很慢。
我不鼓励这种方法,所以我不会解释它是如何工作的,如果您有兴趣可以阅读更多相关信息 here .
不使用伪元素,而是为这些复选框创建一个组件。
我们将创建一个名为 awesome-checkbox
的单文件组件。
AwesomeCheckbox.vue
<template>
<div :class="{'awesome-checkbox': true, [wrapperClassName]: !!wrapperClassName}"
:style="{ color: isChecked ? checkedColor : uncheckedColor }">
<input :id="id"
:name="name"
type="checkbox"
class="awesome-checkbox__input"
v-model="checkboxModel">
<label :for="id"
:style="{ cursor }"
class="awesome-checkbox__label"
@click="toggleCheck">
<font-awesome-icon :icon="isChecked ? checkedIcon : uncheckedIcon"
:size="size" />
</label>
</div>
</template>
<script>
import FontAwesomeIcon from '@fortawesome/vue-fontawesome';
import { faSquare, faCheckSquare } from '@fortawesome/fontawesome-free-solid';
/**
* Custom HTML <input> checkbox element using Font-Awesome-Icon 5 icons for visual effect.
*/
export default {
name: 'awesome-checkbox',
components: { FontAwesomeIcon },
props: {
/**
* A wrapper class other than default that provides extra manipulation from parent component.
*/
wrapperClassName: {
type: String,
default: null,
},
/**
* The name attribute for the checkbox input.
*/
name: {
type: String,
default: null,
},
/**
* The id attribute for the checkbox input.
*/
id: {
type: String,
default: null,
required: true,
},
/**
* The model directive value to create two-way data binding.
*/
model: {
type: Boolean,
default: null,
required: true,
},
/**
* The mouse cursor to display when the mouse pointer is over the Font-Awesome-Icon 5 element.
* Accepts any cursor CSS property value.
*/
cursor: {
type: String,
default: 'pointer',
},
/**
* The Font-Awesome-Icon 5 imported icon object used for the unchecked state.
*/
uncheckedIcon: {
type: Object,
default: () => faSquare,
},
/**
* The Font-Awesome-Icon 5 imported icon object used for the checked state.
*/
checkedIcon: {
type: Object,
default: () => faCheckSquare,
},
/**
* The Font-Awesome-Icon 5 icon size.
*/
size: {
type: String,
default: '1x',
},
/**
* The Font-Awesome-Icon 5 icon color used for the unchecked state.
*/
uncheckedColor: {
type: String,
default: 'inherit',
},
/**
* The Font-Awesome-Icon 5 icon color used for the checked state.
*/
checkedColor: {
type: String,
default: 'inherit',
},
},
data() {
return {
isChecked: !!this.model,
checkboxModel: this.model,
};
},
methods: {
emitModelValueUpdate() {
/**
* Update event.
*
* @event update
* @type {boolean}
*/
this.$emit('update:model', this.isChecked);
},
/**
* Gets called when the user clicks on the label element.
*/
toggleCheck() {
this.isChecked = !this.isChecked;
this.emitModelValueUpdate();
},
},
};
</script>
<style lang="scss" scoped>
.awesome-checkbox {
display: inline-flex;
&__label {
font-size: 1em; // Change Font-Awesome-Icon 5 icon size with css instead of predefined Font-Awesome-Icon 5 size prop.
}
&__input {
display: none; // Hide the HTML <input> element.
}
}
</style>
然后像这样在父组件中使用它:
<template>
<div>
<awesome-checkbox :model.sync="acceptTerms"
checkedColor="#41B883"
uncheckedColor="#E0EAF1"
cursor="pointer"
size="1x"
id="my-awesome-checkbox"
name="acceptTerms"
:checkedIcon="faCheckSquare"
:uncheckedIcon="faSquare" />
</div>
</template>
<script>
import { faSquare, faCheckSquare } from '@fortawesome/fontawesome-free-solid';
import AwesomeCheckbox from '@/components/path/to/AwesomeCheckbox';
export default {
name: 'parent-component',
components: { AwesomeCheckbox },
data() {
return {
acceptTerms: false,
faSquare,
faCheckSquare,
};
},
};
</script>
<style lang="scss" scoped>
/* ... */
</style>
您可以根据需要扩展此组件,例如让 model
属性接受不止一种类型,例如数组而不是 bool 值。
我只是针对你的问题制作了这个组件,并没有完全测试它,请谨慎使用。
关于vue.js - 我可以将 vue-fontawesome 用于复选框复选标记吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768688/
我正在使用 vue.js 2 - 我在我的 App.vue 中添加了一些 mdi 图标,这会在我的项目中生成一个新错误 当我尝试为我的项目提供服务时,但出现如下错误 我还应用了一些命令,但它没有解决我
我们正在启动一个 Angular 6 元素并希望使用 Font Awesome 5.1。 Font Awesome 提供了两个 Node 包: @fortawesome/angular-fontawe
我有一个自定义 SVG 图标,我想将其添加到我的本地 fontawesome 库 (fontawesome-all.js),我根据此处提供的说明将其包含在我的项目资源文件夹中: https://fon
目前我有这个可以展开和折叠的 Accordion :https://jsfiddle.net/cliffeee/a5uxgkmt/15/ . 我想使用 fontawesome 为 Accordion
我正在尝试使用 Font Awesome 5.1.0 在 webpack 4. webpack配置文件的内容: const HtmlWebPackPlugin = require("html-webp
我正在使用 vue-cli。我对 vue 很陌生。我正在尝试更改选择标签右上角的箭头符号。我该怎么做?我尝试寻找解决方案,发现我需要使用 css。但是我们如何在 css 中使用 font-awesom
我在我的 asp.net mvc 项目中使用 fontawesome 4.5。我在控制台上遇到以下错误 我已经在我的字体文件夹中包含了字体 并在layout.cshtml中为fontwesome样式表
我正在尝试使用 HTML5 和 FontAwesome 制作网页。我的问题是:如果我包含一个 FontAwesome 图标,它会出现两次,并且两个副本会相互堆叠(参见此处:image) 我更改了一些
我在页面上的几个地方使用了 Font Awesome 图标,但是当我想在 CSS 中使用它时,它没有显示。我在这里遇到了很多问题,因为我看到其他人也有同样的问题,但没有一个解决方案对我有用: 我的 F
在 Font Awesome 我如何使用此代码:仅在鼠标悬停上工作? 最佳答案 除了覆盖类之外,您还可以创建另一个仅用于悬停的类: .fa-spin-hover:hover { -webkit-a
我用这个 post 来动态加载一些组件 import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; const te
我有一个关于 React 的项目,我需要将 FontAwesome 集成到其中。我找到官方library并按照自述文件中的说明安装它 $ npm i --save @fortawesome/fonta
我的任务是编写一个函数,从 JSON 链接获取比特币交易,使用按钮更改间隔,添加漂亮的向上/向下/线条箭头,具体取决于汇率是否上升/下降/没有变化,并将其显示在网站上。 我什么都有,除了fontawe
我想从 CDN 加载 Fontawesome,但我得到了 跨域请求失败。资源访问受到限制 我知道要解决此问题,应该添加 HTTP header ,但是有什么方法可以避免这样做吗? 我尝试更换'../f
Fontawesome 现在已升级到第 5 版,但我们的许多应用程序仍处于旧版本。我怎样才能找到带有他们的旧版本图标的 content (CSS 属性)列表? 最佳答案 进入图标页面。然后单击左上 A
我想从 CDN 加载 Fontawesome,但我得到了 跨源请求失败。资源访问受限 我知道要解决这个问题,应该添加一个 HTTP header ,但是有什么办法可以避免这样做吗? 我试过更换'../
我正在尝试在 OS X 上的 XeLaTex 中使用 FontAwesome。所以我按照此github中描述的步骤进行操作: 将其保存到您的项目文件夹中,命名为 fontawesome.sty 将\u
这个问题在这里已经有了答案: jquery .text doesn't render HTML elements into the DOM (3 个答案) 关闭 4 年前。 jquery 3.3.1
我正在尝试手动堆叠 Soundcloud 图标并将其显示在正方形上,就像 fa-facebook-square 一样,但我的 Soundcloud 图标是在广场下方创建的。 到目前为止,这是我的代码:
很长一段时间以来,一些 FontAwesome(最新版本)图标在我的元素中不起作用,例如 fa-hashtag,但还有一些其他图标也不起作用。已经在堆栈中检查了一些解决方案,但没有任何效果。 这里是如
我是一名优秀的程序员,十分优秀!