제이쿼리 ( JQuery ) 예제

가상 클래스

 

코드펜 :  https://codepen.io/gamza/pen/OgwbJJ

 

▣ 구상

  - 이미지를 클릭 시 이미지 크기의 변화 및 영역변화에 따른 텍스트의 이동

  - 구현시 domElement 의 클래스 선택자를 이용해 구현하되, html 이나 css 에서 선언하지 않고 구현

 

▣ 조건

 ➊  이미지 

   텍스트 

   이미지의 늘고 줄어드는 기능 ( 가상 클래스 사용 )

 

 

▣ 준비

·  이미지 1장

 

 

 

◆ 레이아웃 ( html, css )

 

 

1.  전체를 감싸는 #wrap 내부에 <img> 와 임의의 텍스트를 포함하는 <p> 를 추가합니다.

2.  <img> 를 float : left 시켜 다음에 오는 <p> 가 오른쪽 영역에 나란히 오도록 만듭니다.

 

● html 

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
31
32
33
34
35
36
37
38
39
40
41
<div id="wrap">
<img src="sting.jpg" alt="sting" />
<p>
You'll remember me when the west wind moves
Upon the fields of barley
You'll forget the sun in his jealous sky
As we walk in fields of gold
So she took her love
For to gaze awhile
Upon the fields of barley
In his arms she fell as her hair came down
Among the fields of gold
Will you stay with me, will you be my love
Among the fields of barley
We'll forget the sun in his jealous sky
As we lie in fields of gold
See the west wind move like a lover so
Upon the fields of barley
Feel her body rise when you kiss her mouth
Among the fields of gold
I never made promises lightly
And there have been some that I've broken
But I swear in the days still left
We'll walk in fields of gold
We'll walk in fields of gold
Many years have passed since those summer days
Among the fields of barley
See the children run as the sun goes down
Among the fields of gold
You'll remember me when the west wind moves
Upon the fields of barley
You can tell the sun in his jealous sky
When we walked in fields of gold
When we walked in fields of gold
When we walked in fields of gold
</p>
</div>

● css

1
2
3
#wrap{*zoom:1}
#wrap:after{display:block;clear:both;content:''}
#wrap>img{float:left;margin-right:10px;border:1px solid black;}

 

 

◆ 기능 ( jquery )

 

 

1.  document 내부의 요소 #wrap > img 클릭

2.  가상클래스를 가진 경우 이미지 축소 후 가상클래스 삭제

3.  가상클래스를 가지지 않은 경우 이미지 확대 후 가상클래스 추가

 

● javascript

1
2
3
4
5
6
7
8
9
$(document).on("click", "#wrap>img",function(){
if ($(this).hasClass("active")){ // 가상클래스를 가지고 있다면
$(this).animate({"width":300},300);
$(this).removeClass("active");
}else{ // 가상클래스를 가지지 않았다면
$(this).animate({"width":1000},700);
$(this).addClass("active");
}
});

댓글

댓글 본문
버전 관리
gamza
현재 버전
선택 버전
공동공부
graphittie 자세히 보기