0

I'm trying to toggle some css whilst I hover over a div. Normally I would use 'toggleclass' but the new background color doesn't seem to overwrite the existing class so I resorted to the method below.

The issue is how to toggle the css off after the hover.

$('#menu_first_home').hover(function(){
$(this).css({ 'border': '10px solid rgba(186, 0, 0, 0.2)' });
$('.search-box').css({ 'background-color': 'rgba(186, 0, 0, 0.5)', 'color': 'rgba(186, 0, 0, 0.5)'    });
});
5
  • 1
    what is the problem when you use toggle class... can you share the code and the css used in that case Commented Oct 13, 2014 at 10:10
  • can u show us a fiddle ? Commented Oct 13, 2014 at 10:13
  • when you say toggle class isn't working is it because you have assigned the original colour to the id and then try to overwrite it with the class? Commented Oct 13, 2014 at 10:22
  • You should use toggleClass for this. If the rules contained in the new class doesn't apply properly, it's only because you have a CSS specifity problem. Commented Oct 13, 2014 at 10:23
  • Sorry bad link - jsfiddle.net/#&togetherjs=tL9Oqix6Cb Commented Oct 13, 2014 at 10:38

3 Answers 3

1

ToggleClass should work, but here is an example for your code to change the css attributes on mouseleave

$('#menu_first_home').hover(

function () { // Mouseenter
    $(this).css({
        'border': '10px solid rgba(186, 0, 0, 0.2)'
    });
    $('.search-box').css({
        'background-color': 'rgba(186, 0, 0, 0.5)',
        'color': 'rgba(186, 0, 0, 0.5)'
    });
}, function(){ // Mouseleave
    $(this).css({
        'border': '10px solid rgba(186, 0, 0, 0.2)'
    });
    $('.search-box').css({
        'background-color': 'rgba(186, 0, 0, 0.5)',
        'color': 'rgba(186, 0, 0, 0.5)'
    });

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

Comments

0

You can have CSS classes for border and background color. Add / remove them for hover on and off as shown below -

CSS -

.borderClass{border: 10px solid rgba(186, 0, 0, 0.2);}

.backgroundColorClass{background-color: rgba(186, 0, 0, 0.5);
                      color: rgba(186, 0, 0, 0.5);}

jQuery -

$('#menu_first_home').hover(function(){
   //for hover on
   $(this).addClass("borderClass");
   $('.search-box').addClass("backgroundColorClass");
},function(){
   //for hover off
   $(this).removeClass("borderClass");
   $('.search-box').removeClass("backgroundColorClass");
});

3 Comments

looks like it may be done shorter - $('#menu_first_home').hover(function(){ $(this).toggleClass('borderClass'); $('.search-box').toggleClass("backgroundColorClass"); });
but this will toggle class for every mouseenter event only and I think OP want to add color when mouseenter and remove it when mouseleave, correct me if I am wrong.
if u use single function on hover it will be called on mouseleave and mouseetnter. Looks like result will be same
0

You can use jquery hover method along with css classes to solve it. hover method accept two callback function, one when the pointer moves over the item, and one when it leaves:

$(item).hover(function() { ... }, function() { ... });

Instead of adding styles dynamically, define it in css files :

#menu_first_home.hoverClass {
    border-color: rgba(186, 0, 0, 0.2);
}

and on hover, add the class (e.g. "hoverClass") to that particular element, and on mouse out remove it.

Here is the live working demo.

Comments

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.