์กฐ๊ฑด
- btn์ด ์ฌ๋ฌ๊ฐ ์ผ ๋ btn ํด๋์ค๋ฅผ ๊ฐ์ง ์์๊ฐ ๋ชจ๋ fix.
- visual์ผ ๋๋ btn์ด ๋ณด์ด์ง ์์์ผ ํ๋ค.
- 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');
})