2017-12-03 72 views
-1

我有一个div,它有两个CTA按钮。我将在达到页面的90%或达到我的#footer div后隐藏该div。我这样做的原因是为了防止它干扰页脚。在滚动某个百分比之后隐藏div

我已经找到了一些代码,但它会隐藏在800px滚动不基于百分比后的div。

$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > 800) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

任何想法?

回答

0

隐藏这个div:

您需要获得90%的Math.round($(document).height() * 90/100)

var height = Math.round($(document).height() * 90/100); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > height) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

或达到我的#footer的DIV

您需要使用offset().top用于获取元素从窗口偏移:

var height = Math.round($('#footer').offset().top); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > height) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div> 
 
<div style="height:1000px;"></div> 
 
<div id="footer"></div>

+1

伟大的Pedram。为我工作。感谢您的回复。 –

+0

但是@佩德拉姆你做了相反的事情。到达页脚时我要隐藏底部菜单。 –

+0

@MohammadGhonchesefidi只需用'<'改变'>') –

0

你可以简单地做一些计算来获得你所需要的百分比:

var limit = parseInt(($(document).height() * 70)/100); 
 
/* here I took 70% of the height of the page*/ 
 

 
console.log(limit); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y < limit) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

0

在这里,我已经转化百分率它。更新PER值来更新配置。达到90%的页面后

//PER is Percentage value 
 
var PER = 90; 
 
var yInPixel, totalHeight; 
 

 
//If page if with dynamic height change totalHeight and yInPixel after Resize Event 
 
$(document).ready(function() { 
 
    totalHeight = $(this).height(); 
 
    yInPixel = totalHeight * (PER/100) - window.innerHeight; 
 
}); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 

 
    if (y > yInPixel) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

+0

这是表现非常糟糕。您将重做每个卷轴上的计算,这是无用的。把这个逻辑放在外面会更好。 –

+0

如果页面具有动态高度并在某些事件中进行更改,该怎么办? –

+0

'如果页面具有动态高度':但这里不是这种情况。所以这里不适合(当然,如果你的解决方案不符合你的解决方案!)或者你可能会考虑在你的答案中增加更多评论来解释它,以便你的答案比其他答案更好;) –