JQuery渐入窗口滚动效果的目标元素的问题
Trouble with targetting elements for the JQuery fade-in on window scroll effect
我正在尝试应用元素淡出的效果,当页面向下滚动到一个水平,他们将是可见的。
更具体地说,我试图在垂直时间轴上瞄准盒子。
这是时间线的结构。
<div class="container-fluid when">
<div class="container none">
<div class="title dark">When?</div>
<div class="timeline">
<div class="container none">
<!-- BOX START -->
<div class="entry fade">
<div class="container-fluid none date-title">
<div class="col-md-6 none">The Pledge</div>
<div class="col-md-6 none">November 23rd 2011</div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum inventore repellat omnis esse accusantium, distinctio vel fugit provident quae possimus culpa magni deleniti est, aperiam illo exercitationem dolore, assumenda. Quis.</p>
<div class="circle-glyph img-circle"><span class="fa fa-envelope"></span></div>
</div>
<!-- /BOX END-->
</div>
</div>
</div>
</div>
以上大部分元素的CSS样式如下:
.when{
@extend .what;
background:#393F43;
color:#ddd;
.title{
color:#ddd;
}
.timeline{
padding:20px;
position:relative;
color:black;
&:before{
content: "";
width:5px;
height:100%;
background:silver;
position: absolute;
top:0;
left:50%;
}
.entry{
padding:20px;
border-radius:2px;
background:#ecf0f1;
width:auto;
max-width:500px;
color:gray;
position: relative;
margin-top:20px;
p{font-size:14px;}
&:before{
content: "";
position:absolute;
right:0;
padding:5px;
left:500px;
width:35px;
height:35px;
background: url(../imgs/arrow.png) no-repeat;
}
.circle-glyph{
background:silver;
padding:15px;
width:45px;
position:absolute;
left:545px;
top:10px;
span{
display:block;
text-align:center;
}
}
.date-title{
height:23px;
div{
&:nth-child(1){
font-size:15px;
font-weight: bold;
}
&:nth-child(2){
text-align:right;
font-style:italic;
font-size:11px;
}
}
}
&:not(:first-child):nth-child(odd){
margin-top:210px;
}
&:nth-child(even){
float:right;
.circle-glyph{
left:-125px;
top:10px;
}
&:before{
content: "";
position:absolute;
right:0;
padding:5px;
left:-35px;
width:35px;
height:35px;
background: url(../imgs/arrow.png) no-repeat;
-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: "FlipH";
}
}
}
}
}
生成以下内容:http://piclair.com/data/4xkqg.jpg
当前在我的script.js
文件中,我有以下代码在文档加载时执行:
$(window).scroll(function () {
/* Check the location of each desired element */
$('.fade').each(function (i) {
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if (bottom_of_window > bottom_of_object) {
$(this).animate({
'opacity': '1'
}, 350);
}
});
});
^我没有创建上面的脚本。
我已经测试过了,它在独立元素上工作得很好,例如,如果我将它应用于:<div class="bg-success fade pad-20">Hello</div>
无论我对多少个对象应用这个效果,它都能单独工作。
我遇到的问题是,一旦一个被script.js
<div class="entry fade">
我猜这与我如何嵌套不同的div,类和元素有关-但我不太确定。
非常感谢您的帮助
我遇到的问题是,褪色效果被触发为每一个单独的
<div class="entry fade">
,只要一个是由脚本在script.js检测到。
也许你的意思是他们都在同一时间消失。我做了一个脚本,当div的底部击中我的例子中的某个标记时,它们会逐渐消失。我不知道这是不是你想要的。我刚把position
改成了offset
。
$(window).on("scroll", function(){
$(".el").each(function(i){
console.log($(this).offset().top)
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
$('.display').html("btofWin: " + bottom_of_window + "btmofObj" + bottom_of_object)
if(bottom_of_window > bottom_of_object){
$(this).animate({
"opacity" : 1
},350)
}
})
})
http://jsfiddle.net/8s1cscq5/我正在学习滚动事件。
相关文章:
- 内容以其开头的目标元素
- 在 Jquery 中获取目标元素的属性
- 使用next()的jQuery目标元素不起作用
- 无法让悬停事件使用 jQuery UI 持续时间参数或 CSS 处理目标元素的同级元素
- 在 jquery not 选择器中具有 2 个类的目标元素
- 目标元素边缘的触摸启动会导致鼠标按下
- 具有 ID AND 类的 jQuery 选择器目标元素不起作用
- 如何在结构中获取目标元素的属性值.js
- 获取将被鼠标悬停的目标元素
- om/react:操作呈现目标元素之外的元素
- 具有不同 id 的目标元素 Jquery
- 如何动态更改基于目标元素的绝对工具提示的位置
- 目标元素由 attr 然后附加类 jQuery
- 多个语义 UI 弹出窗口,其中目标元素在每个实例的 atribute 中定义
- 对 HTML 列表进行排序,然后对目标元素进行排序
- 无法获取目标元素
- TinyMCE 编辑器中的目标元素
- 脚本 600:此操作的目标元素无效.(仅限 IE)
- 突出显示定位点的目标元素内的元素
- 单击即可动态更改目标元素