1

大家好,我正在制作一个粘性订阅框,如下面的网站

https://generalassemb.ly/

我用了下面的js

$.fn.is_on_screen = function(){
    var win = $(window);
    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
$(window).scroll(function(){ // bind window scroll event
    if( $('#foot').length > 0 ) { // if target element exists in DOM
        if( $('#foot').is_on_screen() ) { // if target element is visible on screen after DOM loaded
            $('.subscribeBox').fadeOut(1000);
        } else {
            $('.subscribeBox').fadeIn(1000);
        }
    }       
});

但问题是我必须再添加一项功能,当滚动量大于 200 时,订阅框变得可见,其他虎钳隐藏请给我一些建议如何做到这一点。

4

1 回答 1

1

经过大量尝试,我找到了解决方案……从一个愚蠢的错误中……

HTML 是

<header id="head">
  <p>Fixed Naviagtion</p>
</header>
<div class="content">
   <p>Some Content</p>
</div>
<footer id="footer">
   <p>Footer Contents</p>
</footer>
<div class="subscribeBox">
  <p>Subscribe me</p>
</div>

这是纯js代码..

$.fn.is_on_screen = function(){
    var win = $(window);
    var viewport = {top : win.scrollTop(),left : win.scrollLeft()};
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();
    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();
    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
$(window).scroll(function() {
    if ($(window).scrollTop() >  $('#head').height() ) {
        if( $('#foot').length > 0 ) { 
          if( $('#foot').is_on_screen() ) { 
              $('.subscribeBox').fadeOut('slow');
          }else {$('.subscribeBox').fadeIn('slow');}      
        }}
    else{$('.subscribeBox').fadeOut('slow');}           
});
于 2014-02-06T11:43:49.233 回答