แผง Flex ใน CSS และ JS
ฉันติดตามความท้าทาย 30 วันของ Wes Bos JS ดังนั้น HTML และ CSS ส่วนใหญ่จึงเป็นการคัดลอกวางฉันต้องการความคิดเห็นเกี่ยวกับ JS (ส่วนใหญ่) ขอบคุณ.
const panels = document.querySelectorAll('.panel');
panels.forEach(panel => panel.addEventListener('click', () => {
const isOpen = panel.classList.contains('open');
panels.forEach(panel => panel.classList.remove('open'));
if(!isOpen) {
panel.classList.add('open');
}
}));
panels.forEach(panel => panel.addEventListener('transitionend', e => {
if(e.propertyName.includes('flex')) {
panels.forEach(panel => {
if(panel.classList.contains('open')) {
panel.classList.add('open-active');
} else {
panel.classList.remove('open-active');
}
});
}
}));
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'helvetica neue';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
color: white;
text-align: center;
align-items: center;
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
transition:
font-size 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
justify-content: center;
flex-direction: column;
}
.panel1 {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel2 {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
.panel3 {
background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}
.panel4 {
background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}
.panel5 {
background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}
/* Flex Children */
.panel>* {
margin: 0;
width: 100%;
transition: transform 0.5s;
flex: 1 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.panel *:first-child {
transform: translateY(-100%);
}
.panel *:last-child {
transform: translateY(100%);
}
.panel.open-active *:first-child,
.panel.open-active *:last-child {
transform: translateY(0);
}
.panel p {
text-transform: uppercase;
font-family: 'Amatic SC', cursive;
text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
font-size: 2em;
}
.panel p:nth-child(2) {
font-size: 4em;
}
.panel.open {
font-size: 40px;
flex: 5;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Panels 💪</title>
<link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet" type="text/css">
</head>
<body>
<div class="panels">
<div class="panel panel1">
<p>Hey</p>
<p>Let's</p>
<p>Dance</p>
</div>
<div class="panel panel2">
<p>Give</p>
<p>Take</p>
<p>Receive</p>
</div>
<div class="panel panel3">
<p>Experience</p>
<p>It</p>
<p>Today</p>
</div>
<div class="panel panel4">
<p>Give</p>
<p>All</p>
<p>You can</p>
</div>
<div class="panel panel5">
<p>Life</p>
<p>In</p>
<p>Motion</p>
</div>
</div>
</body>
</html>
คำตอบ
มันดูดีสำหรับฉัน ฉันเห็นบางสิ่งที่ควรพิจารณาเท่านั้น:
ตรวจสอบ propertyName แม่นยำมากขึ้นคุณจะได้รับif(e.propertyName.includes('flex')) {เนื่องจากการใช้ Safari flexและอื่น ๆ flex-growใช้ คุณแน่ใจหรือไม่ว่าflexสตริงย่อยจะไม่ปรากฏในการเปลี่ยน CSS อื่น ๆ ที่เป็นไปได้? แม้ว่าคุณจะแน่ใจ แต่ผู้อ่านโค้ดจะแน่ใจหรือไม่? ฉันจะเปลี่ยนเป็นการ===ทดสอบกับความเป็นไปได้ทั้งสองอย่างหรืออย่างน้อยก็ใช้startsWith(ซึ่งเหมาะสมกว่า.includesที่นี่เล็กน้อยเนื่องจากความเป็นไปได้ทั้งสองเริ่มต้นด้วยflex)
คุณยังสามารถย้ายความคิดเห็นเกี่ยวกับชื่อเหตุการณ์การเปลี่ยนแปลงไปยัง JS ได้เช่นเดียวกับ CSS
การตั้งค่า classList แบบกระชับเมื่อคุณต้องการเพิ่มชื่อคลาสหรือลบชื่อคลาสโดยยึดตามเงื่อนไขคุณสามารถย่อif(...) classList.add(...) else(...) classList.removeเป็น single classList.toggleโดยมีอาร์กิวเมนต์ที่สองที่ระบุว่าจะเพิ่มหรือลบคลาส ของคุณ
if(panel.classList.contains('open')) {
panel.classList.add('open-active');
} else {
panel.classList.remove('open-active');
}
ลดความซับซ้อนเป็น
const { classList } = panel;
classList.toggle('open-active', classList.contains('open'));
ความเข้ากันได้ของเบราว์เซอร์แม้ว่าเบราว์เซอร์โบราณบางตัวจะไม่รองรับอาร์กิวเมนต์ที่ 2 ดังนั้นให้พิจารณาว่าคุณต้องรองรับเบราว์เซอร์ประเภทใด หากคุณต้องการรองรับเบราว์เซอร์ที่ทันสมัยพอสมควรก็ทำได้ดี สิ่งที่ควรทราบอีกประการหนึ่งNodeList.prototype.forEachคือเพิ่งเปิดตัวเมื่อไม่กี่ปีที่ผ่านมาในช่วงปี 2559 หรือ 2560 IIRC เช่นstartsWithใหม่กว่า ES6 ดังนั้นให้ใช้ polyfill หรือใช้ iterators และ Babel แทนเช่น:
for (const panel of panels) {
// do stuff with panel
(หากคุณต้องการรองรับ IE คุณควรใช้ Babel ต่อไปเพื่อโอนรหัสของคุณเป็นไวยากรณ์ ES5)
ผลตอบแทนเป็นโมฆะ? panels.forEach(panel => panel.addEventListenerผลตอบแทนที่คุ้มค่าของการโทรไปยังผู้โทรของaddEventListener forEachเนื่องจากforEachไม่ได้ดูว่าการเรียกกลับของมันกลับมาอย่างไรจึงไม่ได้ทำอะไรเลย ไม่ใช่ปัญหาที่แท้จริง แต่บางคนอาจพิจารณาโค้ดเพื่อให้สมเหตุสมผลขึ้นหากการforEachโทรกลับส่งคืนเป็นโมฆะ (ไม่มีreturnคำสั่งหรือผลตอบแทนโดยปริยายเลย) (อธิบายไว้ใน TSLint ของ TypeScript ที่นี่ )
แผงที่คลิกได้เนื่องจากแผงสามารถคลิกได้อาจเปลี่ยนจากเคอร์เซอร์เริ่มต้นcursor: pointerเพื่อให้ผู้ใช้เห็นได้ชัดเจนยิ่งขึ้นว่าต้องการให้คลิกหรือไม่
ช่องว่างระหว่างองค์ประกอบในตัวเลือกฉันเปลี่ยน.panel>*ไป.panel > *- มันทำให้บิตให้อ่านง่ายขึ้นเมื่อแยกองค์ประกอบจะถูกคั่นด้วยช่องว่าง
แผงซ้ำ ๆมากกว่า
<div class="panel panel1">
</div>
<div class="panel panel2">
</div>
.panel1 {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel2 {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
ลองใช้:nth-childแทนเพื่อให้คุณสามารถลบpanel#คลาสพิเศษทั้งหมดได้
.panel:nth-child(1) {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel:nth-child(2) {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
const panels = document.querySelectorAll('.panel');
panels.forEach((panel) => {
panel.addEventListener('click', () => {
const isOpen = panel.classList.contains('open');
panels.forEach(panel => panel.classList.remove('open'));
if (!isOpen) {
panel.classList.add('open');
}
});
});
panels.forEach((panel) => {
panel.addEventListener('transitionend', e => {
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
if (e.propertyName === 'flex' || e.propertyName === 'flex-grow') {
panels.forEach(panel => {
const { classList } = panel;
classList.toggle('open-active', classList.contains('open'));
});
}
})
});
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'helvetica neue';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
color: white;
text-align: center;
align-items: center;
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
transition:
font-size 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
justify-content: center;
flex-direction: column;
cursor: pointer;
}
.panel:nth-child(1) {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel:nth-child(2) {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
.panel:nth-child(3) {
background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}
.panel:nth-child(4) {
background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}
.panel:nth-child(5) {
background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}
/* Flex Children */
.panel > * {
margin: 0;
width: 100%;
transition: transform 0.5s;
flex: 1 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.panel *:first-child {
transform: translateY(-100%);
}
.panel *:last-child {
transform: translateY(100%);
}
.panel.open-active *:first-child,
.panel.open-active *:last-child {
transform: translateY(0);
}
.panel p {
text-transform: uppercase;
font-family: 'Amatic SC', cursive;
text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
font-size: 2em;
}
.panel p:nth-child(2) {
font-size: 4em;
}
.panel.open {
font-size: 40px;
flex: 5;
}
<link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet" type="text/css">
<div class="panels">
<div class="panel">
<p>Hey</p>
<p>Let's</p>
<p>Dance</p>
</div>
<div class="panel">
<p>Give</p>
<p>Take</p>
<p>Receive</p>
</div>
<div class="panel">
<p>Experience</p>
<p>It</p>
<p>Today</p>
</div>
<div class="panel">
<p>Give</p>
<p>All</p>
<p>You can</p>
</div>
<div class="panel">
<p>Life</p>
<p>In</p>
<p>Motion</p>
</div>
</div>
ทบทวน
ฉันเห็นด้วยกับคำตอบของ CertainPerformance: รหัสดูดีทีเดียว การเยื้องดูเหมือนจะสอดคล้องกันชื่อตัวแปรมีความเหมาะสมและมีการสิ้นสุดบรรทัดด้วย ความสามารถในการอ่านดีมาก
ลูปที่ไม่มีประสิทธิภาพ
รหัสในตัวจัดการเหตุการณ์จะวนซ้ำอยู่บนองค์ประกอบของแผงทั้งหมด แต่อย่างน้อยสององค์ประกอบที่จะมีการอัปเดตรายการชั้นเรียน ดูคำแนะนำด้านล่างเกี่ยวกับวิธีกำจัดลูป
กฎ CSS ส่วนเกิน
font-size: 20px;ไม่จำเป็นต้องใช้สไตล์ภายใต้.panelเนื่องจากมีการระบุกฎเดียวกันhtmlรวมทั้งจะถูกแทนที่โดยตัวเลือกที่เฉพาะเจาะจงมากขึ้น
ข้อเสนอแนะ
ลบลูปโดยใช้การมอบหมายเหตุการณ์
แทนที่จะเพิ่มผู้ฟังเหตุการณ์ให้กับองค์ประกอบแผงแต่ละรายการคุณสามารถเพิ่มผู้ฟังเหตุการณ์ลงในองค์ประกอบคอนเทนเนอร์ได้ สิ่งนี้จะต้องมีการเปลี่ยนตัวจัดการเหตุการณ์เพื่อดูเป้าหมายเหตุการณ์และพิจารณาว่าเป้าหมายตรงกับพาเนลหรือชายด์ของพาเนลหรือไม่ - สามารถทำได้ด้วย.closest()เมธอด และสดHTMLCollectionขององค์ประกอบที่มีชื่อชั้นอาจจะเป็นความจริงครั้งเดียวโดยใช้open document.getElementsByClassName('open');ถ้าองค์ประกอบใดมีคลาสนั้นเมื่อตัวจัดการการคลิกเรียกชื่อคลาสสามารถลบออกได้
สิ่งนี้จะช่วยให้สามารถเพิ่มและลบพาเนลได้โดยไม่จำเป็นต้องลงทะเบียนตัวจัดการเหตุการณ์บนแผงเหล่านั้น แม้ว่าจะไม่สามารถสร้างความแตกต่างได้อย่างชัดเจนในหน้าเล็ก ๆ เช่นนี้ แต่ก็ควรพิจารณาสถานที่ที่สามารถหลีกเลี่ยงการวนซ้ำได้
ในข้อมูลโค้ดด้านล่างการวนซ้ำถูกตัดออกไปแล้ว
const panelsContainer = document.querySelector('.panels');
const openPanels = document.getElementsByClassName('open');
const openActivePanels = document.getElementsByClassName('open-active');
panelsContainer.addEventListener('click', e => {
const panel = e.target.closest('.panel');
if (!panel) {
return;
}
const isOpen = panel.classList.contains('open');
if (openPanels.length) {
openPanels[0].classList.remove('open');
}
panel.classList.toggle('open', !isOpen);
});
panelsContainer.addEventListener('transitionend', e => {
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
if (e.propertyName === 'flex' || e.propertyName === 'flex-grow') {
if (openActivePanels.length) {
openActivePanels[0].classList.toggle('open-active', openActivePanels[0].classList.contains('open'))
}
if (openPanels.length) {
openPanels[0].classList.add('open-active')
}
}
})
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'helvetica neue';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
color: white;
text-align: center;
align-items: center;
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
transition: font-size 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
justify-content: center;
flex-direction: column;
}
.panel1 {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel2 {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
.panel3 {
background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}
.panel4 {
background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}
.panel5 {
background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}
/* Flex Children */
.panel>* {
margin: 0;
width: 100%;
transition: transform 0.5s;
flex: 1 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.panel *:first-child {
transform: translateY(-100%);
}
.panel *:last-child {
transform: translateY(100%);
}
.panel.open-active *:first-child,
.panel.open-active *:last-child {
transform: translateY(0);
}
.panel p {
text-transform: uppercase;
font-family: 'Amatic SC', cursive;
text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
font-size: 2em;
}
.panel p:nth-child(2) {
font-size: 4em;
}
.panel.open {
font-size: 40px;
flex: 5;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flex Panels 💪</title>
<link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet" type="text/css">
</head>
<body>
<div class="panels">
<div class="panel panel1">
<p>Hey</p>
<p>Let's</p>
<p>Dance</p>
</div>
<div class="panel panel2">
<p>Give</p>
<p>Take</p>
<p>Receive</p>
</div>
<div class="panel panel3">
<p>Experience</p>
<p>It</p>
<p>Today</p>
</div>
<div class="panel panel4">
<p>Give</p>
<p>All</p>
<p>You can</p>
</div>
<div class="panel panel5">
<p>Life</p>
<p>In</p>
<p>Motion</p>
</div>
</div>
</body>
</html>