I have to create the effect of bordered when I hover over element and vice versa.
I managed to do it, but the problem is that every time I hover over one of those elements I cancel the hovering effect on the element I was hovering over.
When I hover over with id=”12345img” gets border and with id=”12345a” gets color effect. When I do it vice versa it also works, but only for the first time I’m hovering over it.
This is the code.
<script type="text/javascript">
$(document).ready(function () {
$('.portfolioProject a').hover(function () {
var idElement = this.id;
$("#" + idElement.replace('a', '') + "img").css('border', '2px solid #cdfc5d');
$("#" + idElement + "a").css('color', '#cdfc5d');
});
$('.portfolioProject a').mouseleave(function () {
var idElement = this.id
$("#" + idElement.replace('a', '') + "img").css('border', 'none');
$("#" + idElement + "a").css('color', 'white');
});
$('.portfolioImage img').hover(function () {
var idElement = this.id;
$("#" + idElement.replace('img', '') + "a").css('color', '#cdfc5d');
$("#" + idElement + "img").css('border', '2px solid #cdfc5d');
});
$('.portfolioImage img').mouseleave(function () {
var idElement = this.id
$("#" + idElement.replace('img', '') + "a").css('color', 'white');
$("#" + idElement + "img").css('border', 'none');
});
});
</script>
My question is how to fix it to make it work every time.
I think you have to create CSS class for the mouse hover effect, Then you just Have to Toggle the class on event:
Jquery:
HTML:
CSS:
jsfiddle
You can toggle a different css class to your links for sure. Just modify this in jquery code (in the toggleClass function) and add the class to the css part