์Šคํฌ๋กค ์‹œ ์œˆ๋„์šฐ ํ•˜๋‹จ์— ๊ณ ์ •ํ•˜๊ธฐ

2021. 8. 25. 15:47ยท๐ŸฃcodeLab

 

์กฐ๊ฑด

  1. btn์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๋•Œ btn ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ๋ชจ๋‘ fix.
  2. visual์ผ ๋•Œ๋Š” btn์ด ๋ณด์ด์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  3. resize์—๋„ ๊ฐ€๋Šฅ.

 

์ผ๋‹จ ์ฝ”๋“œ!

<!-- ๋ชจ๋“  html ๋™์ผ -->
<div id="wrap">
	<header>header</header>
    <section>
    	<div class="visual">visual</div>
        <div class="content cnt01">
            cnt01
            <span class="btn btn01"></span>
        </div>
        <div class="content cnt02">
            cnt02
            <span class="btn btn02"></span>
        </div>
        <div class="content cnt03">
            cnt02
            <span class="btn btn03"></span>
        </div>
    </section>
    <footer>footer</footer>
</div>
/* ๋ชจ๋“  css ๋™์ผ */
* { margin: 0; padding: 0; box-sizing: border-box; }
.wrap { position: relative; width: 100%; height: 100%; }
header,
footer { width: 100%; height: 70px; position: relative; background-color: #eaeaea; }
footer { height: 200px; }

section { position: relative; }
.visual { width: 100%; height: calc(100vh - 70px); min-height: 600px; background-color: plum; }
.content { position: relative; width: 100%; height: 700px; background-color: powderblue; }
.cnt02 { background-color: palegreen; height: 1000px; }
.cnt03 { background-color: palevioletred; height: 600px; }

.btn { display: block; position: absolute; bottom: 0; right: 20px; z-index: 1; width: 50px; height: 50px; }
.btn01 {  background-color: tomato; }
.btn02 { border-radius: 50px; background-color: #222; }
.btn03 { background-color: gold; }

.sticky { position: fixed; opacity: 0.5; }

 

์ „์— ์ฐธ๊ณ ๋กœ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๋‹ค์‹œ ๊ฐ€์ ธ์™€์„œ ํ–ˆ๋Š”๋ฐ๋„ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค. ๋ฌด์—‡๋ณด๋‹ค footer๊ฐ€ content์˜ ๋†’์ด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด fix๊ฐ€ ๋˜์ง€์•Š์•„ ๋†’์ด์˜ ์ˆ˜์ •์— ์œ ์—ฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

// jQuery
$(function() {
    $(window).on('scroll', function() {
        var wTop = $(window).scrollTop();
        if(wTop < $(document).height() - $(window).height() - $('.cnt01').height() - $('.cnt02').height() ) {
            $('.btn01').addClass('sticky');
        } else {
            $('.btn01').removeClass('sticky');
        }
        
        if(wTop < $(document).height() - $(window).height() - $('footer').height() ) {
            $('.btn02').addClass('sticky');
        } else {
            $('.btn02').removeClass('sticky');
        }
    }).trigger('scroll');
})

 

๋‹ค์‹œ ์ฝ”๋“œ ์ž‘์„ฑ.

// jQuery
function scrollSticky($fixed, $remove) {
    var _this = $($fixed).offset().top + $($fixed).height();
    var _remove = $($remove).offset().top + $($remove).height();
    
    $(window).on('scroll resize', function() {
        var wTop = $(window).scrollTop();
        var wH = $(window).height();
		
        // ์กฐ๊ฑด1
        if(wTop + wH <= _this) {
            $($fixed).addClass('sticky');
        } else {
            $($fixed).removeClass('stikcy');
        }
        
        // ์กฐ๊ฑด2
        if(wTop + wH <= _remove) {
            $($fixed).css({visibility: 'hidden'});
        } else {
            $($fixed).css({visibility: 'visible'});
        }
    }).trigger('scroll', 'resize');
}

scrollSticky('.btn01', '.visual');
scrollSticky('.btn02', '.visual');

์œ„์˜ ์ฝ”๋“œ๋กœ ์ž‘๋™์ด ๋˜์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ btn์˜ ์ˆ˜๋งŒํผ ๋ถˆ๋Ÿฌ์•ผ ํ•œ๋‹ค.

 

 

๋‹ค์‹œ ์ฝ”๋“œ ์ •๋ฆฌ ์ž‘์„ฑ.

// jQuery
$(document).ready(function() {
    function eachSticky($fixed, $remove) {
        var _remove = $($remove).offset().top + $($remove).height();
        // ๊ฐ๊ฐ์˜ $fixed๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
        $($fixed).each(function() {
            var _this = $(this);
            var _thisH = _this.offset().top + _this.height();

            $(window).on('scroll resize', function() {
                var wTop = $(window).scrollTop();
                var wH = $(window).height();

                // ์กฐ๊ฑด1
                if(wTop + wH <= _thisH) {
                    _this.addClass('sticky');
                } else {
                    _this.removeClass('sticky');
                }

                // ์กฐ๊ฑด2
                if(wTop + wH <= _remove) {
                    $($fixed).css({visibility: 'hidden'})
                } else {
                    $($fixed).css({visibility: 'visible'});
                }
            }).trigger('scroll', 'resize');
            
            // ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰ํ•˜๋ ค๋ฉด return false
            // return false;
        })
    }
    eachSticky('.btn', '.visual');  
})

 

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋™์ผ์กฐ๊ฑด
'๐ŸฃcodeLab' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • swiper slide ์–‘ ์˜† (margin) ์ฃผ๊ธฐ
  • SVG ์ฐธ๊ณ 
  • darkmode ๋งŒ๋“ค์–ด๋ณด๊ธฐ
  • ๊ฐ€์ƒ์„ ํƒ์ž๋ฅผ ์‚ฌ์šฉํ•ด backgroud ๋„“์ด์™€ ๋†’์ด ์ ์šฉ
jmjm
jmjm
  • jmjm
    /* jmjmjm */
    jmjm
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (127)
      • ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ปSTUDY (83)
        • HTML (9)
        • CSS (24)
        • SASS (4)
        • JavaScript (15)
        • jQuery (9)
        • Publishing (8)
        • Git (11)
        • React (1)
        • Vue (0)
        • ์›น์ ‘๊ทผ์„ฑ (1)
        • Gulp (1)
      • ๐ŸฃcodeLab (17)
      • ๐ŸŒˆetc (9)
      • ๐Ÿ”ฅ TIL (16)
      • ๐Ÿฅฐ ์ผ์ƒ (2)
      • ๐ŸŒŸ์ฝ์–ด๋ณด๊ธฐ (0)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    sticky jsfiddle
    ์›น์ ‘๊ทผ์„ฑ
    gulp ์‹œ์ž‘ํ•˜๊ธฐ
    javascript htmlcollection nodelist
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ slideup
    sass compiler
    gulp ๋ฒ„์ „
    javascript slideup
    javascript slidedown
    ์ ‘๊ทผ์„ฑ ์ฃผ์˜
    HTML Form
    html collection
    javascript htmlcollection
    sticky codepen
    javascript ์ปฌ๋ ‰์…˜
    ์ ‘๊ทผ์„ฑ ์ฃผ์˜์‚ฌํ•ญ
    vscode ๋‹จ์ถ•ํ‚ค
    javascript nodelist
    ์•„์ฝ”๋””์–ธ๋ฉ”๋‰ด
    the legacy js api is deprecated and will be removed in dart sass 2.0.0
    CSS ์„ ํƒ์ž
    sass
    css flex
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ปฌ๋ ‰์…˜
    javascript accordion
    css ๊ฐ€์ƒ์„ ํƒ์ž
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ slidedown
    sticky ํ™œ์šฉ
    js ์•„์ฝ”๋””์–ธ
    javascript ์•„์ฝ”๋””์–ธ
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
jmjm
์Šคํฌ๋กค ์‹œ ์œˆ๋„์šฐ ํ•˜๋‹จ์— ๊ณ ์ •ํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”