2016-04-03 45 views
1

我想我的固定div停止滚动在我的网页上的某个点,但是,我找到的解决方案都没有实际运作正常,并做了我想要的...固定的Div停止在某些点jQuery

这是我目前的jQuery:

var windw = this; 
$.fn.followTo = function (pos) { 
    var $this = this, $window = $(windw); 

    $window.scroll(function(e){ 
     if ($window.scrollTop() > pos) { 
      $this.css({ 
       position: 'absolute', 
       top: pos 
      }); 
     } else { 
      $this.css({ 
       position: 'fixed', 
       bottom: -10px 
      }); 
     } 
    }); 
}; 

$('#qF').followTo(800); 

这是我的HTML:

<div id="qF" class="central theater-dir-adown"> 
    <img src="data/img/prefs/dir_adown" class="dir-adown"> 
</div> 

这是我的CSS:

.theater-dir-adown{ 
    cursor: pointer; 
    display: inline-block; 
    position: fixed; 
    bottom: -10px; 
    left: calc(100%/2 - 51px); 
    width: calc(75%/9); 
    height: auto; 
} 

.dir-adown{ 
    width: 100%; 
} 

所以我想要的是停止滚动800px的div #qF,但我使用的代码不工作,div将继续向下滚动页面。我不确定在我的代码中是否有某种错误,但有人可以帮我解决吗?还是很新的jQuery的..

感谢

+0

滚动删除';''从底部:-10px;' –

+0

@NirmalyaGhosh没有工作,抱歉地说:(仍无法工作,因为我t应该 –

+0

从底部移除'px':-10px'它会抛出错误'Uncaught SyntaxError:Unexpected identifier'。工作:http://output.jsbin.com/latuji –

回答

1

添加顶部:设置位置时, '自动' 固定

var windw = this; 
$.fn.followTo = function (pos) { 
    var $this = this, $window = $(windw); 

    $window.scroll(function(e){ 
     if ($window.scrollTop() > pos) { 
      $this.css({ 
       position: 'absolute', 
       top: pos 
      }); 
     } else { 
      $this.css({ 
       position: 'fixed', 
       bottom: -10, 
       top:'auto' 
      }); 
     } 
    }); 
}; 

$('#qF').followTo(800); 

您可以添加顶部:POS + $(窗口).height()来从开始底部

http://jsbin.com/fuzutaxiha/1/edit?html,css,js,output

+0

对不起,但没有工作:( –

+0

没有你看到的jsbin链接?!img被固定到底部直到滚动丰富的800像素的高度,然后它滚动到顶部,这不是你想要的吗? –

+0

JSBin工作正常,但是,它不是与我的代码一起工作 –