티스토리 뷰

submenu (서브메뉴)

🙾  서브 메뉴의 높이에 따라 노출

let header = $('header');
let headerHeight = header.outerHeight();
let menu = $('nav > ul > li');
let headerTotalHeight = 0;

/*
마우스가 올라간 그 요소의 자식요소(ul)의 높이를 구하고, 
기존 header의 높이와 합산하여 header 높이를 변경한다.
*/
menu.mouseover(function(){
	let headerTotalHeight = headerHeight + $(this).find('ul').outerHeight();
	header.stop().animate({height: headerTotalHeight});
})
.mouseout(function(){
header.stop().animate({height:headerHeight});
})

 


Loading animation bar

▏setInterval 속성 활용

let progressBar = $('.progress-bar'),
bar = progressBar.find('.bar'),
rate = progressBar.find('.rate'),
targetNum = rate.attr('data-rate');

/*
bar의 너비가 targetNum의 값으로 늘어나도록 시간은 1.5초

대상.animate({css속성명:값, css속성명:값},시간);
*/

bar.animate({width:targetNum+'%'},1500);

let numAnimation = setInterval(function(){
    let barCurrentWidth = Math.ceil(bar.width() / progressBar.width()*100);
    rate.text(barCurrentWidth+'%');
    if(targetNum == barCurrentWidth){
        clearInterval(numAnimation);
    }
    console.log('작동')
},100);

 

 

▏animate의 progress속성 활용

ex) 1번째 구현

let progressBar = $('.progress-bar'),
bar = progressBar.find('.bar'),
rate = progressBar.find('.rate'),
targetNum = rate.attr('data-rate');

/*
대상.animate({속성명:값, 속성명:값}시간,이징,할일);

대상.animate({opacity:1},{1500, 'swing', function(){}});

대상.animate({opacity:1},{
    duration: 1500, 
    easing: 'swing', 
    progess: function(){..} //진행중 할일
    complete: function(){..} //완료후 할일
});
*/

$({num:0}).animate({num:60},{
    duration: 1500,
    progress: function(){
        let now = Math.ceil(this.num);
        rate.text(now+'%');
    }, // animate가 진행 중에 할일
    complete:function(){
        console.log('완성');
    } // animate가 다 끝난 다음에 할일
});

ex) 2번째 구현

 

/*
progress마다 각각 할일
변수명 bar에 각요소의 .bar,
변수명 rate에 각요소의 .rate,
변수명 rateNum 각 요소의 data-rate의 값을 할당
*/
progressBar.each(function(){
    let bar = $(this).find('.bar'),
        rate = $(this).find('.rate'),
        rateNum = rate.attr('data-rate');
    
    bar.animate({width:rateNum+'%'}, 1500);
  
    $({num:0}).animate({num:rateNum},{
      duration:1500,
      progress:function(){
        let now = Math.ceil(this.num);
        rate.text(now+'%');
      }
    });
  });

 

▏library 활용

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="robots" content="index, follow">
<title></title>	
<link rel="stylesheet" href="css/main.css">
</head>
<body>
  <h1>number animation</h1>
  <section>
    <h2>section</h2>
  </section>
  <section>
    <h2>section</h2>
  </section>
    <div class="progress">
      <div class="progress-bar">
        <div class="bar"></div>
        <div class="rate" data-rate="60">0%</div>
      </div>
      <div class="progress-bar">
        <div class="bar"></div>
        <div class="rate" data-rate="50">0%</div>
      </div>
      <div class="progress-bar">
        <div class="bar"></div>
        <div class="rate" data-rate="30">0%</div>
      </div>
   </div>
    <section>
      <h2>section</h2>
    </section>
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="js/jquery.waypoints.min"></script>
    <script src="js/jquery.animateNumber.min.js"></script>
    <script src="js/script.js"></script>
  </body>
  </html>
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}


.progress-bar{
	position: relative;
	width: 80%;
	margin: 5em auto;
	height: 40px;
	border:5px solid #ebebeb;
	border-radius:20px;
	overflow: hidden;
	box-sizing: border-box;
}
.bar{
	width: 0;
	height: 100%;
	position: absolute;
	left: 0;
	top: 0;
	background: #3498db;
}
.rate{
	position: absolute;
	right:20px;
	top: 0;
	line-height: 30px;
}

section {
	height: 100vh;
}
let progressBar = $('.progress-bar');
let active = false;

function startAnimation(){

    progressBar.each(function(){
        let bar = $(this).find('.bar'),
            rate = $(this).find('.rate'),
            rateNum = rate.attr('data-rate');
        
        bar.animate({width:rateNum+'%'}, 1500);
      
        $({num:0}).animate({num:rateNum},{
          duration:1500,
          progress:function(){
            let now = Math.ceil(this.num);
            rate.text(now+'%');
          }
        });
      });
      active = true;
    }
    
    $(window).scroll(function(){
        if($(this).scrollTop() >= progressOST){
            // if(!active==false)
            if(!active){
                startAnimation();
                }
        }
    });

    var waypoints = $('.animate').waypoint({
        handler: function() {
            if(!active = true)
            startAnimation();
        },
        offset: '70%'
      })

'jQuery' 카테고리의 다른 글

fade / sticky / shrinking-nav-bar  (0) 2023.10.10
jQuery?  (0) 2023.10.10
Tab / light box / back to top  (0) 2023.10.10
animation 한 번만 작동하게 하기  (0) 2023.10.10
each 함수 / aside menu  (0) 2023.10.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/06   »
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
글 보관함