2015-02-06 33 views
6

我不確定標題是否清楚,所以很少有解釋的字眼。我有幾個小元素,比如說div's(200px x 400px CONSTANT)。在他們的每一個裏面,都有一段包含大約20行文字的段落。 當然,這對於一個可憐的小格子來說很多。我想要做的是:CSS:如何獲得瀏覽器滾動條寬度? (用於:懸停{overflow:auto}好利潤)

  1. 通常divoverflow: hidden財產。
  2. 將鼠標移過(:hover)此屬性更改爲overflow: auto;,並出現滾動條。

什麼問題?我想在段落文本和滾動條之間留出一點空白(填充或邊距)。我們假設這個段落有一個對稱的margin: 20px;。但在觸發:hover之後,將出現滾動條,並將該段的整個右側移動到左側"scrollbar-width" px。其他行中的句子被打破,整個段落看起來不同,這是非常煩人的,而不是用戶友好的。我怎樣才能設置我的CSS,所以懸停後唯一的變化將是scroolbar的外觀?

換句話說:

/* Normally no scrollbar */ 
div {display: block; width: 400px; height: 200px; overflow: hidden;} 
/* On hover scrollbar appears */ 
div:hover {overflow: auto;} 

/* Paragraph margin predicted for future scrollbar on div */ 
div p {margin: 20px (20px + scrollbarwidth px) 20px 50px;} 
/* With scrollbar margin is symmetrical */ 
div:hover p {margin: 20px;} /* With scrollbar */ 

我在strong做了它一個小片段,與我的問題的exaplanation。我希望一切都清楚:)。我正在尋找一個近兩個小時的解決方案,所以我認爲我的問題非常獨特和有趣。

div.demo1 { 
 
    width: 400px; 
 
    height: 200px; 
 
    overflow: hidden; 
 
    background: #ddd; 
 
} 
 
div.demo1:hover { 
 
    overflow: auto; 
 
} 
 
div.demo1 p { 
 
    background: #eee; 
 
    margin: 20px; 
 
} 
 
div.demo2 { 
 
    width: 400px; 
 
    height: 200px; 
 
    overflow: hidden; 
 
    background: #ddd; 
 
} 
 
div.demo2:hover { 
 
    overflow: auto; 
 
} 
 
div.demo2 p { 
 
    background: #eee; 
 
    margin: 20px 40px 20px 20px; 
 
} 
 
div.demo2:hover p { 
 
    margin: 20px 20px 20px 20px; 
 
}
<div class="demo1"> 
 
    <p> 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    </p> 
 
</div> 
 
<br> 
 
<br> 
 
<strong>As you can see, on hover right side of the paragraph "moves" left. But I want something like:</strong> 
 
<br> 
 
<br> 
 
<div class="demo2"> 
 
    <p> 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. 
 
    </p> 
 
</div> 
 

 
<strong>But with a "perfect" scrollbar width (in this example I used 20px)</strong>

+0

順便說一句。我知道我可以硬編碼段落寬度並使用例如'350px',但這不是我想要的解決方案 - 它很醜並且假設滾動條的最大寬度僅爲50px:P。 Thx爲你的時間,最好的問候。 – 2015-02-06 07:56:19

+1

JS是唯一的選擇,因爲這樣可能的重複:http://stackoverflow.com/questions/13382516/getting-scroll-bar-width-using-javascript – SW4 2015-02-06 08:33:41

回答

8

所有的滾動條可以根據瀏覽器和操作系統的不同,所以你必須使用JavaScript。

我發現了一個很酷的片段在這裏:http://davidwalsh.name/detect-scrollbar-width

這個例:http://jsfiddle.net/a1m6an3u/

JS代碼創建一個div .scrollbar-measure,看看滾動條的大小和返回。

它看起來像這樣:

var scrollDiv = document.createElement("div"); 
scrollDiv.className = "scrollbar-measure"; 
document.body.appendChild(scrollDiv); 

// Get the scrollbar width 
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; 
console.warn(scrollbarWidth); // Mac: 15 

// Delete the DIV 
document.body.removeChild(scrollDiv); 

於是我趕緊加了一些jQuery代碼,但我認爲你可以只在JS做到這一點。

+0

你的答案真的很棒,從我+1。但是,如果您不介意我會等待幾個小時的接受,也許有人會用純粹的CSS解決方案。如果沒有,我當然會接受它。感謝您的時間,以及偉大的研究。 – 2015-02-06 08:37:20

+0

我試過這個代碼在Linux和Chrome和控制檯警告顯示0(零)...任何想法有什麼可能是錯的? – mtx 2017-07-12 19:00:06

+0

@mtx您是否爲您的滾動條測量類定義了CSS?它可能沒有飛過。 jsfiddle應該有所幫助。 – Koh 2017-09-05 18:24:41