javascript編程起步(第五課)
更新時(shí)間:2007年02月27日 00:00:00 作者:
鼠標(biāo)事件(上)
隨著課程的進(jìn)行,能跟著下來(lái)的人是越來(lái)越少了,不知道是不是因?yàn)闆](méi)有太多的表現(xiàn),只是死記的東西,大家都沒(méi)有興趣啊。其實(shí)網(wǎng)頁(yè)上
的很多特效,動(dòng)作大都是用javascript來(lái)實(shí)現(xiàn)的,沒(méi)有javascript的網(wǎng)頁(yè),就象一個(gè)人沒(méi)有了肌肉一樣。但是所有的動(dòng)作都是有函數(shù)來(lái)控制的
,而控制語(yǔ)句是基礎(chǔ)中的基礎(chǔ)。希望大家能耐心的學(xué)下去。今天的課程就輕松一下,學(xué)習(xí)點(diǎn)能見(jiàn)到效果的。
主要內(nèi)容就是基于鼠標(biāo)的事件,有如下幾種:
1.mouseover(鼠標(biāo)移至)
2.mouseout(鼠標(biāo)移出)
3.mousemove(鼠標(biāo)移動(dòng))
4.mousedown(鼠標(biāo)按下)
5.mouseup(鼠標(biāo)彈起)
6.click(單擊)
7.dblclick(雙擊)
通常1和2組合起來(lái)使用,當(dāng)用戶把鼠標(biāo)移動(dòng)到一個(gè)超鏈接或者其他元素時(shí),mouseover事件就會(huì)發(fā)生,mouseout總會(huì)伴隨著它,因?yàn)楫?dāng)鼠標(biāo)
離開時(shí),mouseout事件就會(huì)發(fā)生。
例子:
<html>
<head>
<title>test</title>
<script language="javascript">
function text_onmouseover(){
mytext.style.fontSize="30pt";
mytext.style.color="red";
mytext.style.fontStyle="italic";
}
function text_onmouseout(){
mytext.style.fontSize="20pt";
mytext.style.color="blue";
mytext.style.fontStyle="normal";
}
</script>
</head>
<body>
<p id=mytext onmouseover="text_onmouseover()"
onmouseout="text_onmouseout()">http://www.javascript.com.cn</p>
<p>看看字體樣式有什么變化</p>
</body>
</html>
這里定義了兩個(gè)函數(shù),來(lái)使字體改變樣式。關(guān)于函數(shù)我們以后會(huì)詳細(xì)的講解。
(注意:ie對(duì)頁(yè)面上的所有元素都支持mouseover和mouseout事件,但對(duì)于netscape navigator來(lái)說(shuō),只有超鏈接和層支持這兩個(gè)事件。)
下面看鼠標(biāo)移動(dòng)的例子:
<html>
<body onMousemove="micro$oftMouseMove()">
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == Netscape)
{
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = netscapeMouseMove;
}
function netscapeMouseMove(e) {
if (e.screenX != document.test.x.value && e.screenY != document.test.y.value)
{
document.test.x.value = e.screenX;
document.test.y.value = e.screenY;
}
}
function micro$oftMouseMove() {
if (window.event.x != document.test.x.value && window.event.y !=
document.test.y.value)
{
document.test.x.value = window.event.x;
document.test.y.value = window.event.y;
}
}
</SCRIPT>
<FORM NAME="test">
X: <INPUT TYPE="TEXT" NAME="x" SIZE="4"> Y: <INPUT
TYPUE="TEXT" NAME="y" SIZE="4">
</FORM>
</body>
</html>
鼠標(biāo)移動(dòng)的事件在鼠標(biāo)跟隨效果的使用上比較多,大家可以看看鼠標(biāo)跟隨特效。網(wǎng)上n多。
(需要注意的是:?jiǎn)?dòng)這個(gè)事件處理過(guò)程存在一個(gè)問(wèn)題,就是它有可能會(huì)屏蔽其他事件。另外還增加了頁(yè)面的處理時(shí)間,應(yīng)盡量少用。)
先說(shuō)到這兒吧,下節(jié)講鼠標(biāo)的另外4個(gè)事件。
今天的作業(yè)是:
1.圖片鏈接的轉(zhuǎn)換(當(dāng)鼠標(biāo)放上去時(shí)是一個(gè)圖片,當(dāng)鼠標(biāo)離開時(shí)是另外一個(gè)圖片)
2.圖片跟隨鼠標(biāo)(當(dāng)鼠標(biāo)移動(dòng)時(shí),會(huì)有個(gè)圖片跟隨著鼠標(biāo)一起移動(dòng))
隨著課程的進(jìn)行,能跟著下來(lái)的人是越來(lái)越少了,不知道是不是因?yàn)闆](méi)有太多的表現(xiàn),只是死記的東西,大家都沒(méi)有興趣啊。其實(shí)網(wǎng)頁(yè)上
的很多特效,動(dòng)作大都是用javascript來(lái)實(shí)現(xiàn)的,沒(méi)有javascript的網(wǎng)頁(yè),就象一個(gè)人沒(méi)有了肌肉一樣。但是所有的動(dòng)作都是有函數(shù)來(lái)控制的
,而控制語(yǔ)句是基礎(chǔ)中的基礎(chǔ)。希望大家能耐心的學(xué)下去。今天的課程就輕松一下,學(xué)習(xí)點(diǎn)能見(jiàn)到效果的。
主要內(nèi)容就是基于鼠標(biāo)的事件,有如下幾種:
1.mouseover(鼠標(biāo)移至)
2.mouseout(鼠標(biāo)移出)
3.mousemove(鼠標(biāo)移動(dòng))
4.mousedown(鼠標(biāo)按下)
5.mouseup(鼠標(biāo)彈起)
6.click(單擊)
7.dblclick(雙擊)
通常1和2組合起來(lái)使用,當(dāng)用戶把鼠標(biāo)移動(dòng)到一個(gè)超鏈接或者其他元素時(shí),mouseover事件就會(huì)發(fā)生,mouseout總會(huì)伴隨著它,因?yàn)楫?dāng)鼠標(biāo)
離開時(shí),mouseout事件就會(huì)發(fā)生。
例子:
<html>
<head>
<title>test</title>
<script language="javascript">
function text_onmouseover(){
mytext.style.fontSize="30pt";
mytext.style.color="red";
mytext.style.fontStyle="italic";
}
function text_onmouseout(){
mytext.style.fontSize="20pt";
mytext.style.color="blue";
mytext.style.fontStyle="normal";
}
</script>
</head>
<body>
<p id=mytext onmouseover="text_onmouseover()"
onmouseout="text_onmouseout()">http://www.javascript.com.cn</p>
<p>看看字體樣式有什么變化</p>
</body>
</html>
這里定義了兩個(gè)函數(shù),來(lái)使字體改變樣式。關(guān)于函數(shù)我們以后會(huì)詳細(xì)的講解。
(注意:ie對(duì)頁(yè)面上的所有元素都支持mouseover和mouseout事件,但對(duì)于netscape navigator來(lái)說(shuō),只有超鏈接和層支持這兩個(gè)事件。)
下面看鼠標(biāo)移動(dòng)的例子:
<html>
<body onMousemove="micro$oftMouseMove()">
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == Netscape)
{
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = netscapeMouseMove;
}
function netscapeMouseMove(e) {
if (e.screenX != document.test.x.value && e.screenY != document.test.y.value)
{
document.test.x.value = e.screenX;
document.test.y.value = e.screenY;
}
}
function micro$oftMouseMove() {
if (window.event.x != document.test.x.value && window.event.y !=
document.test.y.value)
{
document.test.x.value = window.event.x;
document.test.y.value = window.event.y;
}
}
</SCRIPT>
<FORM NAME="test">
X: <INPUT TYPE="TEXT" NAME="x" SIZE="4"> Y: <INPUT
TYPUE="TEXT" NAME="y" SIZE="4">
</FORM>
</body>
</html>
鼠標(biāo)移動(dòng)的事件在鼠標(biāo)跟隨效果的使用上比較多,大家可以看看鼠標(biāo)跟隨特效。網(wǎng)上n多。
(需要注意的是:?jiǎn)?dòng)這個(gè)事件處理過(guò)程存在一個(gè)問(wèn)題,就是它有可能會(huì)屏蔽其他事件。另外還增加了頁(yè)面的處理時(shí)間,應(yīng)盡量少用。)
先說(shuō)到這兒吧,下節(jié)講鼠標(biāo)的另外4個(gè)事件。
今天的作業(yè)是:
1.圖片鏈接的轉(zhuǎn)換(當(dāng)鼠標(biāo)放上去時(shí)是一個(gè)圖片,當(dāng)鼠標(biāo)離開時(shí)是另外一個(gè)圖片)
2.圖片跟隨鼠標(biāo)(當(dāng)鼠標(biāo)移動(dòng)時(shí),會(huì)有個(gè)圖片跟隨著鼠標(biāo)一起移動(dòng))
相關(guān)文章
js 使用form表單select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果
本文介紹javascript使用html中form表單中的select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果,需要的朋友可以了解下2012-12-12
javascript中的3種繼承實(shí)現(xiàn)方法
這篇文章主要介紹了javascript中的3種繼承實(shí)現(xiàn)方法,包括使用Object.create實(shí)現(xiàn)類式繼承、使用utilities工具包自帶的util.inherites、使用extends關(guān)鍵字,非常的實(shí)用,希望對(duì)大家了解javascript繼承能夠有所幫助2016-01-01
Javascript實(shí)例教程(19) 使用HoTMetal(7)
Javascript實(shí)例教程(19) 使用HoTMetal(7)...2006-12-12
JavaScript中也使用$美元符號(hào)來(lái)代替document.getElementById
JavaScript可以定義$符號(hào)函數(shù),簡(jiǎn)寫或書寫兼容性更好的代碼。2010-06-06
window.parent與window.openner區(qū)別介紹
今天總結(jié)一下js中幾個(gè)對(duì)象的區(qū)別和用法,對(duì)這幾個(gè)概念混淆的朋友可以看看2012-04-04

