2017-04-22 13 views
2

我想在不使用JS的情況下實現可調整大小的窗格,僅使用CSS Grid layout。這可能嗎?可以使用CSS網格佈局來實現編輯器窗格的大小調整(例如,在CodePen中)

例如, CodePen的編輯器爲其HTML/CSS/JS編輯器提供了可調整大小的窗格。另外,看下面的例子,它以普通的JS實現它(我似乎無法在其中添加一個URL到CodePen示例中,所以添加屬性有點困難,代碼是http://codepen.io/gsound/)。

let isResizing = false; 
 

 
let $handler = document.getElementById('handler'); 
 
let $wrapper = document.getElementById('wrapper'); 
 
let $left = document.getElementById('left'); 
 

 
$handler.addEventListener('mousedown', function(e){ 
 
    isResizing = true; 
 
}); 
 

 
document.addEventListener('mousemove', function(e){ 
 
    if(!isResizing) return; 
 
    let newWidth = e.clientX - $wrapper.offsetLeft; 
 
    $left.style.width = newWidth + 'px'; 
 
}); 
 

 
document.addEventListener('mouseup', function(){ 
 
    isResizing = false; 
 
});
html,body{ 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0; 
 
} 
 

 
#app{ 
 
    height: 100%; 
 
/* max-width: 1400px; */ 
 
    margin: 0 auto; 
 
} 
 

 
header{ 
 
    background-color: #AAA; 
 
    height: 50px; 
 
} 
 

 
#wrapper{ 
 
    margin: 0 auto; 
 
    height: calc(100% - 50px); 
 
    width: 100%; 
 
    background-color: #EEE; 
 
    display: flex; 
 
} 
 

 
#handler{ 
 
    background-color: red; 
 
    width: 5px; 
 
    cursor: col-resize; 
 
} 
 

 
#left{ 
 
    width: 200px; 
 
} 
 

 
#content{ 
 
    width: 100%; 
 
    flex: 1; 
 
} 
 

 

 
/* ----------- */ 
 

 
#left{ 
 
    background-color: yellow; 
 
} 
 

 
#content{ 
 
    background-color: #232378; 
 
}
<div id="app"> 
 
    <header>head</header> 
 
    <div id="wrapper"> 
 
    <aside id="left"></aside> 
 
    <div id="handler"></div> 
 
    <article id="content"></article> 
 
    </div> 
 
</div>

P.S作爲邊注,我已經重新實現的例如上述通過添加和刪除'mousemove''mouseup'事件,並想知道這是否是「更好」比使用一個布爾isResizing並保持事件監聽器一直存在(更好的性能)...

回答

1

網格佈局模塊是純CSS。

可調整大小的窗格通常需要JavaScript來運行。

瞭解CSS主要用於樣式和演示文稿,因此Grid Layout spec中很可能沒有內置屬性或方法,它們可以提供可手動調整大小的窗格。

相關問題