I'm currently experimenting with a small project where I have multiple div elements containing an image, h1 tag, and p tag all sharing the same class name. My goal is to add CSS effects that make the h1 tag and p tag slide into view and become visible when hovering over the specific div element they are within.
The issue I'm facing is that the code I've implemented applies the effects to all div elements instead of just the one being hovered on.
Below is a glimpse of my code:
The CSS
<style>
.col-md-4 {
margin-top: 30px;
margin-bottom: 26px;
color: white;
}
.title-1 {
margin-top: -260px;
text-align: center;
position: relative;
top: -104px;
opacity: 0;
transition: top 1s, opacity 1s;
}
.paragraph-1 {
margin-top: 160px;
text-align: center;
position: relative;
top: 60px;
opacity: 0;
transition: top 1s, opacity 1s;
}
.title-2 {
margin-top: -260px;
text-align: center;
position: relative;
top: -20px;
opacity: 1;
transition: top 1s, opacity 1s;
}
.paragraph-2 {
margin-top: 160px;
text-align: center;
position: relative;
top: -20px;
opacity: 1;
transition: top 1s, opacity 1s;
}
Here's the jQuery snippet
<script>
$('document').ready(function() {
$('.col-md-4').mouseover(function() {
$('h1').addClass('title-2');
$('h1').removeClass('title-1');
$('p').addClass('paragraph-2');
$('p').removeClass('paragraph-1');
});
$('.col-md-4').mouseleave(function() {
$('h1').addClass('title-1');
$('h1').removeClass('title-2');
$('p').addClass('paragraph-1');
$('p').removeClass('paragraph-2');
});
});
</script>
And this is how the HTML structure looks like
<div class="col-md-4">
<img src="images/remodeling3.jpg" class="img">
<h1 class="title-1">Title Here</h1>
<p class="paragraph-1">Paragraph here.</p>
</div>
<div class="col-md-4">
<img src="images/remodeling3.jpg" class="img">
<h1 class="title-1">Title Here</h1>
<p class="paragraph-1">Paragraph here.</p>
</div>
<!-- More similar divs -->
I understand the need to use the 'this' keyword to target the current item, but I'm struggling to implement it correctly in order to achieve the desired effects with my existing code. Any assistance would be greatly appreciated.