2016-04-03 46 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工作正常,但是,它不是與我的代碼一起工作 –