I have a fixed link that will change its text for each section on scroll. Not only the text, but also the URL should change. Has anyone an idea of how best to do that?
My Code:
$( document ).ready(function() {
$(window).on("scroll resize", function () {
var pos = $('.homeCaption').offset();
$('.homeStage').each(function () {
if (pos.top >= $(this).offset().top && pos.top <= $(this).next().offset().top) {
$('.homeCaption').html($(this).find('.projectDescription').text());
return;
}
});
});
$(document).ready(function () {
$(window).trigger('scroll');
});
});
section{
background-color: gray;
height: 100vh;
width: 100%;
}
.homeCaption {
position:fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
z-index: 999;
font-size: 24px;
text-align: center;
}
.green{
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="homeStage">
<div class="homeStageContent">
<p class="projectDescription" style="display: none;">Hello</p>
</div>
</section>
<section class="homeStage green">
<div class="homeStageContent">
<p class="projectDescription" style="display: none;">Hell2o</p>
</div>
</section>
<a class="homeCaption" href="#"></a>
Link to fiddle: https://jsfiddle.net/8zf2d2ah/3/
Thanks for any help :)