๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿฃ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');  
})

 

๋ฐ˜์‘ํ˜•