메뉴 건너뛰기

조회 수 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. jquery 팝업 차단 없이 띄우기

    Date2019.03.05 Views2868
    Read More
  2. [jQuery] 라디오(radio) 버튼, 체크박스(checkbox) 선택/해제 하는 방법

    Date2019.03.05 Views1129
    Read More
  3. jQuery 기초 (Query link url / download (위치, 사용법) // p태그, id, class 접근 / 일반태그 가져오기 / 클릭시 값)

    Date2019.01.16 Views1079
    Read More
  4. jQuery 기초 (클릭하면 이미지 변경 / mouseover시 애니메이션 효과주기 / 동적으로 변경)

    Date2019.01.16 Views1507
    Read More
  5. jQuery 기초 (focus, blur, toggle / mouseenter, mouseleave, mousedown, mouseup, hover)

    Date2019.01.16 Views1268
    Read More
  6. jQuery 기초 (style.css <link> 로 추가하기 / 버튼 클릭시 데이터 삽입)

    Date2019.01.16 Views1181
    Read More
  7. jQuery 기초 (JQuery - text(), val(), html(), attr(), prop())

    Date2019.01.16 Views1055
    Read More
  8. jQuery 기초 (attr()로 두가지 동시에 접근 / 변경)

    Date2019.01.16 Views1119
    Read More
  9. jQuery 기초 (텍스트 추가 (createElement, createTextNode, appendChild), (html, javascript, jquery)

    Date2019.01.16 Views1411
    Read More
  10. jQuery 기초 ((문자열 추가 .before / .after) (문자열 삭제 .remove / .empty)

    Date2019.01.16 Views1084
    Read More
  11. jQuery 기초 (txt 파일 가져오기 (load) , 클릭시에 배경색을 변경(json))

    Date2019.01.16 Views3225
    Read More
  12. Query 기초 (동적 테이블 (데이터 추가 / 삭제), integrateTable(정렬(sort))

    Date2019.01.16 Views2194
    Read More
  13. jQuery 기초 (jQuery 달력 (datepicker))

    Date2019.01.16 Views1321
    Read More
  14. jQuery 기초 (Postcodify - 도로명주소 우편번호 검색 프로그램 (코딩 예제) (HTML) / POP UP 버젼)

    Date2019.01.16 Views1349
    Read More
  15. .removeAttr() : 특정 속성을 제거

    Date2019.01.16 Views1021
    Read More
  16. .attr() : 태그의 속성 값을 읽어오거나 속성을 추가및 재설정

    Date2019.01.16 Views962
    Read More
  17. popModal jQuery Plugin Examples / 무료 jQuery 팝업 플러그인

    Date2019.01.16 Views1235
    Read More
  18. jQuery Plugin : Slider

    Date2019.01.10 Views1113
    Read More
  19. jQuery datepicker 팝업창 사이즈 바꾸기

    Date2019.01.10 Views1501
    Read More
  20. jQuery를 이용한 스크롤 따라니는 배너를 쉽게 맨들기(scroll follow)

    Date2019.01.10 Views1093
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved