I have the following HTML. Essentially 4 similarly styled boxes that are inline with eachother. When I hover each individual box, i'm trying to change the img src to the data-alt-src, only for the individual div thats being hovered.
I have the jQuery further below to do this. But it is very hard-coded, i'm wondering if there is a better way of doing this? Also with an ease-in-out of .4s would be a huge help as I already have the CSS doing this for the background color. The main source image is blue and the alt source is white.
<div class="skillsBox">
<div class="skill_div">
<div class="skill_img">
<img src="css/house.png" data-alt-src="css/house2.png" alt="House"/>
</div>
<div class="skill_title">
<h3>Repairs</h3>
</div>
</div>
<div class="skill_div">
<div class="skill_img">
<img src="css/settings.png" data-alt-src="css/settings2.png" alt="Repair"/>
</div>
<div class="skill_title">
<h3>Improvement</h3>
</div>
</div>
<div class="skill_div">
<div class="skill_img">
<img src="css/shopping-cart.png" data-alt-src="css/shopping-cart2.png" alt="Cart"/>
</div>
<div class="skill_title">
<h3>Affordable</h3>
</div>
</div>
<div class="skill_div">
<div class="skill_img">
<img src="css/like.png" data-alt-src="css/like2.png" alt="Like"/>
</div>
<div class="skill_title">
<h3>Satisfaction</h3>
</div>
</div>
</div>
Here is the jQuery:
$(".skill_div:nth-of-type(1)").hover(
function () {
var $this = $(".skill_div:nth-of-type(1) img");
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource)
});
$(".skill_div:nth-of-type(2)").hover(
function () {
var $this = $(".skill_div:nth-of-type(2) img");
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource)
});
$(".skill_div:nth-of-type(3)").hover(
function () {
var $this = $(".skill_div:nth-of-type(3) img");
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource)
});
$(".skill_div:nth-of-type(4)").hover(
function () {
var $this = $(".skill_div:nth-of-type(4) img");
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource);
});
Thanks a bunch, I did try to research the answer but could not find with my particular key-words, if this has been done before I do apologise.