2016-01-07 55 views
0

我有一個側欄,有時比主頁面大,我嘗試設置垂直溢出,但垂直滾動條仍然不出現。爲什麼垂直滾動條不會出現雖然設置了垂直溢出?

/** 
* Created by eduard on 07.01.2016. 
*/ 
console.log("Height 1 ", sidebar_height); 

if ($(window).height() < sidebar_height + 35) { 
    $('body').css('overflowY', 'auto'); 
    //$("#sidebar").css('overflowY', 'auto'); 
    console.log("Scrollbar should appear", $(window).height() - sidebar_height); 
} 
else { 
    console.log("Scrollbar should not appear", $(window).height() - sidebar_height); 
} 

爲什麼垂直滾動條不出現,儘管垂直型溢出設置?

+0

嘗試將其設置爲滾動,而不是汽車。 – Styphon

+0

@Styphon將它設置爲「滾動」仍然不會使滾動條出現 –

+0

您是否確實擁有固定的高度? – epascarello

回答

1

的值設置爲scroll應該做的伎倆:

$('body').css('overflowY', 'scroll'); 

它實質上迫使滾動條,無論溢出內容或不出現。

編輯 - 或者,嘗試添加一個類時,窗口的高度低於:

https://jsfiddle.net/0Lhwfjk6/

我稍微簡化了examle這種情況。您需要調整實際的瀏覽器窗口大小。

的jQuery:

/** 
* Created by eduard on 07.01.2016. 
*/ 
console.log("Height 1 ", sidebar_height); 

if ($(window).height() < 300) { 
    $('.content').addClass('scrollbar'); 
    //$("#sidebar").css('overflowY', 'auto'); 
    console.log("Scrollbar should appear", $(window).height() - sidebar_height); 
} 
else { 
    console.log("Scrollbar should not appear", $(window).height() - sidebar_height); 
} 

CSS:

.scrollbar { 
    overflow-y: scroll; 
} 
+0

請參閱上面的我的編輯。 –

相關問題