ex13
Mouse Event, ๋ง์ฐ์ค ๊ด๋ จ ์ด๋ฒคํธ
- onmouseXXX
1. onmouseover(= onmouseenter)
- ํด๋น ๊ฐ์ฒด์ ์์ญ์ ๋ง์ฐ์ค(์ปค์)๊ฐ ์ง์
ํ๋ ์๊ฐ ๋ฐ์
2. onmouseout(= onmouseleave)
- ํด๋น ๊ฐ์ฒด์ ์์ญ์ ๋ง์ฐ์ค(์ปค์)๊ฐ ๋น ์ ธ๋๊ฐ๋ ์๊ฐ ๋ฐ์
<script>
function message(txt) {
var now = new Date();
txt1.value = txt + '-' + now.toLocaleTimeString() + '\r\n' + txt1.value; /* ๋์ */
}
txt1.onmouseover = m1;
function m1() {
message('mouseover');
window.document.body.bgColor = 'gold';
}
txt1.onmouseout = m2;
function m2() {
message('mouseout');
window.document.body.bgColor = 'white';
}
</script>
ํ๋ฉด
๋ง์ฐ์ค ๊ด๋ จ ์ด๋ฒคํธ
3. onmousedown
- ํด๋น ๊ฐ์ฒด์ ์์ญ์์ ๋ง์ฐ์ค ๋ฒํผ์ ๋๋ฅด๋ ์๊ฐ ๋ฐ์
-๋ง์ฐ์ค ์ด๋ค ๋ฒํผ?
event.buttons
์ผ์ชฝ(1), ์ค๋ฅธ์ชฝ(2), ์ผ์ชฝ+์ค๋ฅธ์ชฝ(3), ํ ๋ฒํผ(4)
-๋ง์ฐ์ค ์ปค์ ์ขํ
1. x, y (์์์)
- ๋ฌธ์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
- ๋นํ์ค(MS-IE) > ๋น๊ถ์ฅ
2. clientX, clientY (*****)
- ๋ฌธ์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
- ํ์ค > ๊ถ์ฅ
- CSS > position: absolute;์ ๋น์ท > ์ฎ์ด์ ์ ์
3. screenX, screenY (์์์)
- ๋ชจ๋ํฐ ์ข์ธก ์๋จ์ ๊ธฐ์ค
4. offsetX, offsetY (*****)
- ์ด๋ฒคํธ ๊ฐ์ฒด์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
- ๋ ์์ ์ด ๊ธฐ์ค
- CSS > position: relative; ์ ๋น์ท
4. onmouseup
- ํด๋น ๊ฐ์ฒด์ ์์ญ์์ ๋ง์ฐ์ค ๋ฒํผ์ ๋ผ๋ ์๊ฐ ๋ฐ์
<script>
function message(txt) {
var now = new Date();
txt1.value = txt + '-' + now.toLocaleTimeString() + '\r\n' + txt1.value; /* ๋์ */
}
txt1.onmouseover = m1;
function m1() {
//message('mouseover');
//window.document.body.bgColor = 'gold';
}
txt1.onmouseout = m2;
function m2() {
//message('mouseout');
//window.document.body.bgColor = 'white';
}
txt1.onmousedown = m3;
function m3() {
// message('mousedown');
// window.document.body.bgColor = 'tomato';
//๋ง์ฐ์ค ์ด๋ค ๋ฒํผ?
//event.buttons
//์ผ์ชฝ(1), ์ค๋ฅธ์ชฝ(2), ์ผ์ชฝ+์ค๋ฅธ์ชฝ(3)
// message(event.buttons);
// if (event.buttons == 2) {
// alert('์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ๋ฒํผ ํด๋ฆญ ๊ธ์ง!!!');
// }
//๋ง์ฐ์ค ์ปค์ ์ขํ
//1. x, y
// - ๋ฌธ์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
// - ๋นํ์ค(MS-IE) > ๋น๊ถ์ฅ
//2. clientX, clientY *****
// - ๋ฌธ์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
// - ํ์ค > ๊ถ์ฅ
// - CSS > position: absolute;์ ๋น์ท > ์ฎ์ด์ ์ ์
//3. screenX, screenY (๋ค๋ฃจ๊ธฐ ์ด๋ ค์ > ์ ์์)
// - ๋ชจ๋ํฐ ์ข์ธก ์๋จ์ ๊ธฐ์ค
//4. offsetX, offsetY *****
// - ์ด๋ฒคํธ ๊ฐ์ฒด์ ์ข์ธก ์๋จ์ ๊ธฐ์ค
// - ๋ ์์ ์ด ๊ธฐ์ค
// - CSS > position: relative; ์ ๋น์ท
// message(event.x + ',' + event.y)
// message(event.clientX + ',' + event.clientY);
//message(event.screenX + ',' + event.screenY);
//message(event.offsetX + ',' + event.offsetY);
}
txt1.onmouseup = m4;
function m4() {
// message('mouseup');
// window.document.body.bgColor = 'white';
}
</script>
ํ๋ฉด
๋ง์ฐ์ค ๊ด๋ จ ์ด๋ฒคํธ
5. onmousemove
- ํด๋น ๊ฐ์ฒด์ ์์ญ์์ ๋ง์ฐ์ค๊ฐ ์์ง์ผ๋๋ง๋ค ๋ฐ์
<script>
txt1.onmousemove = m5;
function m5() {
// message('mousemove');
}
</script>
*** onclick
- ๋ง์ฐ์ค ์ด๋ฒคํธ(X) / ํค๋ณด๋ ๋ฑ์ผ๋ก๋ ํด๋ฆญ ๊ฐ๋ฅ
- onclick ๋ด์์๋ ๋ง์ฐ์ค์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ ์๋ค.
- ์ด๋ค ๋ฒํผ? ์ขํ? > ์ ์ ๋ถ๊ฐ๋ฅ
ex14
Key Event, ํค ์ด๋ฒคํธ
- onkeyXXX
- ํฌ์ปค์ค๋ฅผ ๊ฐ์ง๋ ํ๊ทธ์์๋ง ๋ฐ์(ํ
์คํธ ๋ฐ์ค, ์ฒดํฌ ๋ฐ์ค ๋ฑ)
- ๋ฐ์ ์์: keydown > keypress > keyup
ํ์์ ํค ์ด๋ฒคํธ > keydown
ํน๋ณํ ๋๋ฅธ ํค๊ฐ ๊ด์ฌ๋ ์์
> keyup
1. onkeydown
- ํค๋ฅผ ๋๋ ์ ๋ ๋ฐ์
- ๋ฌธ์์ ๋ฐ์ํ๋ ์ด๋ฒคํธ(X) > ์ด๋ค ๋ฌธ์๊ฐ ์
๋ ฅ๋์๋์ง(X)
- ๋ฌผ๋ฆฌํค์ ๋ฐ์ํ๋ ์ด๋ฒคํธ > ์ด๋ค ํค๋ฅผ ๋๋ ๋์ง(O)
- ํค๋ณด๋์ ์กด์ฌํ๋ ๋ชจ๋ ํค์ ๋ฐ์(******)
- ๋ฐฉ๊ธ ์
๋ ฅํ ๋ฌธ์๋ ํ์์์ ๋ ์ฌ์ฉ!!
2. onkeyup
- ํค๋ฅผ ๋์ ๋ ๋ฐ์
- ๋ฌผ๋ฆฌํค ๋ฐ์
- ๋ฐฉ๊ธ ์
๋ ฅํ ๋ฌธ์๊ฐ ํ์ํ ๊ฒฝ์ฐ์ ์ฌ์ฉ!!
3. onkeypress(์ ์ฌ์ฉ ์ํจ)
- ํค๋ฅผ ๋๋ ์ ๋ ๋ฐ์
- ๋ฌธ์์ ๋ฐ์ํ๋ ์ด๋ฒคํธ(O)
- ๋ฌธ์๊ฐ ์๋ ํค๋ ๋ฐ์ ์ํจ(*****)
<script>
txt1.onkeydown = m1;
txt1.onkeyup = m2;
txt1.onkeypress = m3;
function m1(evt) {
// console.log('keydown');
//event.keyCode
// ๋นํ์ค ๋ฐฉ๋ฒ
// console.log(event.keyCode);
// ํ์ค ๋ฐฉ๋ฒ > ๋งค๊ฐ๋ณ์
// console.log(evt.keyCode);
//๋ฐฉํฅํค
//- ์ข(37), ์(38), ์ฐ(39), ํ(40)
// if (evt.keyCode == 37) {
// txt3.cols--;
// } else if (evt.keyCode == 39) {
// txt3.cols++;
// } else if (evt.keyCode == 38) {
// txt3.rows--;
// } else if (evt.keyCode == 40) {
// txt3.rows++;
// }
// ์ฒซ๋ฒ์งธ ํ
์คํธ ๋ฐ์ค ๊ฐ์ ๋๋ฒ์งธ ๋ฐ์ค์ ๋ณต์ฌ
// keydown > ํ๋ฐ์ ๋๋ฆผ
//txt2.value = txt1.value;
}
function m2() {
// console.log('keyup');
// console.log(event.keyCode);
// keyup > ๋ฐ๋ก ์ ์ฉ๋จ
//txt2.value = txt1.value;
}
function m3() {
// console.log('keypress');
// console.log(event.keyCode);
}
</script>
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ex17] window (0) | 2023.04.24 |
---|---|
[ex15~16] Attribute, images (0) | 2023.04.21 |
[ex11~] BOM, event (0) | 2023.04.21 |
[ex09~10] Date, Array (0) | 2023.04.21 |
[ex06~08] scope, casting, string (0) | 2023.04.21 |