2013-07-31 23 views
1

我有一個網站使用橫向滾動插件在https://github.com/brandonaaron/jquery-mousewheel上進行水平滾動。那裏沒有問題。使用水平滾動將絕對位置從絕對值更改爲固定值

我的問題是,我有一個250px寬的div,100%的高度從屏幕右側開始100px。當我滾動(水平)時,div向左滾動並最終從屏幕上消失。 div的Position設置爲Absolute。我想要的是一旦div擊中瀏覽器窗口的左側,div就會固定在屏幕的左側。

我猜我需要使用JS來更改div的CSS,一旦它到達屏幕上的特定位置(最左)。我見過不少網站通過垂直滾動來實現這一點,在屏幕頂部觸摸頂部時,菜單欄就會固定。菜單可能已經開始在頁面的中間並且在滾動上向上移動。我只是想在水平面上達到同樣的效果。

我對代碼一般都比較陌生,並且是全新的Javascript。我可能會咬掉更多,但我喜歡挑戰。感謝任何人的幫助。

的CSS代碼,我會改變是

#header { 
    width: 250px; 
    background: rgba(22,22,22,.85); 
    height: 100%; 
    position: absolute; 
    margin-left: calc(100% - 350px); 
    z-index: 999; 
} 

然後換

#header { 
    width: 250px; 
    background: rgba(22,22,22,.85); 
    height: 100%; 
    position: fixed; 
    margin-left: 0px; 
    z-index: 999; 
} 
+0

如果你提供了一些工作,你做了什麼,我的意思是代碼..話,肯定我們可以幫助ü。這似乎沒有那麼大的概率,但ü需要向我們展示一些代碼,以便我們能夠處理它。 –

+0

Hi Kaushik。感謝您的迴應。我已經添加了我認爲需要在上述問題中進行更改的CSS。我希望這有幫助。 –

回答

0

這是我嘗試按我從你的問題理解... N啊,我有用戶的JavaScript ..

HTML:

<div id="headerSlideContainer"> 

    <div id="headerSlideContent"> 

     Header content goes here! 

    </div> 
    <div id="new"> 
     Other Contents 
    </div> 

</div> 

CSS:

#headerSlideContainer { 
    position: absolute; 

    top:0px; 
    width: 100%; 

    height:1000px; 
    background: black; 
} 
#headerSlideContent { 
    width: 250px; 
    background-color:red; 
    height: 100%; 
    position: fixed; 
    margin-left: 0px; 
    z-index: 999; 
} 

#new{ 
    top:60px; 
    z-index:2; 
    height:100px; 
    background:Orange; 
    position:absolute; 
    color: white; 

} 

和JS:

$(function() { 

    var bar = $('#headerSlideContainer'); 
    var top = bar.css('top'); 
    $(window).scroll(function() { 

     if($(this).scrollright() > 100) { 
      bar.stop().animate({'top' : '5px'}, 500); 
     } else { 
      bar.stop().animate({'top' : top}, 500); 
     } 
    }); 
}); 

變化是scrollright量需要。如果scrollright不工作,然後嘗試scrollleft()..讓我知道如果您有任何概率..

+0

感謝隊友。欣賞答案。我會讓你知道我是如何去的,如果它適合我​​。 –

+0

是啊肯定好友... :) –

+0

嘿夥計。我現在在玩它。查看這個鏈接,瞭解我正在使用的橫向卷軸。只是想確保我解釋得很好。 http://healthyselfie.com/photosite您可能需要在Chrome中加載它,因爲我現在只在玩這個遊戲,並且只能在那裏進行測試。 –

相關問題