Making Data-type Breadcrumb from a <li> Scrolling Down Page

Asked

Viewed 74 times

0

Can someone help me with that.

I have the page and html which is a giant list.

Each < li> has a date field in it

<li class="classe_dia" data-dia="12">Conteúdo</li>
<li class="classe_dia" data-dia="13">Conteúdo</li>
<li class="classe_dia" data-dia="14">Conteúdo</li>
<li class="classe_dia" data-dia="15">Conteúdo</li>

When scrolling the page I would like to capture the date-day of the element that is at the 150px height of $(window). scrollTop()

Codepen: https://codepen.io/alemoraesra/pen/eXjdyx

1 answer

1


   var $topOffset = [],
	$section = $('.classe_dia');

$(window).resize(function() {
  $topOffset = [];
  $.each($section, function() {
$topOffset.push($(this).offset().top);
  });
}).trigger('resize');

$(window).scroll(function() {
var $browserTop = $(window).scrollTop();

$.each($section, function(i) {
	if (($browserTop >= $topOffset[i]) && ($browserTop < $topOffset[i] + $(this).height())) {
		var result = $(this).attr('data-dia');
 $('#resultado').text(result);

	}
});
}).trigger('scroll');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<li class="classe_dia" data-dia="12">Conteúdo</li>
<li class="classe_dia" data-dia="13">Conteúdo</li>
<li class="classe_dia" data-dia="14">Conteúdo</li>
<li class="classe_dia" data-dia="15">Conteúdo</li> 

With this script I get the value of the LI element that is at the same height of scrollTop. I hope to have helped you!

  • Hi John Victor, I want to make a breadcrumb of a list I have, I tried to reproduce in this codepen https://codepen.io/alemoraesra/pen/eXjdyx, did not work well, after it comes at the end it does not change more

  • 1

    Oops. I managed to solve, the new code is in the same place as before

Browser other questions tagged

You are not signed in. Login or sign up in order to post.