I would use the addClass()
/removeClass()
approach on this one.
First, define the 'default' class and an 'active' class:
.main-nav-button {
border: 1px solid red;
border-color: red blue green yellow;
}
.main-nav-button.active {
border-color: green yellow red blue;
}
Then you add this class on mousedown()
, and remove it on mouseup()
. Note that I also added mouseout()
. It will remove the class when you leave the div with your mouse (also when mousedown is active).
$(function(){
$(".main-nav-button")
.mouseup(function() { $(this).removeClass('active'); })
.mouseout(function() { $(this).removeClass('active'); })
.mousedown(function() { $(this).addClass('active'); });
});
DEMO
$(function(){
$(".main-nav-button")
.mouseup(function() { $(this).removeClass('active'); })
.mouseout(function() { $(this).removeClass('active'); })
.mousedown(function() { $(this).addClass('active'); });
});
.main-nav-button {
float: left;
padding: 1em;
border: 5px solid red;
border-color: red blue green yellow;
}
.main-nav-button.active {
border-color: green yellow red blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-nav-button">Button</div>
EDIT
Based on your comment, to make it dynamicly. The only thing you need to check when the cursor leaves is if the mousedown event is triggered. To do so, I've added an active class to it.
$(function () {
function toggle(elem) {
var top = elem.css("borderTopColor"),
right = elem.css("borderRightColor"),
bottom = elem.css("borderBottomColor"),
left = elem.css("borderLeftColor");
elem.css("borderTopColor", bottom);
elem.css("borderLeftColor", right);
elem.css("borderBottomColor", top);
elem.css("borderRightColor", left);
}
$(".main-nav-button")
.mousedown(function () {
toggle($(this));
$(this).addClass('active');
})
.mouseup(function () {
toggle($(this));
$(this).removeClass('active');
})
.mouseout(function () {
if( $(this).hasClass('active') ) {
toggle($(this));
$(this).removeClass('active');
}
});
});
$(function () {
function toggle(elem) {
var top = elem.css("borderTopColor"),
right = elem.css("borderRightColor"),
bottom = elem.css("borderBottomColor"),
left = elem.css("borderLeftColor");
elem.css("borderTopColor", bottom);
elem.css("borderLeftColor", right);
elem.css("borderBottomColor", top);
elem.css("borderRightColor", left);
}
$(".main-nav-button")
.mousedown(function () {
toggle($(this));
$(this).addClass('active');
})
.mouseup(function () {
toggle($(this));
$(this).removeClass('active');
})
.mouseout(function () {
if( $(this).hasClass('active') ) {
toggle($(this));
$(this).removeClass('active');
}
});
});
.main-nav-button {
float: left;
padding: 1em;
margin: 1em;
border: 5px solid red;
border-color: red blue green yellow;
}
.main-nav-button.nr2 {
border-color: purple orange cyan black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-nav-button">Button</div>
<div class="main-nav-button nr2">Button</div>