简体   繁体   English

div在水平进入视口时的动画

[英]Animation for div when it enters the viewport horizontally

I'm creating an infinite horizontal feed where I want to animate the element when it enters the viewport horizontally. 我正在创建一个无限水平馈送,我想在元素水平进入视口时为其设置动画。 I'm trying waypoint.js for this. 我正在尝试waypoint.js。

JS FIDDLE JS FIDDLE

<div id="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

Adding .show to div which will change opacity of div from 0 to 1. 将.show添加到div,这会将div的不透明度从0更改为1。

$(function() {
$(".item").each(function(){
   $(this).waypoint(function() {
     $(this).addClass('show');
     }, {
       offset: '100%',
       horizontal: true
     });  
 });      
});

CSS CSS

.item
{
width:500px;
height:250px;
background:red;
color:#fff;
display:inline-block;
opacity:.2;
}
.item.show
{
opacity:1;
}

But now the elements would not change their opacity from 0 to 1 when they enter the viewport horizontally. 但是现在元素在水平进入视口时不会将其不透明度从0更改为1。 Any idea why? 知道为什么吗? Sorry, I'm really new to javascript and waypoint. 对不起,我是javascript和waypoint的新手。

The 100% width is incorrect; 100%宽度不正确; if you change it to 500px (the specified width of the individual items), it will work. 如果将其更改为500px(单个项目的指定宽度),它将起作用。 This is non-optimal though (you will need to update both the JS and CSS whenever you change anything): a better approach would probably be to get the width of the items via JS, and use that value as the offset. 这不是最佳的(你需要在更改任何东西时更新JS和CSS):更好的方法可能是通过JS获取项目的宽度,并使用该值作为偏移量。

Percentage values passed to offset are percentages of the viewport, which I assume is not what you want here, rather the offset should be ~the width of each item. 传递给offset百分比值是视口的百分比,我假设这不是你想要的,而是偏移量应该是〜每个项目的宽度。 At the minute, any item even partially within the viewport is opaque, so you never see any change. 在某一刻,任何甚至部分在视口内的项目都是不透明的,所以你永远不会看到任何变化。

See http://imakewebthings.com/waypoints/api/offset-option/ http://imakewebthings.com/waypoints/api/offset-option/

this inside the waypoint handler is not the element, it is the Waypoint instance. this在航点处理程序内部不是元素,它是Waypoint实例。 You want to use $(this.element).addClass('show') 你想使用$(this.element).addClass('show')

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

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