<?php
$i = 0;
$datasrc = '';
// loop through the images
// result is a comma separated list of the srcs
foreach($project->images() as $image) {
if ($i == 0) {
$datasrc .= $image->url();
} else {
$datasrc .= ',' . $image->url();
}
$i++;
}
?>
<div class="img--container">
<img src="<?php echo $project->images()->first()->url() ?>" alt="<?php echo $project->title()->html() ?>" data-src="<?php echo $datasrc ?>" data-index="0">
</div>
JS :
$('img').bind('mouseenter', function () {
var _this = $(this),
_imgSrc = _this.attr('data-src').split(','),
_imgIndex = _this.attr('data-index');
// Increment our index
_imgIndex++;
// If the incremented value is higher than the images count -> back to 0
// (since length isn't 0-based, we need to substract 1 in order to compare the two)
_imgIndex = (_imgIndex > _imgSrc.length - 1) ? 0 : _imgIndex;
// Insert the new src and update the index
_this.attr({
src: _imgSrc[_imgIndex],
'data-index': _imgIndex
});
});
@sylvainjule the fiddle is exactly what i’m looking for. But the html seems not to work. Maybe because i did not show you the whole code. Sorry. Check:
<div>
<?php
$projects = page('projects')->children()->visible();
$count = 0; foreach ($projects as $project) : ?>
<?php if($count % 3 < 1): ?>
<div style="text-align: center;">
<div>
<h2><a href="<?php echo $project->url() ?>"><?php echo $project->title()->html() ?></a></h2>
<a href="<?php echo $project->url() ?>">
<?php
$i = 0;
$data-src = '';
// loop through the images
// result is a comma separated list of the srcs
foreach($project->images() as $image) {
if ($i == 0) {
$data-src .= $image->url();
} else {
$data-src .= ',' . $image->url();
}
$i++;
}
?>
<div class="img--container">
<img src="<?php echo $project->images()->first()->url() ?>" alt="<?php echo $project->title()->html() ?>" data-src="<?php echo $data-src ?>">
</div>
</a>
<div>
<span><?php echo $project->images()->count() ?> Images</span>
</div>
</div>
</div>
<?php else: ?>
<div>
<h2><a href="<?php echo $project->url() ?>"><?php echo $project->title()->html() ?></a></h2>
<a href="<?php echo $project->url() ?>">
<?php
$i = 0;
$data-src = '';
// loop through the images
// result is a comma separated list of the srcs
foreach($project->images() as $image) {
if ($i == 0) {
$data-src .= $image->url();
} else {
$data-src .= ',' . $image->url();
}
$i++;
}
?>
<div class="img--container">
<img src="<?php echo $project->images()->first()->url() ?>" alt="<?php echo $project->title()->html() ?>" data-src="<?php echo $data-src ?>">
</div>
</a>
<div>
<span><?php echo $project->images()->count() ?> Images</span>
</div>
</div>
<?php endif; ?>
<?php $count++; endforeach ?>
</div>
Woops it was a late-night-code-mistake from my part, remove the dash in the variable’s name ($datasrc instead of $data-src), i edited my previous post.
Hi, thx for your answer.
It works, but every project has the same data-src images like project 1 now. I hover and the same images appear on on every project.