Can the addClass and fadeIn functions be combined in JS?
Although I'm not very experienced with JS, I'm working on a script where the div container changes background color by fading in and out on text hover.
I've created a CodePen to demonstrate what I'm trying to achieve. Currently, when you hover over a title, the background appears abruptly without any transition effect.
$(document).ready(function () {
$('#1st').hover(function () {
$('#BG').addClass('first');
$('#BG').removeClass('second');
});
$('#2nd').hover(function () {
$('#BG').addClass('second');
$('#BG').removeClass('first');
});
});
section{
min-height: 500px;
text-align: center;
}
.title{
margin: 50px auto;
font-size: 5em;
font-weight: 400;
}
.first {
background: url(https://cdn.pixabay.com/photo/2022/11/03/15/24/coffee-7567749_960_720.jpg);
background-size: cover;
}
.second {
background-image: url(https://cdn.pixabay.com/photo/2022/10/25/13/28/hanoi-7545860_960_720.jpg);
background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="BG">
<div id="1st" class="title">TITLE</div>
<div id="2nd" class="title">TITLE 2</div>
</section>
Is it possible to fade in the current item being hovered over and fade out the other one using fadeIn? Any suggestions would be greatly appreciated :-) Thanks!