Analysis of the effects of the timeline

怪我咯
Release: 2017-06-26 11:55:30
Original
2611 people have browsed it

Although the timeline is not a new thing, I was just interested in it, so I studied it. I recently searched for a timeline demo that I felt was better on the Internet, downloaded it, studied it, and made some modifications. Specifically The effect is as shown below: (This demo implements scrolling loading of images)

Code address: Responsive timeline.zip

How to implement scrolling loading of images ?The most important part is the following code part:

(function() {
  $(document).ready(function() {var timelineAnimate;
    timelineAnimate = function(elem) {      return $(".timeline.animated .timeline-row").each(function(i) {var bottom_of_object, bottom_of_window;
        bottom_of_object = $(this).position().top + $(this).outerHeight();
        bottom_of_window = $(window).scrollTop() + $(window).height();if (bottom_of_window > bottom_of_object) {          return $(this).addClass("active");}
      });
    };
    timelineAnimate();return $(window).scroll(function() {      return timelineAnimate();
    });
  });

}).call(this);
Copy after login

Because our example is not actually purely dynamically loading images (that is, dynamically generating html tags and dom elements, which can be done later) To implement), just hide the original page or change the opacity attribute value from 0 to 1. By looking at the above code, this place actually has a small algorithm.

if (bottom_of_window > bottom_of_object) 才会去给当前对象(即类控制器为.timeline.animated .timeline-row的对象)添加类选择器active(暂且先不具体该类选择器实现什么效果)
我们先讨论下这两个值bottom_of_window和bottom_of_object
Copy after login
bottom_of_window = $(window).scrollTop() + $(window).height();
Copy after login
$(window).scrollTop()表示当前滚动条的位置距离页面顶端的距离,其实可以理解为页面滚动条拉到某个位置,顶部隐藏的页面内容的高度;
Copy after login
$(window).height()表示当前可视页面区域的高度;

bottom_of_object = $(this).position().top + $(this).outerHeight();
Copy after login
$(this).position().top表示当前元素距离父元素的距离,个人理解为应该就是margintop的值吧,
Copy after login
$(this).outerHeight()表示当前元素的高度还有padding+border,但不包括margin
如下盒子模型:
Copy after login

当if (bottom_of_window > bottom_of_object)为真的情况下,我们看到执行了return $(this).addClass("active"),这段代码起什么作用呢,其实作用就是
用户在拖动滚动条时时间轴内容的过渡效果,我们可以看到添加效果是向.timeline.animated .timeline-row添加的,我们查看样式文件关于这个类选择器的所在对象都有什么
样式效果:
Copy after login
.timeline.animated .timeline-row .timeline-content {opacity: 0;left: 20px;-webkit-transition: all 0.8s;-moz-transition: all 0.8s;transition: all 0.8s; }
  .timeline.animated .timeline-row:nth-child(odd) .timeline-content {left: -20px; }
  .timeline.animated .timeline-row.active .timeline-content {opacity: 1;left: 0; }
  .timeline.animated .timeline-row.active:nth-child(odd) .timeline-content {left: 0; }
Copy after login

Obviously after executing $(this).addClass("active"), the following style works.

.timeline.animated .timeline-row.active .timeline-content {opacity: 1;left: 0; }
Copy after login
Copy after login

Why is there a transitional floating effect? ​​In fact, it defines

.timeline.animated .timeline-row .timeline-content {opacity: 0;left: 20px;-webkit-transition: all 0.8s;-moz-transition: all 0.8s;transition: all 0.8s; }
Copy after login
Copy after login

transition (css3 tag) and defines the class selector .timeline.animated .timeline-row .timeline-content Any style change of the included object will have a transition effect of 0.8.s. Of course, we can modify this time

again.

Because before we execute $(this).addClass("active"), the style of the object on the left side of our timeline is as follows (let’s talk about the left side of the timeline first)

.timeline.animated .timeline-row:nth-child(odd) .timeline-content {
Copy after login
opacity: 0;

:; }
Copy after login

The style after execution is as follows:

.timeline.animated .timeline-row.active .timeline-content {opacity: 1;left: 0; }
Copy after login
Copy after login

So there will be an implementation effect from left to right, because the transparency and left margin have changed.

Why is the object on the right side of the timeline a cut-in effect from right to left? Before executing $(this).addClass("active") first, the style of the object on the right side of the timeline is

.timeline.animated .timeline-row .timeline-content {opacity: 0;left: 20px;-webkit-transition: all 0.8s;-moz-transition: all 0.8s;transition: all 0.8s; }
Copy after login
Copy after login

We see that left is 20px, opacity (transparency is 0), after executing $(this).addClass("active")

 .timeline.animated .timeline-row.active .timeline-content {opacity: 1;left: 0; }
Copy after login

left is 0, opacity (transparency is 1), and transition is 0.8s, so there is a transition effect from right to left.

There is a delicate point in the above code

.timeline-row:nth-child(odd)中的nth-child(odd)选择器,因为css的解析顺序是从右到左,所以这个地方的意思表示.timeline-row为奇数的对象(属于其父元素的第奇数个timeline-row)
Copy after login
假如有以下代码,
Copy after login
<!DOCTYPE html><html><head><style> p:nth-child(2){background:#ff0000;}</style></head><body><h1>这是标题</h1><p>第一个段落。</p><p>第二个段落。</p><p>第三个段落。</p><p>第四个段落。</p><p><b>注释:</b>Internet Explorer 不支持 :nth-child() 选择器。</p></body></html>
Copy after login

where p:nth-child(2) represents the second child element in the parent element of p, and this child element is p. At this time, the second child element happens to be P, so the display effect is as follows

If changed to the following

<h1>这是标题</h1><h2>第一个段落。</h2><p>第二个段落。</p><p>第三个段落。</p><p>第四个段落。</p>
Copy after login

The effect will be as follows

Because the second child element of p:nth-child(2) is h2, not p, no matching element is found, so the background color does not take effect.

Let’s study this first, and we will have time to prepare the page later. Elements are dynamically loaded, instead of being displayed early on the page, they are only shown or hidden by controlling the transparency.

 <br>
Copy after login
<br><br>
Copy after login

The above is the detailed content of Analysis of the effects of the timeline. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!