1

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

10
  • 1
    nitpick: Your code is full of globals because you are not declaring var. Var is not optional. Commented Jul 21, 2012 at 12:52
  • @epascarello: I added var but there is no change to act. it's still not working Commented Jul 21, 2012 at 12:56
  • 1
    @ahmadalishafiee var wasn't the problem you are trying to solve, but it was still a problem Commented Jul 21, 2012 at 12:57
  • @HunterMcMillen: I know. and I edited the code Commented Jul 21, 2012 at 13:00
  • Are there any errors on the page? Are you sure jQueryUI is included properly? Commented Jul 21, 2012 at 13:10

2 Answers 2

3

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); 
    });
});​

DEMO.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

2 Comments

The second parameter is added by jQueryUI's version of addClass:
I don't want delay!! I want animate with addClass method and Jquery-UI has it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.