javascript - 使用 JQuery 添加/删除基于 div/section 更改的类

标签 javascript jquery html css

我有一个固定的图像,我需要根据 div/section 的变化对其进行动画处理

<div class="fixed">
  <img src="some_image.jpg" class="child-fixed">
</div>

<section class="section" id="section-1">
//this is rotating section
</section>
<section class="section" id="section-2">
//this is flip section
</section>

CSS 看起来像这样,

.fixed{
position:fixed;
margin:50% auto;
width:50px;
height:50px;
}
.fixed-child{
width:100%;
height:100%;
margin:0;
padding:0;
}
.section{
height:100vh;
width:100vw;
}
.rotate{
//some css totate styles here
}
.flip{
//some css flip styles here
}

而 JQuery 代码将是这样的,

$(document).ready(function(){
$(window).scroll(function(){
if ($(window).scrollTop() > 100){
    $('.fixed-child').addClass( "rotate");
 }elseif($(window).scrollTop() > 200){
    $('.fixed-child').addClass( "flip");
  }
 });
});

现在我需要在 section-1 上的图像上添加旋转类,并在使用 jquery 在 section-2 上删除旋转类并添加翻转类,

我搜索了那个,我找到了一些例子但是使用这些例子我可以添加/删除基于 jquery scrollTop() 方法的类,但是我希望 jquery 检测那个类并在我向下滚动和向上滚动副时添加相应的类 -反之亦然。

我没有编写旋转和翻转 CSS 代码来减少此处的行数。但那些翻转和旋转工作。

请帮我实现这种风格!

最佳答案

根据我从您的问题/评论中收集到的信息,您需要像 Waypoints 这样的东西

Waypoints is the easiest way to trigger a function when you scroll to an element.

关于javascript - 使用 JQuery 添加/删除基于 div/section 更改的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40722338/

相关文章:

javascript - 使用javascript和jquery从外部文件div填充div

java - 尝试解析被javascript隐藏的html

javascript - jQuery - 在自定义函数中使用 $(this)

javascript - 使用 Greasemonkey 提交表单

javascript - 提交表单数据的 toggle() div 元素

javascript - jquery 交换元素上的图像单击不起作用

jquery - 更改 SELECT 框中的文本颜色

php - 选中复选框时jquery显示按钮

javascript - 将两个选择器与一个 jQuery 对象结合起来

javascript - 根据子元素的值对 div 进行排序