whatisthis?

practice - css ) μŠ¬λΌμ΄λ“œ νŽ˜μ΄μ§€ - CSS only (JS❌) λ³Έλ¬Έ

PRACTICE/SELF

practice - css ) μŠ¬λΌμ΄λ“œ νŽ˜μ΄μ§€ - CSS only (JS❌)

thisisyjin 2022. 3. 11. 16:48

Slide Page

- only CSS

- without JS

 

 

πŸ“ƒ μ‹€μŠ΅ν•˜λ©΄μ„œ μ•Œκ²Œλœ 점 기둝

여지껏 js둜 λ³΅μž‘ν•˜κ²Œ ν•˜λ˜ 방식 λŒ€μ‹ , jsλ₯Ό ν•˜λ‚˜λ„ μ•ˆμ“°κ³ 
μ˜€λ‘œμ§€ css와 :checked μ„ νƒμž 만으둜 μŠ¬λΌμ΄λ“œλ₯Ό κ΅¬ν˜„ν•  수 μžˆλ‹€κ³  ν•œλ‹€.


πŸ“Œ :checked
- CSS κ°€μƒν΄λž˜μŠ€
- μ„ νƒν–ˆκ±°λ‚˜ on μƒνƒœμΈ radio, checkbox, optionμš”μ†Œλ₯Ό λ‚˜νƒ€λƒ„

__

πŸ“Œ CSS sibling element κ΄€λ ¨ κ²°ν•©μž

~ : 이후 ν˜•μ œμš”μ†Œ 닀같이~ μ„ νƒν•˜λŠ” λŠλ‚Œ.
+ : λ°”λ‘œ λ‹€μŒμ— μœ„μΉ˜ν•˜λŠ” ν˜•μ œμš”μ†Œλ§Œ 선택.


>> λ°”λ‘œ λ‹€μŒ ν˜•μ œλ§Œ ν•„μš”ν•˜λ©΄ + / κ·Έ μ™Έκ°€ ν•„μš”ν•˜λ©΄ ~ μ“°κΈ°. 

p ~ span {
  color: red;
}​
<span>이건 빨강이 μ•„λ‹™λ‹ˆλ‹€.</span>
<p>μ—¬κΈ° 문단이 μžˆμŠ΅λ‹ˆλ‹€.</p>
<code>그리고 μ½”λ“œλ„ μžˆμŠ΅λ‹ˆλ‹€.</code>
<span>이제 λΉ¨κ°•μž…λ‹ˆλ‹€!</span>
<code>더 λ§Žμ€ μ½”λ“œκ°€ μžˆμŠ΅λ‹ˆλ‹€.</code>
<span>이것도 λΉ¨κ°•μž…λ‹ˆλ‹€!</span>
cr: mdn
πŸ“Œ https://developer.mozilla.org/ko/docs/Web/CSS/General_sibling_combinator

 

 

 

ver 1 - κ°€λ‘œ μŠ¬λΌμ΄λ“œ (κΈ°λ³Έ)

 

app.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Slide Banner</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="slide">
      <input type="radio" name="pos" id="pos1" checked />
      <input type="radio" name="pos" id="pos2" />
      <input type="radio" name="pos" id="pos3" />
      <input type="radio" name="pos" id="pos4" />
      <ul class="outer">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <p class="bullet">
        <label for="pos1">1</label>
        <label for="pos2">2</label>
        <label for="pos3">3</label>
        <label for="pos4">4</label>
      </p>
    </div>
  </body>
</html>

 

style.css 

/* colors
#fbf8cc
#f1c0e8
#cfbaf0
#a3c4f3
*/

* {
  margin: 0;
  padding: 0;
}

ul,
li {
  list-style: none;
}

.slide {
  height: 300px;
  overflow: hidden;
  position: relative;
}

.slide .outer {
  width: calc(100% * 4);
  display: flex;
  transition: all 1s ease-out;
}

.slide li {
  width: calc(100% / 4);
  height: 300px;
}

.slide li:nth-child(1) {
  background-color: #fbf8cc;
}
.slide li:nth-child(2) {
  background-color: #f1c0e8;
}
.slide li:nth-child(3) {
  background-color: #cfbaf0;
}
.slide li:nth-child(4) {
  background-color: #a3c4f3;
}

.slide input {
  display: none;
}

.slide .bullet {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  z-index: 10;
}

.slide .bullet label {
  cursor: pointer;
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid #666;
  font-size: 0;
  background-color: #fff;
  transition: all 0.5s ease-in-out;
}

/* radio μ²΄ν¬μ‹œ - λ§ˆμ§„ 적용 */

#pos1:checked ~ .outer {
  margin-left: 0;
}

#pos2:checked ~ .outer {
  margin-left: -100%;
}

#pos3:checked ~ .outer {
  margin-left: -200%;
}

#pos4:checked ~ .outer {
  margin-left: -300%;
}

/* bullet μ•‘ν‹°λΈŒ ν‘œμ‹œ */

#pos1:checked ~ .bullet label:nth-child(1),
#pos2:checked ~ .bullet label:nth-child(2),
#pos3:checked ~ .bullet label:nth-child(3),
#pos4:checked ~ .bullet label:nth-child(4) {
  background-color: #666;
}

 

 

Slide Banner

 

 


 

 

ver 2 - κ°€λ‘œ νŽ˜μ΄λ“œ (opacity 이용)

 

style.css

/* colors
#fbf8cc
#f1c0e8
#cfbaf0
#a3c4f3
*/

* {
  margin: 0;
  padding: 0;
}

ul,
li {
  list-style: none;
}

.slide {
  height: 300px;
  overflow: hidden;
  position: relative;
}

.slide li {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  opacity: 0;
  transition: all 1s ease-out;
}

.slide li:nth-child(1) {
  background-color: #fbf8cc;
}
.slide li:nth-child(2) {
  background-color: #f1c0e8;
}
.slide li:nth-child(3) {
  background-color: #cfbaf0;
}
.slide li:nth-child(4) {
  background-color: #a3c4f3;
}

.slide input {
  display: none;
}

.slide .bullet {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  z-index: 10;
}

.slide .bullet label {
  cursor: pointer;
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid #666;
  font-size: 0;
  background-color: #fff;
  transition: all 0.5s ease-in-out;
}

/* radio μ²΄ν¬μ‹œ - opacity 1둜 */

#pos1:checked ~ ul li:nth-child(1),
#pos2:checked ~ ul li:nth-child(2),
#pos3:checked ~ ul li:nth-child(3),
#pos4:checked ~ ul li:nth-child(4) {
  opacity: 1;
}

/* bullet μ•‘ν‹°λΈŒ ν‘œμ‹œ */

#pos1:checked ~ .bullet label:nth-child(1),
#pos2:checked ~ .bullet label:nth-child(2),
#pos3:checked ~ .bullet label:nth-child(3),
#pos4:checked ~ .bullet label:nth-child(4) {
  background-color: #666;
}

 

 

Slide Banner

 

 


 

 

result

 

 

 

 

 

 

ref >

http://junil-hwang.com