Dust_In_The_Wind
Dust_In_The_Wind

Reputation: 3692

Changing background color with addClass in jQuery

I am learning jQuery, and I've written a simple script that is intended to add (or remove) a class when a mouse enters (or leaves) a div. The new class adds a different height and is also supposed to change the background color and opacity of the entered div.

Here's the code:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        #one {
            background-color: red;
        }

        #two {
            background-color: green;
        }

        #three {
            background-color: blue;
        }

        .change {
            height: 150px;
            background-color: rgba(0,0,0,0.4);
        }

    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("div").mouseenter(function () {
                $(this).addClass("change");
            });

            $("div").mouseleave(function () {
                $(this).removeClass("change");
            });
        });

    </script>

</body>
</html>

While the height changes, there's no effect on the background-color property. Why is that so?

I've checked out the .mouseenter(), .mouseleave() and the .addClass() API documentation, but couldn't find anything specific to this problem.

Upvotes: 1

Views: 10698

Answers (5)

Maus
Maus

Reputation: 11

There are some good answers here and explanations why it's not working, but it looks like no one has answered how to do what it seems like your intent was.

Which was to add the two background colors together to get a darker color on hover. You can get around it changing it to a grey by adding something else in.

For example:

.change {
    height: 150px;
    background-image: linear-gradient(to right, rgba(0,0,0,0.4) 0%,rgba(0,0,0,0.4) 100%)!important;
}

This puts a solid color gradient in as the background image which layers on top of the background color.

Like This JSFiddle

Upvotes: 1

guest271314
guest271314

Reputation: 1

Try utilizing css selectors #one:hover, #two:hover, #three:hover , :hover pseudo-class, transition . See also Specificity

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        #one {
            background-color: red;
        }

        #two {
            background-color: green;
        }

        #three {
            background-color: blue;
        }

        #one:hover, #two:hover, #three:hover {
            height: 150px;
            background-color:rgba(0,0,0,0.4);
            transition: height 1000ms, background-color 1000ms;
        }

    </style>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
</body>
</html>

Upvotes: 1

Dariusz Sikorski
Dariusz Sikorski

Reputation: 4407

Your javaScript code is 100% fine. The problem is in CSS You have written. Selection by id #one is more important than classname .one, so when you apply some #rules and .rules to the emenet at the same time, browser will chose the first ones as more important. If You can, try not to use #id selectors at all in css and leave it for javaScript usage.

css:

.one {
    background-color: red;
}

.two {
    background-color: green;
}

.three {
    background-color: blue;
}

html:

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

demo: https://jsfiddle.net/cz8v1gy4/

Upvotes: 1

Arielle Lewis
Arielle Lewis

Reputation: 540

Edited answer after a re-read:

You're running in to CSS specificity issues. IDs are more specific than classes, which means that your background color is going to stay the color of the ID and not the class. This is one reason why it's (often) recommended to not use IDs in CSS.

If you change everything over to classes it will work how you're expecting:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        .one {
            background-color: red;
        }

        .two {
            background-color: green;
        }

        .three {
            background-color: blue;
        }

        .change {
            height: 150px;
            background-color: rgba(0,0,0,0.4);
        }

    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div class="one"></div>
    <div class="two"></div>
    <div class="three"></div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("div").mouseenter(function () {
                $(this).addClass("change");
            });

            $("div").mouseleave(function () {
                $(this).removeClass("change");
            });
        });

    </script>

</body>
</html>

Upvotes: 1

Majid Fouladpour
Majid Fouladpour

Reputation: 30252

When you hover over div#one it becomes div#one.changed. Now with regard to background color you have two competing values:

#one {
  background-color: red;
}

And:

.change {
  background-color: rgba(0,0,0,0.4);
}

The first one wins, because it is more specific.

To get around this use

.change {
  height: 150px;
  background-color: rgba(0,0,0,0.4)!important;
}

Upvotes: 0

Related Questions