I am doing a WordPress test project where I have an overview page for some other projects. When I hover over a project, the body get’s a background related to the project.
I wrote a loop adding an unique id of #project-number to each project dynamically. For simplicity, I wrote the ID’s myself in the code below. Now I would like to add a loop that adds the hover support for every project I add. The problem is that they will all need another hover image. Otherwise I would have to add some jQuery with every project I add.
HTML
<div class="row">
<div class="column12 project-overview" id="project-1">
<h2>Project 1</h2>
<p>Type of project - date</p>
</div>
</div>
<div class="row">
<div class="column12 project-overview" id="project-2">
<h2>Project 2</h2>
<p>Type of project - date</p>
</div>
</div>
<div class="row">
<div class="column12 project-overview" id="project-3">
<h2 class="test">Project 3</h2>
<p>Type of project - date</p>
</div>
</div>
<div class="row">
<div class="column12 project-overview" id="project-4">
<h2 class="test">Project 4</h2>
<p>Type of project - date</p>
</div>
</div>
CSS
body {
font-family: Helvetica, sans-serif;
background: rgba(199, 178, 153, 0.1);
}
jQuery
var body = $('body');
$(function(){
$('#project-1').hover(function(){
body.css('background', 'url(http://placehold.it/1920x1080/6699FF) center top fixed');
body.css('background-size', 'cover');
}).mouseleave(function(){
body.css('background', 'rgba(199, 178, 153, 0.1)');
});
});
$(function(){
$('#project-2').hover(function(){
body.css('background', 'url(http://placehold.it/1920x1080/00FF00) center top fixed');
body.css('background-size', 'cover');
}).mouseleave(function(){
body.css('background', 'rgba(199, 178, 153, 0.1)');
});
});
$(function(){
$('#project-3').hover(function(){
body.css('background', 'url(http://placehold.it/1920x1080/FFFF00) center top fixed');
body.css('background-size', 'cover');
}).mouseleave(function(){
body.css('background', 'rgba(199, 178, 153, 0.1)');
});
});
$(function(){
$('#project-4').hover(function(){
body.css('background', 'url(http://placehold.it/1920x1080/0000FF) center top fixed');
body.css('background-size', 'cover');
}).mouseleave(function(){
body.css('background', 'rgba(199, 178, 153, 0.1)');
});
});
I made pen, which is available here http://codepen.io/linkerd/pen/BsanI .
Would really appreciate some help. I tried several options, but none seemed to work.
You can use custom
data-*
attributes to store image to be displayed on hover.HTML
Script
You can define image url to element’s data attribute which will be replaced on body with mouse over:
Here is demo: http://codepen.io/anon/pen/JLfFA
html:
jQuery: