File size: 2,701 Bytes
40f23a9 | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <template>
<i
:class="iconClass"
:style="iconStyle"
@click="handleClick"
></i>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps({
// 图标名称(FontAwesome类名,不需要前缀)
name: {
type: String,
required: true
},
// 图标大小
size: {
type: [String, Number],
default: 'inherit'
},
// 图标颜色
color: {
type: String,
default: 'inherit'
},
// 是否可点击
clickable: {
type: Boolean,
default: false
},
// 旋转角度
rotate: {
type: [String, Number],
default: 0
},
// 是否加载中(旋转动画)
loading: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['click'])
// 图标类名
const iconClass = computed(() => {
const classes = []
// 基础类名
if (props.name.startsWith('fa-')) {
classes.push('fas', props.name)
} else {
classes.push('fas', `fa-${props.name}`)
}
// 可点击样式
if (props.clickable) {
classes.push('icon-clickable')
}
// 加载动画
if (props.loading) {
classes.push('fa-spin')
}
return classes.join(' ')
})
// 图标样式
const iconStyle = computed(() => {
const styles = {}
// 大小
if (props.size !== 'inherit') {
const sizeValue = typeof props.size === 'number' ? `${props.size}px` : props.size
styles.fontSize = sizeValue
}
// 颜色
if (props.color !== 'inherit') {
styles.color = props.color
}
// 旋转
if (props.rotate && !props.loading) {
styles.transform = `rotate(${props.rotate}deg)`
}
return styles
})
// 处理点击事件
const handleClick = (event) => {
if (props.clickable) {
emit('click', event)
}
}
</script>
<style scoped>
.icon-clickable {
cursor: pointer;
transition: var(--transition-fast);
user-select: none;
}
.icon-clickable:hover {
opacity: 0.8;
transform: scale(1.05);
}
.icon-clickable:active {
transform: scale(0.95);
}
/* 常用图标预设 */
.fa-play {
margin-left: 2px; /* 播放图标视觉居中 */
}
.fa-heart {
color: #e53e3e;
}
.fa-heart.far {
color: var(--text-secondary);
}
.fa-star {
color: #ed8936;
}
.fa-star.far {
color: var(--text-secondary);
}
/* 加载动画优化 */
.fa-spin {
animation: fa-spin 1s infinite linear;
}
@keyframes fa-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* 无障碍支持 */
.icon-clickable:focus-visible {
outline: 2px solid var(--accent-red);
outline-offset: 2px;
border-radius: 2px;
}
/* 禁用状态 */
.icon-disabled {
opacity: 0.3;
cursor: not-allowed;
pointer-events: none;
}
</style> |