简体   繁体   English

进入查看端口的时间轴更改CSS

[英]Timeline change css on enter view port

Am trying to change the background color of the timeline on scroll like on this site .My replication of the sample is development site . 我正在尝试像本网站一样在滚动条上更改时间轴的背景色。我复制的示例是开发网站 Take a look at the codepen I tried using. 看看我尝试使用的Codepen The closest I have come to replicating it is with the code below which makes the change in color on each timeline circle flicker on/off on scroll. 我最复制它的是下面的代码,它使每个时间轴上的颜色更改在滚动时闪烁开/关。

jQuery(document).ready(function($) {
function onScroll() {
  $('.cd-timeline-block').each( function() {
    if( $(this).offset().top <= $(window).scrollTop()+$(window).height()*0.05 && $(this).find('.cd-timeline-img').hasClass('cd-inactive') ) {
      $(this).find('.cd-timeline-img').removeClass('cd-inactive').addClass('cd-active');
      $(this).find('.cd-date').addClass('cd-ssf-color');
    } else {
      $(this).find('.cd-timeline-img').addClass('cd-inactive').removeClass('cd-active');
      $(this).find('.cd-date').removeClass('cd-ssf-color');
    }
  });
};

$(window).scroll(onScroll);
});

I have made some modification to the above code. 我对上面的代码做了一些修改。

CodePen link: http://codepen.io/anon/pen/KzqWVm CodePen链接: http ://codepen.io/anon/pen/KzqWVm

Javascript: Javascript:

jQuery(document).ready(function($) {
  var $timeline_block = $('.cd-timeline-block');
  var firstelm = $timeline_block.first();

    //on scolling, show/animate timeline blocks when enter the viewport
    $(window).on('scroll', function() {
      var _win = $(window), iselm = null;
      $timeline_block.each(function(index) {
        var _this = $(this);
        if (((_this.offset().top - _win.height())) <= (_win.scrollTop())) {
          iselm = _this;
        }

      });
      if (_win.scrollTop() < $(firstelm).offset().top) {
        iselm = $(firstelm);
      }

      if (iselm) {
        $('.cd-active').removeClass('cd-active').addClass('cd-inactive');
          iselm.find('.cd-timeline-img').removeClass('cd-inactive').addClass('cd-active');

        if ((iselm.offset().top - _win.height()) > (_win.scrollTop() * 0.75)) {
          $('.cd-date').removeClass('cd-ssf-color');
        }
        iselm.find('.cd-date').addClass('cd-ssf-color');
      }
    });
  });

Continuing each loop on scroll might not work properly. 继续滚动每个循环可能无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM