๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/JavaScript jQuery

[JavaScript / jQuery] position : sticky ํ™œ์šฉํ•˜๊ธฐ

by Connecting-the-dots 2022. 3. 9.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • position : sticky ๋ฅผ ์ด์šฉํ•ด์„œ ํ™”๋ฉด์˜ ์ขŒ์ธก์— ์œ„์น˜ํ•œ ํ…์ŠคํŠธ๋Š” ์Šคํฌ๋กค์„ ๋‚ด๋ฆฌ๋ฉด ์›€์ง์ด๊ณ , ์šฐ์ธก์˜ ์ด๋ฏธ์ง€๋Š” ๊ณ ์ •๋˜์–ด์žˆ๋Š” ๋…ํŠนํ•œ ๊ตฌ์กฐ์˜ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.

๐Ÿ’œ HTML

<div class="gray">
    <!-- ์™ผ์ชฝ: ํ…์ŠคํŠธ -->
    <div class="text-container">
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
        <div class="text">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. 
            Sapiente expedita obcaecati ducimus, at quia nam deserunt ad atque ex cupiditate illo 
            itaque laboriosam molestiae dignissimos molestias minima vitae, doloremque non.
        </div>
    </div>
    <!-- ์˜ค๋ฅธ์ชฝ : ์ด๋ฏธ์ง€ -->
    <div class="image">
        <img src="./img/appletv.jpg" width="100%">
    </div>
    <div style="clear:both;"></div>
</div>
  • ์ขŒ์ธก์€ ์•„๋ฌด์˜๋ฏธ ์—†๋Š” ํ…์ŠคํŠธ๋กœ ์ฑ„์›Œ์ฃผ๊ณ  ์˜ค๋ฅธ์ชฝ์—๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • float ์†์„ฑ์„ ์‚ฌ์šฉํ•  ๊ฑฐ๋ผ์„œ ์ค‘๊ฐ„์— clear : both ์†์„ฑ์ด ๋“ค์–ด๊ฐ„ div ํƒœ๊ทธ๋„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. 

๐Ÿ’œ CSS

.gray {
  background: rgb(218, 216, 216);
  height: 2550px;
  margin-top: 300px;
  width: 100%;
}

.text-container {
  float: left;
  width: 45%;
}

.text {
  margin-top: 150px;
}

.image {
  float: right;
  width: 600px;
  position: sticky;
  top: 400px;
}
  • ์ขŒ์ธก์— ํ•ด๋‹นํ•˜๋Š” text-container ํด๋ž˜์Šค๋Š” float : left ๋ฅผ ์ฃผ๊ณ , ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” image ํด๋ž˜์Šค๋Š” float : right ๋ฅผ ์ฃผ์—ˆ๋‹ค.
  • ๊ฐ๊ฐ ์ ๋‹นํžˆ ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•ด ๋น„์œจ์„ ๋งž์ถฐ์ฃผ๊ณ  image ํด๋ž˜์Šค์—๋งŒ position : sticky ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  top ์†์„ฑ์„ ํ†ตํ•ด ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ณ ์ •์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•