Renārs Abeļūns
Renārs Abeļūns

Reputation: 25

How to make a hover effect stay after unhovering the image?

I made a JS Fiddle https://jsfiddle.net/t3zaskum/ to show it better, I am trying to use jscript to make the items go up a bit when hovered but it just aint working.

Javascript:

$('.cover-img').mouseover(function() {
    if ($(this).hasClass('active')) return;
    $('.active').removeClass('active');
    $(this).addClass('active');
});

CSS:

.top-section-hover {
    display: flex;
    padding-top: 10em;
    justify-content: center;
    flex-grow: 2;
    text-align: center;
    text-transform: uppercase;
    font-weight: bold;
    max-width: 96%;
}

.top-section-hover img {
    position: relative;
    top: 0;
    transition: top ease 0.5s;
    left: 0;
}

.active {
    top: -30px;
    transition: all 0.5s ease
}

.cover-title {
    font-weight: bold;
}

.hover-cover-middle:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}

.hover-cover-left:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}

.hover-cover-right:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}

Thanks in advance

Upvotes: 2

Views: 178

Answers (1)

qiAlex
qiAlex

Reputation: 4356

HTML is fine, JS is fine, only thing is:

.top-section-hover img - is more specific than .active. So we always see a styles of .top-section-hover img.

changing .active to .top-section-hover img.active making that block more specific, so it's rules is applied

$('.cover-img').mouseover(function() {
    if ($(this).hasClass('active')) return;
    $('.active').removeClass('active');
    $(this).addClass('active');
});
.top-section-hover {
    display: flex;
    padding-top: 10em;
    justify-content: center;
    flex-grow: 2;
    text-align: center;
    text-transform: uppercase;
    font-weight: bold;
    max-width: 96%;
}

.top-section-hover img {
    position: relative;
    top: 0;
    transition: top ease 0.5s;
    left: 0;
}

.top-section-hover img.active {
    top: -30px;
    transition: all 0.5s ease
}

.cover-title {
    font-weight: bold;
}

.hover-cover-middle:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}

.hover-cover-left:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}

.hover-cover-right:hover .cover-title {
    color: white;
    border-bottom: 3px solid white;
    transition: 0.5s all;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="top-section-hover">
            <span class="hover-cover-left">
                <a href="#" class="cover-left-content">
                    <img src="https://via.placeholder.com/150

C/O https://placeholder.com/" alt="" class="cover-img">
                    <span class="cover-title">Office</span>
                </a>
            </span>
            
            <span class="hover-cover-middle">
                <a href="#" class="cover-left-content">
                    <img src="https://via.placeholder.com/150

C/O https://placeholder.com/" alt="" class="cover-img">
                    <span class="cover-title">Home</span>
                </a>
            </span>
            
            <span class="hover-cover-right">
                <a href="#" class="cover-left-content">
                    <img src="https://via.placeholder.com/150

C/O https://placeholder.com/" alt="" class="cover-img">
                    <span class="cover-title">Events</span>
                </a>
            </span>
</div>

Upvotes: 2

Related Questions