<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .fa { width: 200px; display: flex; justify-content: space-between; white-space: nowrap; overflow-y: hidden; overflow-x: scroll; } .son { padding: 12.8px 0 12.8px 0; margin-right: 19.2px; background: tomato; } .scrollBox { height: 500px; overflow-x: scroll; /* 內容會被裁剪,會以滾動條顯示 */ overflow-y: hidden; /* 超出內容不可見 */ white-space: nowrap; /* 不換行 */ /* 文本不會換行,會在同一行上繼續,直到遇到<br>爲止 */ } .blockDiv { width: 340px; margin-right: 10px; display: inline-table; background: tomato; height: 500px; /* 不換行 */ } </style> </head> <body> <div class="fa"> <div class="son">1111111</div> <div class="son">22222222</div> <div class="son">3333333333</div> <div class="son">4444444444</div> <div class="son">666666666</div> </div> <div class="scrollBox"> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> <div class="blockDiv"> </div> </div> </body> </html>
以上方法是錯的,實際上是經過white:space 僞實現(子盒子寬不寫死)html
下面是真正的實現(子盒子的寬固定)promise
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> * { margin: 0; padding: 0; } li { list-style: none; } .box1 { width: 320px; height: 60px; overflow: hidden; /* 超出隱藏滾動條 */ background-color: skyblue; } .box1 .wrap { width: 320px; /* 和父盒子寬度同樣 */ height: 76px; /* 比裏層元素高16px 爲了隱藏滾動條*/ overflow-x: scroll; /* 定義超出此盒子滾動 */ overflow-y: hidden; } .box1 .wrap ul { width: 640px; display: flex; } .box1 .wrap ul li { flex: 1; width: 60px; height: 60px; box-sizing: border-box; } </style> <body> <div class="box1"> <div class="wrap"> <ul> <li>1移動端</li> <li>2可滑動</li> <li>3ie8以上</li> <li>4</li> <li>5</li> </ul> </div> </div> </body> </html>
更加完美,flex適應子盒子async
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <style> * { margin: 0; padding: 0; } li { list-style: none; } .box1 { width: 320px; height: 60px; overflow: hidden; /* 超出隱藏滾動條 */ background-color: skyblue; } .box1 .wrap { width: 320px; /* 和父盒子寬度同樣 */ height: 76px; /* 比裏層元素高16px 爲了隱藏滾動條*/ overflow-x: scroll; /* 定義超出此盒子滾動 */ overflow-y: hidden; } .box1 .wrap ul { /* width: 640px; */ display: flex; } .box1 .wrap ul li { /* flex: 1; */ width: 80px; height: 60px; flex-shrink: 0; box-sizing: border-box; } </style> <body> <div class="box1"> <div class="wrap"> <ul> <li>1移動端</li> <li>2可滑動</li> <li>3ie8以上</li> <li>4</li> <li>5</li> </ul> </div> </div> <script> // let promise = new Promise(function (resolve, reject) { // // 當 promise 被構造完成時,自動執行此函數 // // 1 秒後發出工做已經被完成的信號,並帶有結果 "done" // setTimeout(() => { // console.log(1) // }, 1000) // resolve() // }) // promise.then(() => { // console.log(2) // }) async function a() { let b = await setTimeout(() => { console.log(1) }, 200) console.log(2) } a() </script> </body> </html>