Browse Source

阻止冒泡

pull/53/head
zq 2 weeks ago
parent
commit
5f1f8fa442
  1. 298
      src/components/HoverButton.vue

298
src/components/HoverButton.vue

@ -1,161 +1,169 @@
<template> <template>
<button <button
class="hover-button" class="hover-button"
:class="{ 'hover-effect': hoverEffect }" :class="{ 'hover-effect': hoverEffect }"
@mouseenter="isHovered = true" @mouseenter="isHovered = true"
@mouseleave="isHovered = false" @mouseleave="isHovered = false"
@click="$emit('click')" @click="handleClick"
> type="button"
<!-- 图片/图标部分 --> >
<div class="button-icon"> <!-- 图片/图标部分 -->
<slot name="icon"> <div class="button-icon">
<img <slot name="icon">
v-if="defaultIcon || hoverIcon" <img
:src="isHovered && hoverIcon ? hoverIcon : defaultIcon" v-if="defaultIcon || hoverIcon"
:alt="iconAlt" :src="isHovered && hoverIcon ? hoverIcon : defaultIcon"
/> :alt="iconAlt"
<span v-else class="default-icon-placeholder"></span> />
</slot> <span v-else class="default-icon-placeholder"></span>
</div> </slot>
</div>
<!-- 文字部分 --> <!-- 文字部分 -->
<span <span
class="button-text" class="button-text"
:style="{ :style="{
color: isHovered && hoverTextColor ? hoverTextColor : defaultTextColor color: isHovered && hoverTextColor ? hoverTextColor : defaultTextColor
}" }"
> >
<slot>{{ buttonText }}</slot> <slot>{{ buttonText }}</slot>
</span> </span>
</button> </button>
</template> </template>
<script> <script>
export default { export default {
name: 'HoverButton', name: 'HoverButton',
props: { props: {
// //
buttonText: { buttonText: {
type: String, type: String,
default: '按钮' default: '按钮'
}, },
// //
defaultIcon: { defaultIcon: {
type: String, type: String,
default: '' default: ''
}, },
// //
hoverIcon: { hoverIcon: {
type: String, type: String,
default: '' default: ''
}, },
// alt // alt
iconAlt: { iconAlt: {
type: String, type: String,
default: '按钮图标' default: '按钮图标'
},
//
defaultTextColor: {
type: String,
default: '#333333'
},
//
hoverTextColor: {
type: String,
default: '#007bff'
},
//
hoverEffect: {
type: Boolean,
default: true
},
// (primary, danger, success)
type: {
type: String,
default: 'default',
validator: value => ['default', 'primary', 'danger', 'success', 'warning'].includes(value)
}
}, },
data() { //
return { defaultTextColor: {
isHovered: false type: String,
} default: '#333333'
},
//
hoverTextColor: {
type: String,
default: '#007bff'
},
//
hoverEffect: {
type: Boolean,
default: true
},
// (primary, danger, success)
type: {
type: String,
default: 'default',
validator: value => ['default', 'primary', 'danger', 'success', 'warning'].includes(value)
}
},
data() {
return {
isHovered: false
}
},
methods:{
handleClick(e) {
e.preventDefault();
e.stopPropagation();
this.$emit('click', e);
} }
} }
</script> }
</script>
<style scoped> <style scoped>
.hover-button { .hover-button {
font-family: Microsoft YaHei UI; font-family: Microsoft YaHei UI;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
padding: 8px 16px; padding: 8px 16px;
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 4px; border-radius: 4px;
background-color: white; background-color: white;
cursor: pointer; cursor: pointer;
transition: all 0.3s ease; transition: all 0.3s ease;
font-size: 14px; font-size: 14px;
outline: none; outline: none;
background: #F2F3F5; background: #F2F3F5;
border: 1px solid #BABDC2; border: 1px solid #BABDC2;
} }
.hover-button.hover-effect:hover { .hover-button.hover-effect:hover {
background: #F2F7FF; background: #F2F7FF;
border-color: #006AFF; border-color: #006AFF;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
} }
/* 按钮类型样式 */ /* 按钮类型样式 */
.hover-button.primary { .hover-button.primary {
background-color: #007bff; background-color: #007bff;
border-color: #007bff; border-color: #007bff;
color: white; color: white;
} }
.hover-button.danger { .hover-button.danger {
background-color: #dc3545; background-color: #dc3545;
border-color: #dc3545; border-color: #dc3545;
color: white; color: white;
} }
.hover-button.success { .hover-button.success {
background-color: #28a745; background-color: #28a745;
border-color: #28a745; border-color: #28a745;
color: white; color: white;
} }
.hover-button.warning { .hover-button.warning {
background-color: #ffc107; background-color: #ffc107;
border-color: #ffc107; border-color: #ffc107;
color: #212529; color: #212529;
} }
/* 图标样式 */ /* 图标样式 */
.button-icon { .button-icon {
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
margin-right: 8px; margin-right: 8px;
} }
.button-icon img { .button-icon img {
width: 20px; width: 20px;
height: 20px; height: 20px;
transition: opacity 0.3s ease; transition: opacity 0.3s ease;
} }
.default-icon-placeholder { .default-icon-placeholder {
display: inline-block; display: inline-block;
width: 20px; width: 20px;
height: 20px; height: 20px;
background-color: #ddd; background-color: #ddd;
border-radius: 50%; border-radius: 50%;
} }
/* 文字样式 */ /* 文字样式 */
.button-text { .button-text {
transition: color 0.3s ease; transition: color 0.3s ease;
} }
</style> </style>
Loading…
Cancel
Save