here is a helpful code snippet for detecting scroll using jQuery:
$(document).ready(function(){
$(window).on('scroll',function(){
var Wscroll = $(this).scrollTop();
$('a[name^="para"]').each(function(){
var ThisOffset = $(this).closest('p').offset();
if(Wscroll > ThisOffset.top && Wscroll < ThisOffset.top + $(this).closest('p').outerHeight(true)){
$(this).closest('p').css('background','red');
console.log($(this).attr('id')); // will return undefined if this anchor not has an Id
// to get parent <p> id
console.log($(this).closest('p').attr('id')); // will return the parent <p> id
}
});
});
});
Check out the demo here
Don't forget to include Jquery in your project.
To Explain : $(this)
inside .each()
selects anchors with name starting with para, while closest('p')
selects the parent <p>
of this anchor. Feel free to customize this script to fit your needs!