메뉴 건너뛰기

조회 수 8476 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<style>

*{

margin:0;padding:0;

}

.header{

background:#91b280;

border-bottom:8px solid #91b280;

}

.menu{

height:25px;

}

.menu li{

list-style-type:none;

float:left;

padding:0 0 0 8px;

}

</style>

<script>
$(function(){
$('.menu_item').each(function(){
var a = $(this);
var img = a.find('img');
var src_off = img.attr('src');
var src_on = src_off.replace('_off', '_on');
a.hover(function(){
img.attr('src', src_on);
},function(){
img.attr('src', src_off);
});
});
});
</script>
<body>
<div class="header">
<ul class="menu">
<li><a href="" class="menu_item"><img src="/images/menu1_off.png"></a></li>
<li><a href="" class="menu_item"><img src="/images/menu2_off.png"></a></li>
<li><a href="" class="menu_item"><img src="/images/menu3_off.png"></a></li>
</ul>
</div>
</body>



메뉴


$('.menu_item').each(function(){  

}

클래스 속성값으로 menu_item 을 갖는 각각의 객체에 대해서 파라미터로 전달된 콜백함수를 수행한다. 여기서는 메뉴를 구성하는 <a>태그들이 클래스 속성 값으로 menu_item 를 갖는다.


var a = $(this);   

선택한 객체(DOM)를 jQuery 개체로 변환해서 변수 a 에 반환한다.


var img = a.find('img');

현재 객체 안에서 이미지 객체를 찾아서 반환한다.


var src_off = img.attr('src');

이미지 객체의 속성값 src의 값을 변수 src_off 에 반환한다. 변수 src_off 값으로는 차례대로 '/images/menu1_off.png', /images/menu2_off.png', '/images/menu3_off.png' 가 온다.


var src_on = src_off.replace('_off', '_on');

변수 src_off 에 들어있는 문자열 '/images/menu1_off.png' 에서 '_off' 를 찾아 '_on' 으로 변경한 후, 변경된 문자열을 변수 src_on 에 넣는다.


a.hover(function(){},function{});

<a> jQuery 메소드 hover()의 콜백함수를 정의한다. 첫번째 콜백함수는 마우스가 객체위에 올라갔을때 실행된다. 두번째 콜백함수는 마우스가 객체를 벗어날때 실행된다. 즉, 첫번째 콜백함수에서는 마우스가 메뉴에 올라갔을때 이미지로 교체하고 두번째 콜백함수는 마우스가 메뉴에서 벗어났을때 이미지로 교체한다.


a.hover(function(){

img.attr('src', src_on);

},function(){

img.attr('src', src_off);

}


아래 이미지는 '메뉴1'에 마우스를 올렸을 때, 이미지를 파란색 배경 이미지로 교체한 모습이다. 


메뉴1에 마우스를 올렸을 때 모습


  1. jqGrid / custom pager / custom paging / 그리드 / 페이징

    Date2016.09.09 Views12522
    Read More
  2. javascript, jQuery에서 루프 돌리기 예 (for, forEach, each)

    Date2021.03.25 Views311
    Read More
  3. Input TYPE=“File” 을 히든으로 하고 외부 버튼을 눌러서 파일을 선택

    Date2016.09.21 Views6801
    Read More
  4. input checkbox 모두 체크하기

    Date2021.03.26 Views176
    Read More
  5. iframe height auto resize

    Date2018.11.07 Views1403
    Read More
  6. html 테이블 고정 (table fixed)

    Date2016.11.18 Views7118
    Read More
  7. Html 색상표 모음, 색상코드표 (RGB), 이미지X, 복사 가능

    Date2016.11.17 Views8145
    Read More
  8. hover 메소드를 이용해서 메뉴 on, off를 구현

    Date2016.09.21 Views8476
    Read More
  9. Hide pager if bxslider has only 1 slide

    Date2018.11.07 Views1296
    Read More
  10. Get방식 파라미터 전송 & 탭 메뉴 선택

    Date2016.09.21 Views8566
    Read More
  11. getScript()

    Date2016.09.21 Views7125
    Read More
  12. getJSON() 함수

    Date2016.09.21 Views8379
    Read More
  13. getElementsByClassName 을 IE8 이하에서 동작하게 하는 코드

    Date2018.11.07 Views1299
    Read More
  14. FORM 객체 초기화 하기

    Date2016.09.21 Views7346
    Read More
  15. foreach문

    Date2021.03.26 Views195
    Read More
  16. focus() 로 오브젝트 옮기기

    Date2021.03.26 Views171
    Read More
  17. fadeIn() , fadeOut() 을 이용한 간단한 자동 그림 전환

    Date2021.03.26 Views285
    Read More
  18. easing - 효과의 진행 속도

    Date2021.03.26 Views156
    Read More
  19. draggable - div 드래그

    Date2021.03.31 Views273
    Read More
  20. document.getElementById() 처럼 DOM 객체 얻기

    Date2016.09.21 Views6810
    Read More
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 Next
/ 9

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved