티스토리 뷰

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>메뉴를 따라다니는 메뉴바</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
* { box-sizing: border-box;}

body {
  background: #1A1E23;
  font-family: "Lato", sans-serif;
  -webkit-font-smoothing: antialiased;
}

nav {
  position: relative;
  padding-bottom: 12px;
}
nav .line {
  height: 2px;
  position: absolute;
  bottom: 0;
  margin: 10px 0 0 0;
  background: #FF1847;
}
nav ul {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: flex;
}
nav ul li {
  margin: 0 40px 0 0;
  opacity: 0.4;
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}
nav ul li:hover {
  opacity: 0.7;
}
nav ul li.active {
  opacity: 1;
}
nav ul li:last-child {
  margin-right: 0;
}
nav ul li a {
  text-decoration: none;
  color: #fff;
  text-transform: uppercase;
  display: block;
  font-weight: 600;
  letter-spacing: 0.2em;
  font-size: 14px;
}

body {
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  align-items: center;
  min-height: 100vh;
}
</style>
</head>

<body>
<nav>
  <ul>
    <li class="active"><a href="">First</a></li>
    <li><a href="">Second</a></li>
    <li><a href="">Third</a></li>
  </ul>
</nav>

<script>
var nav = $('nav');
var line = $('<div />').addClass('line');

line.appendTo(nav);

var active = nav.find('.active');
var pos = 0;
var wid = 0;

if(active.length) {
  pos = active.position().left;
  wid = active.width();
  line.css({
    left: pos,
    width: wid
  });
}

nav.find('ul li a').click(function(e) {
  e.preventDefault();
  if(!$(this).parent().hasClass('active') && !nav.hasClass('animate')) {
    
    nav.addClass('animate');

    var _this = $(this);

    nav.find('ul li').removeClass('active');

    var position = _this.parent().position();
    var width = _this.parent().width();

    if(position.left >= pos) {
      line.animate({
        width: ((position.left - pos) + width)
      }, 300, function() {
        line.animate({
          width: width,
          left: position.left
        }, 150, function() {
          nav.removeClass('animate');
        });
        _this.parent().addClass('active');
      });
    } else {
      line.animate({
        left: position.left,
        width: ((pos - position.left) + wid)
      }, 300, function() {
        line.animate({
          width: width
        }, 150, function() {
          nav.removeClass('animate');
        });
        _this.parent().addClass('active');
      });
    }

    pos = position.left;
    wid = width;
  }
});
</script>
</body>
</html>

출처: wsss.tistory.com/561

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함