I've two buttons:
1 - Follow
2 - Unfollow
At first glance the Follow button will be displayed and then when you hover over the Follow button, then I want to make the follow button to disappear and Unfollow button to appear, then after once hovered over the Unfollow button then I want the Follow button to appear and Unfollow button to dispear, so I how do I do it?
Feed backs are welcomed.
<div class="btn-follow">Follow</div>
<div class="btn-unfollow">Unfollow</div>
.btn-follow {
color: #FFF;
background-color: #38B7EA;
padding: 5px 0;
width: 100px;
margin: 0 auto;
border-radius: 20px;
}
.btn-unfollow {
color: #FFF;
background-color: #A5BECB;
padding: 5px 0;
width: 100px;
margin: 0 auto;
border-radius: 20px;
}
Hoverable Buttons Use the :hover selector to change the style of a button when you move the mouse over it.
A tgl() method is utilized to toggle a button in JavaScript. In this method, you extract the HTML element by employing the getElementById property, and then the if else-if statement is added to it. If the “value==ON”, toggle the value to “OFF”. If the value is OFF, then the value will be toggled to “ON”.
An alternative and more optimal solution as I said which can be done for same button as below:
DEMO HERE
HTML
<button id="followUnFollow" class="followUnF follow">Follow</button>
JS
$(document).ready(function(){
$('#followUnFollow').on('click',function()
{
if($(this).hasClass('follow'))
$(this).removeClass('follow').addClass('unfollow').text('Unfollow');
else
$(this).removeClass('unfollow').addClass('follow').text('Follow');
});
});
UPDATE
If you want the same on hover
instead of click you can change .on('click'
to hover
as below:
$('#followUnFollow').hover(function(){
......
});
Try following code:
It will hide the click
ed button and show other button.
$('.btn-follow').on('click', function () {
$(this).hide();
$('.btn-unfollow').show();
});
$('.btn-unfollow').on('click', function () {
$(this).hide();
$('.btn-follow').show();
});
Demo: https://jsfiddle.net/tusharj/Ls8gk2es/
I think using hover event you can make it simpler.
HTML
<button class="btn btn-follow">Follow</button>
<button class="btn btn-unfollow">Unfollow</button>
JS
$('.btn-unfollow').hide();
$('.btn').hover(function () {
$('.btn').toggle();
});
CSS (same as the styling given in your question)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With