I'm making something like a Twitter like button.
I want to change the icon button color.
When clicking the button, changing from gray to red was successful.
But I don't know how to change it from red to gray.
I am using javascript and vue.
<template>
<div id="postbox">
<div class="thumbnail-img" v-bind:style="{ backgroundImage: 'url(' postItem.img ')'}"></div>
<div>
<h4>{{postItem.title}}</h4>
<p>{{ cutDescript }}</p>
<div class="text-date">{{postItem.date}}</div>
<hr>
<div class="footer">
<img class="profile-img" :src="postItem.profileImg"/>
<span class="writer">by <span class="bold">{{postItem.writer}}</span></span>
<b-icon icon="heart-fill" class="gap_margin_5px_horizontal"
:variant="currentMode == 'grid' ? 'danger' : ''"
v-on:click="greet('grid')"
/>
<span class="good_num">{{postItem.good}}</span>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'postbox',
props: {
post: {
type: Object,
default: function () {
return {
title: 'Undefined',
descript: 'This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.This is description.',
date: '----년 --월 --일',
profileImg: '../assets/logo.png',
writer: 'unknown',
good: 0,
img: '../assets/logo.png'
}
}
}
},
data () {
return {
postItem: this.post,
currentMode: this.mode
}
},
computed: {
cutDescript: function () {
if (this.postItem && this.postItem.descript && this.postItem.descript.length >= 200) {
return `${this.postItem.descript.slice(0, 197)}...`
} else if (this.postItem && !this.postItem.descript) {
return '본문이 비어있습니다.'
}
return this.postItem.descript
}
},
methods: {
greet: function (mode) {
if (mode !== 'grid' && mode !== 'map') {
mode = 'grid'
}
this.currentMode = mode
this.$emit('current-mode', this.currentMode)
}
}
}
</script>
How do I make the button switch between gray and red?
CodePudding user response:
In this case, you should use a boolean data instead, to switch like button state's between red and grey
<b-icon icon="heart-fill" class="gap_margin_5px_horizontal"
:variant="isLiked ? 'danger' : ''"
@click="isLiked = !isLiked"
/>
data () {
return {
isLiked: false
}
}