목차
Scrollspy 예제 - 수평
Scrollspy 예제 - 수직
Scrollspy 예제 - 수평
<style>
body {position: relative ;}
.container-fluid {padding-top:70px;padding-bottom:70px}
</style>
<body data-spy="scroll" data-target=".navbar" data-offset="50" >
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top ">
<ul class="navbar-nav ">
<li class="nav-item ">
<a class="nav-link " href="#section1 ">Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section3">Section 3</a>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle " href="#" id="navbardrop" data-toggle="dropdown" >
Section 4
</a>
<div class="dropdown-menu ">
<a class="dropdown-item " href="#section41">Link 1</a>
<a class="dropdown-item" href="#section42">Link 2</a>
</div>
</li>
</ul>
</nav>
<div id="section1 " class="container-fluid bg-success">
<h1>Section 1</h1>
<p>홈짱닷컴</p>
</div>
<div id="section2" class="container-fluid bg-warning">
<h1>Section 2</h1>
<p>Homzzang.com</p>
</div>
<div id="section3" class="container-fluid bg-secondary">
<h1>Section 3</h1>
<p>홈페이지제작 + 서버관리/p>
</div>
<div id="section41" class="container-fluid bg-danger">
<h1>Section 4 - Sub 1</h1>
<p>HTML CSS JS JQ BS</p>
</div>
<div id="section42" class="container-fluid bg-info">
<h1>Section 4 - Sub 2</h1>
<p>PHP SQL</p>
</div>
결과보기
[body 태그 속성]
data-spy="scroll"
"스크롤가능영역"으로 사용해야하는 요소에 추가. (보통, body 요소) ※ 해당 요소가 올바르게 작동하려면 position:relative 속성 필요.
메뉴 링크타겟(href="#section1 ")과 스크롤요소의 아이디(id="section1" )가 대응되는지 확인.
data-target=".navbar"
네비게이션바의 (class 또는 ID) 입력. 네이게이션바가 "스크롤가능영역"과 연결되었는지 확인.
data-offset="50"
스크롤 위치 계산할 때, 위에서 오프셋 할 픽셀수 지정. (기본값: 10)
스크롤요소로 이동할 때 메뉴바 링크 활성상태가 너무 빨리 바뀐다 여겨질 때 설정하면 유용.
Scrollspy 예제 - 수직
<style>
body {position: relative;}
ul.nav-pills {top: 20px;position: fixed;}
div.col-8 div {height: 500px;}
</style>
<body data-spy="scroll" data-target="#hz " data-offset="1" >
<div class="container-fluid">
<div class="row">
<nav class="col-sm-3 col-4" id="hz" >
<ul class="nav nav-pills flex-column ">
<li class="nav-item">
<a class="nav-link active" href="#section1">Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section3">Section 3</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#">Section 4</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#section41">Link 1</a>
<a class="dropdown-item" href="#section42">Link 2</a>
</div>
</li>
</ul>
</nav>
<div class="col-sm-9 col-8">
<div id="section1" class="bg-success">
<h1>Section 1</h1>
<p>홈짱닷컴</p>
</div>
<div id="section2" class="bg-warning">
<h1>Section 2</h1>
<p>Homzzang.com</p>
</div>
<div id="section3" class="bg-secondary">
<h1>Section 3</h1>
<p>홈페이지제작관리 + 서버관리</p>
</div>
<div id="section41" class="bg-danger">
<h1>Section 4-1</h1>
<p>HTML CSS JS JQ BS</p>
</div>
<div id="section42" class="bg-info">
<h1>Section 4-2</h1>
<p>PHP SQL</p>
</div>
</div>
</div>
</div>
</body>
결과보기
주소 복사
랜덤 이동