ßê ä³çíàòèñÿ êîîðäèíàòè êóðñîðà

Ùîá çàïðîãðàìóâàòè ÿê³ñü 䳿 ó â³äïîâ³äü íà ïåðåì³ùåííÿ êóðñîðà ó â³êí³ áðàóçåðà áóâຠíåîáõ³äíî âèçíà÷èòè éîãî êîîðäèíàòè. Öå ìîæå çðîáèòè ñêðèïò, ÿêèé ìຠìîæëèâ³ñòü â³äñòåæóâàòè ïî䳿, ùî â³äáóâàþòüñÿ â áðàóçåð³. Òàêó ìîæëèâ³ñòü ìຠâèêîíóâàíèé íà ñòîðîí³ ê볺íòà javascript-ñöåíàð³é. Íèæ÷å îïèñàíèé îäèí ç âàð³àíò³â îòðèìàííÿ êîîðäèíàò êóðñîðà ç âèêîðèñòàííÿì ìîæëèâîñòåé ö³º¿ ìîâè.
ßê ä³çíàòèñÿ êîîðäèíàòè êóðñîðà




²íñòðóêö³ÿ
1
Âèêîðèñòîâóéòå âëàñòèâîñò³ îá'ºêòà event äëÿ îòðèìàííÿ ïîòî÷íèõ êîîðäèíàò êóðñîðà. Öåé îá'ºêò ìຠö³ëèé íàá³ð âëàñòèâîñòåé, ÿê³ ìàþòü â³äíîøåííÿ äî âèçíà÷åííÿ êîîðäèíàò êóðñîðó ìèø³. Âëàñòèâ³ñòü LayerX ì³ñòèòü âèì³ðÿíå â ï³ñêñåëàõ â³äñòàíü â³ä ë³âîãî êðàþ ïîòî÷íîãî øàðó, à LayerY - àíàëîã³÷íó â³äñòàíü â³ä éîãî âåðõíüîãî êðàþ. Ó öèõ äâîõ âëàñòèâîñòåé º ñèíîí³ìè - çàì³ñòü event.LayerX ìîæíà ïèñàòè event.x, à çàì³ñòü event.LayerY - event.y. Âëàñòèâîñò³ pageX ³ pageY ì³ñòÿòü ãîðèçîíòàëüíó ³ âåðòèêàëüíó êîîðäèíàòè êóðñîðà ùîäî âåðõíüîãî ë³âîãî êðàþ â³êíà áðàóçåðà, à â âëàñòèâîñò³ screenX ³ screenY ïîì³ùàþòüñÿ àíàëîã³÷í³ çíà÷åííÿ ùîäî åêðàíó ìîí³òîðà.


2
Äîäàâàéòå â êîä ïåðåâ³ðêó òèïó áðàóçåðà ³ âèêîðèñòîâóéòå âëàñòèâîñò³ clientX ³ clientY íà äîäàòîê äî íàâåäåíèõ âèùå âëàñòèâîñòÿì îá'ºêòà event. Öå íåîáõ³äíî ÷åðåç òå, ùî Microsoft â ñâ³é áðàóçåð Internet Explorer çàêëàäຠâ³äì³ííå â³ä ñòàíäàðòíèõ âèçíà÷åíü ïîçíà÷åííÿ âëàñòèâîñòåé. Ïîºäíàòè îáèäâà ï³äõîäè äî âèçíà÷åííÿ êîîðäèíàò ìîæíà, íàïðèêëàä, òàê:
if (evevnt.pageX || evevnt.pageY) {

coordinateX = evevnt.pageX-

coordinateY = evevnt.pageY-

}

else if (evevnt.clientX || evevnt.clientY)
3
Ïîì³ñò³òü êîä âèçíà÷åííÿ êîîðäèíàò â êîðèñòóâà÷à ôóíêö³þ. Íàïðèêëàä:
function GetMouse (evevnt) {

var coordinateX = 0, coordinateY = 0-
if (! evevnt) evevnt = window.event-
if (evevnt.pageX || evevnt.pageY) {

coordinateX = evevnt.pageX-

coordinateY = evevnt.pageY-

}

else if (evevnt.clientX || evevnt.clientY)
return {"coordX": coordinateX, "coordY": coordinateY} -

}
Öÿ ôóíêö³ÿ ïîâåðòຠìàñèâ ç äâîõ ³ìåíîâàíèõ åëåìåíò³â, ïåðøèé ç ÿêèõ (ç êëþ÷åì coordX) ì³ñòèòü êîîðäèíàòó ïî îñ³ X, à äðóãèé (coordY) - ïî îñ³ Y.
4
Âèêëèêàéòå öþ ôóíêö³þ ç ÿêîãî àáî ïî䳿 - íàïðèêëàä, çà ïî䳺þ ïåðåì³ùåííÿ ìèø³ (onmousemove) â êîíòåêñò³ äîêóìåíòà. Íàâåäåíèé íèæ÷å çðàçîê âèêîðèñòîâóº ôóíêö³þ äëÿ âèâåäåííÿ êîîðäèíàò ìèø³ â ðÿäîê ñòàíó:
document.onmousemove = function (evevnt) {var CurCoord = GetMouse (evevnt) - window.status = "êîîðä X:" + CurCoord.coordX + "px, êîîðä Y:" + CurCoord.coordY + "px" -} -
Ïåðåãëÿä³â: 4621

Óâàãà, ò³ëüêè ÑÜÎÃÎÄͲ!