2012-06-26 23 views
0

我想確定一個特定的DIV總是從底部20px和從右邊20px的最佳方式,即使用戶滾動。固定應該做的伎倆jQuery:在滾動上,DIV總是在任何可見的底部右邊

<body> 
<div id="wrap"> 
<p>Some content</p> 
</div> 
<div class="social-badges"><!-- this is the box that will always be at the bottom right --></div> 
</body> 

$(window).scroll(function() { 
     console.log('scrolling'); 
     $(".tba-social-slider").css({"position" : "absolute", "bottom" : "20px", "right" : "20px"}); 
    }); 
+0

那麼你的代碼有什麼問題?你告訴我們你想要達到的目標,但不是目前正在發生的事情 –

回答

3

CSS位置:

.tba-social-slider{ 
    position: fixed; 
    bottom: 20px; 
    right: 20px; 
} 

沒有所需的JavaScript IMO。

+0

是的,這個不需要JavaScript。 –

+0

同意。感謝您擺脫蜘蛛網。 :) – dcolumbus

0

此代碼。

$(document).ready(function() { 

    var div = $('.social-badges'); 
    var start = $(div).offset().top; 

    $.event.add(window, "scroll", function() { 
     var p = $(window).scrollTop() + $(window).height(); 
     $(div).css('position',((p)>start) ? 'fixed' : 'static'); 
     $(div).css('top',((p)>start) ? '5px' : ''); 
    }); 
}); 

這應該有效,但不確定。 :)

+0

一些提示:'div'已經是一個jQuery對象,所以不需要'$(div)'。你也可以使用'.css(...).css(...)'或甚至'.css({...})'鏈接。 – pimvdb

+0

這不是我的代碼。我從過去使用過的另一個堆棧溢出中得到了這個結果。謝謝你。 :) – transparent