Reputation: 4657
this is my HTML
<div id="c">
<div class="base">
<div class="cb out" id="red" data-color="Red">
</div>
</div>
<div class="base">
<div class="cb out" id="green" data-color="Green">
</div>
</div>
<div class="base">
<div class="cb out" id="blue" data-color="Blue">
</div>
</div>
</div>
I want to remove out
class and add in
class using jquery-ui with effect. This is the code:
//focuse mouseower
function fmo(element) {
var $element = $(element);
$element.removeClass("out");
$element.addClass("in",300);
}
//blur mouseout
function bmo(element) {
var $element = $(element);
$element.removeClass("in");
$element.addClass("out",300);
}
function ready() {
$(".cb").mouseover(function () { fmo(this); });
$(".cb").mouseout(function () { bmo(this); })
$(".cb").focus(function () { fmo(this); });
$(".cb").blur(function () { bmo(this); });
}
$(function () { ready(); });
the code above doesn't work but if I remove jquery-ui reference and just use jquery to do the job with this code:
//focuse mouseower
function fmo(element) {
var $element = $(element);
$element.removeClass("out");
$element.addClass("in");
}
//blur mouseout
function bmo(element) {
var $element = $(element);
$element.removeClass("in");
$element.addClass("out");
}
function ready() {
$(".cb").mouseover(function () { fmo(this); });
$(".cb").mouseout(function () { bmo(this); })
$(".cb").focus(function () { fmo(this); });
$(".cb").blur(function () { bmo(this); });
}
$(function () { ready(); });
it works. I don't know what to do but I really need help. update this is my style(I think it may can effect the result)
<style type="text/css">
.out {
display: inline-block;
margin-left: 5px;
background-color: #56a100;
opacity: 0.5;
margin: auto;
width: 70%;
height: 70%;
}
.in {
display: inline-block;
margin-left: 5px;
background-color: #56a100;
opacity: 1;
margin: auto;
width: 100%;
height: 100%;
}
.base {
display: inline-block;
width: 50px;
height: 50px;
margin-left: 5px;
margin-top: 100px;
}
</style>
I uploaded the code here
Upvotes: 1
Views: 757
Reputation: 146191
Try this
$(function(){
$(".cb").on('mouseenter', function(){
$(this).stop(1,1).removeClass("out").addClass("in", 300);
})
.on('mouseleave', function(){
$(this).stop(1,1).removeClass("in").addClass("out",300);
});
});
Upvotes: 3
Reputation: 207511
Where did you get that second parameter on add/remove class?
Use queue, use chaining, and make functions to reuse code.
function helper (_elem, add, remove){
var elem = $(_elem);
elem.removeClass(remove).delay(300).queue(
function(next){
elem.addClass(add);
next();
}
);
}
//focus mouseower
function fmo() {
helper(this, "in","out");
}
//blur mouseout
function bmo() {
helper(this, "in","out");
}
function ready() {
$(".cb").on("mouseover focus", fmo).on("mouseout blur", fmo);
}
$(ready);
Upvotes: 0