Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在滚动到视图时向元素添加类_Javascript_Jquery_Css - Fatal编程技术网

Javascript 在滚动到视图时向元素添加类

Javascript 在滚动到视图时向元素添加类,javascript,jquery,css,Javascript,Jquery,Css,我想在元素滚动到视图中时尝试将类添加到元素中。到目前为止,我已经完成了。它不起作用 我已经做了上下滚动。我想把它也从下到上滚动 scrollOffset = 200; $(window).scroll(function() { $('.wpb_animate_when_almost_visible').each(function() { if ($(window).scrollTop() + $(window).height() - scrollOffset > $(this

我想在元素滚动到视图中时尝试将类添加到元素中。到目前为止,我已经完成了。它不起作用

我已经做了上下滚动。我想把它也从下到上滚动

scrollOffset = 200;

$(window).scroll(function() {
  $('.wpb_animate_when_almost_visible').each(function() {
    if ($(window).scrollTop() + $(window).height() - scrollOffset > $(this).position().top) {
      if ($(this).hasClass('wpb_start_animation')){
            $(this).toggleClass('wpb_start_animation');
      }
    }
  });
});
这是正确的方法吗??还是我做错了? 编辑

检查我的新小提琴


希望它会有用


.wpb_在_几乎可见时设置_动画{
高度:200px;
宽度:200px;
背景色:红色;
}
.wpb_开始_动画{
高度:200px;
宽度:900px;
背景颜色:蓝色;
}
$(文档).ready(函数(){
var scrollOffset=200;
$(窗口)。滚动(函数(){
$('.wpb_几乎可见时设置动画')。每个(函数(){
如果($(窗口).scrollTop()+$(窗口).height()-scrollOffset>$(此).position().top){
if($(this).hasClass('wpb\u start\u animation')){
$(this.removeClass('wpb_start_animation');
}
否则{
$(this.addClass('wpb_start_animation');
}
}
});
});
});
a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a


希望它会有用


.wpb_在_几乎可见时设置_动画{
高度:200px;
宽度:200px;
背景色:红色;
}
.wpb_开始_动画{
高度:200px;
宽度:900px;
背景颜色:蓝色;
}
$(文档).ready(函数(){
var scrollOffset=200;
$(窗口)。滚动(函数(){
$('.wpb_几乎可见时设置动画')。每个(函数(){
如果($(窗口).scrollTop()+$(窗口).height()-scrollOffset>$(此).position().top){
if($(this).hasClass('wpb\u start\u animation')){
$(this.removeClass('wpb_start_animation');
}
否则{
$(this.addClass('wpb_start_animation');
}
}
});
});
});
a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a


添加
转换:所有2个0if($(window).scrollTop()++$(window).height()>=$(this.position().top&&$(window.scrollTop()<$(this.position().top++$(this.height())
在jquery中切换类

演示:-

scrollOffset=200;
$(窗口)。滚动(函数(){
$('.wpb_几乎可见时设置动画')。每个(函数(){
//log(this+$(this.position().top);
如果($(窗口).scrollTop()+$(窗口).height()>=$(此).position().top&$(窗口).scrollTop()<$(此).position().top+$(此).height()){
//log(this+$(this.position().top);
//if($(this).hasClass('wpb\u start\u animation')){
if(!$(this).hasClass('wpb\u start\u animation')){
$(this.addClass('wpb_start_animation');
}
}
else if($(this).hasClass('wpb\u start\u animation')){
//log(this+$(this.position().top);
//if($(this).hasClass('wpb\u start\u animation')){
$(this.removeClass('wpb_start_animation');
//}
}
});
});
.wpb\u在几乎可见时设置动画{
高度:200px;
宽度:200px;
背景色:红色;
-webkit转换:所有2到0;
-moz转换:所有2秒到0秒;
-o-过渡:所有2s-0s;
-ms转换:所有2秒到0秒*/
过渡:所有2到0;
}
.wpb_开始_动画{
高度:200px;
宽度:900px;
背景颜色:蓝色;
}

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a


添加
转换:css中的所有2简化0;
并在jquery中使用条件
if($(窗口).scrollTop()++$(窗口).height()>=$(此).position().top&&$(窗口).scrollTop()<$(此).position().top++$(此).height())
切换类

演示:-

scrollOffset=200;
$(窗口)。滚动(函数(){
$('.wpb_几乎可见时设置动画')。每个(函数(){
//log(this+$(this.position().top);
如果($(窗口).scrollTop()+$(窗口).height()>=$(此).position().top&$(窗口).scrollTop()<$(此).position().top+$(此).height()){
//log(this+$(this.position().top);
//if($(this).hasClass('wpb\u start\u animation')){
if(!$(this).hasClass('wpb\u start\u animation')){
$(this.addClass('wpb_start_animation');
}
}
else if($(this).hasClass('wpb\u start\u animation')){
//log(this+$(this.position().top);
//if($(this).hasClass('wpb\u start\u animation')){
$(this.removeClass('wpb_start_animation');
//}
}
});
});
.wpb\u在几乎可见时设置动画{
高度:200px;
宽度:200px;
背景色:红色;
-webkit转换:所有2到0;
-moz转换:所有2秒到0秒;
-o-过渡:所有2s-0s;
-ms转换:所有2秒到0秒*/
过渡:所有2到0;
}
.wpb_开始_动画{
高度:200px;
宽度:900px;
背景颜色:蓝色;
}