foreach ($items as $index => $item) {
$arr = explode(' ', trim($item['title'])); // select the first word
- $res .= '<li class="year-item" data-year="' . $index . '"><a>' . $arr[0] . '</a></li>';
+ $res .= '<li class="year-item" data-year="' . $index . '"><a href="#'.$index.'">' . $arr[0] . '</a></li>';
}
$res .= '</ul>'; // .year-item
foreach ($items as $index => $item) {
- $res .= '<div class="timeline-item" data-timeline="' . $index . '">';
+ $res .= '<div id="#'.$index.'" class="timeline-item" data-timeline="' . $index . '">';
$res .= '<h2 class="timeline-item-title">' . $item['title'] . '</h2>';
$res .= '<h3 class="timeline-item-subtitle">' . $item['subtitle'] . '</h3>';
$res .= wp_get_attachment_image($item['image']['id'], 'large', false, ['class' => 'timeline-item-img']);
// import $ from 'jquery';
-// console.log('wesh');
+// let navigation = $('.year-item');
-let link = $('.year-item');
+// navigation.on('click',function(e){
+// e.preventDefault();
+// var target = $($(this).attr('href'));
+// if(target.length){
+// var scrollTo = target.offset().top;
+// $('body, html').animate({scrollTop: scrollTo+'px'}, 800);
+// }
+// });
-link.on('click', function() {
- let target = $(this).data('year');
+// //TODO : ON CLICK REDIRECT TO THE RIGHT SECTION
+// navigation.on('click', function() {
+// let target = $(this).data('year');
+// alert(target);
+// // $('html, body').animate({
+// // scrollTop: $(target).offset().top(),
+// // },'slow');
+// });
- alert(target);
+
+// smoothscroll on click bug
+$('.year-item ').on('click', function() {
+ let hash = $(this).data('year');
+ let $toElement = $('a[href=#'+hash+']');
+ let toPosition = $toElement.offset().top;
+ $('html,body').animate(
+ {
+ scrollTop: toPosition,
+ },500);
+ console.log(toPosition);
});
+
+$('.year-item:first').addClass('active'); // add class active to the first year-item
+
+// fonction scroll spy
$(window).on('scroll', function () {
- var sections = $('.timeline-item')
- , nav = $('.year-item')
- , nav_height = nav.outerHeight()
- , cur_pos = $(this).scrollTop();
- sections.each(function() {
- var top = $(this).offset().top - nav_height,
+ let timeline = $('.timeline-item'),
+ nav = $('.year-item'),
+ nav_height = nav.outerHeight(),
+ currentPosition = $(this).scrollTop();
+
+ // see the current position on scroll
+ console.log('current position : ' + currentPosition);
+
+ // pour chaque element timeline
+ timeline.each(function () {
+ // on detecte le top et bottom de chaque section timeline
+ let top = $(this).offset().top - nav_height,
bottom = top + $(this).outerHeight();
- if (cur_pos >= top && cur_pos <= bottom) {
- nav.find('a').removeClass('active');
- sections.removeClass('active');
+ console.log('top : ' + top + ' ---- ' + 'bottom : ' + bottom);
+
+ // si la position actuel est supérieur ou egale au top ET inferieur ou egale au bottom alors on ajoute la class active sur l"élément timeline-item en question
+ if (currentPosition >= top && currentPosition <= bottom) {
+ timeline.removeClass('active');
$(this).addClass('active');
- // nav.data('year').addClass('active');
+
+ let thing = $(this);
+ // on attribut la class active a l'element ''year-item'' uniquement si les element data attribute de timeline-item ET year-item ont la meme valeur
+ nav.each(function () {
+
+ if (thing.data('timeline') === $(this).data('year')) {
+ nav.removeClass('active');
+ $(this).addClass('active');
+ }
+ });
+ // console.log($(this).data('timeline'));
}
});
});
\ No newline at end of file