jQuery - Change image src on hover
You can use mouseenter, mouseleave functions.
<a href="#" class="hover-change-img"><img src="sample1.png" class="img-responsive"></a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
$("document").ready(function(){
$(".hover-change-img img").mouseenter(function(){
$(this).attr('src','sample2.png');
});
$(".hover-change-img img").mouseleave(function(){
$(this).attr('src','sample1.png');
});
});
</script>
This can be done without javascript, with only css. Like this:
Give different classes for icons like fb
for facebook,tw
or twitter and so on.
HTML:
<div id="main-social">
<a href="#!"><span class="img-social fb left" title="Company - Facebook"></span></a>
<a href="#!"><span class="img-social tw left" title="Company - Twitter"></span></a>
<a href="#!"><span class="img-social ln left" title="Company - LinkedIn"></span></a>
<a href="#!"><span class="img-social wp left" title="Company - Word Press"></span></a>
</div>
CSS:
.img-social{display:inline-block;height:20px;width:20px;}
.fb{background:url("images/fb.png");}
.fb:hover{background:url("images/fb-active.png");}
.tw{background:url("images/twitter.png");}
.tw:hover{background:url("images/twitter-active.png");}
.ln{background:url("images/linkedin.png");}
.ln:hover{background:url("images/linkedin-active.png");}
.wp{background:url("images/wordpress.png");}
.wp:hover{background:url("images/wordpress-active.png");}
You can use sprite for efficiency.
You could do this just in CSS if you don't need to be compliant with older browsers.
To do it in jquery, you can try something like this:
$(".img-social").hover(function(){
$(this).attr("src", function(index, attr){
return attr.replace(".png", "-active.png");
});
}, function(){
$(this).attr("src", function(index, attr){
return attr.replace("-active.png", ".png");
});
});