Prev | Current Page 311 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


To illustrate the use of an external library in JavaScript a simple class Panel is defined in the external
.js file. The class accepts the name of an element (attribute id in the HTML element) as argument for its
constructor and is able to perform some simple actions on the element such as dragging it and switching
its visibility.
// content of file external.js
function Panel(name)
{
var el = document.getElementById(name);
this.el = el;
el.style.left = el.style.left || el.offsetLeft;
el.style.top = el.style.top || el.offsetTop;
this.visible = true;
this.draggable = false;
}
Panel.prototype.activateDrag = function()
{
var el = this.el;
el.style.backgroundColor = ???#eeeeee???;
el.onmousedown = function(e) {
e = e || event; // normalize event
if(!e) return; // no compatible browser
var pos = {
x : parseInt(el.style.left),
y : parseInt(el.style.top),
h : e.clientX,
v : e.clientY }
document.onmouseup = function(e)
{
document.onmousemove = null;
}
document.onmousemove = function(e)
{
if(!e) e = event; // normalize event
el.


Pages:
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323