Basic Event Management in CBEIntro To CBE EventsIt will be helpful to first read about the CBE object model and event model. The individual events are described in the reference. CBE provides support for three categories of events:
The CrossBrowserElement methods addEventListener() and removeEventListener() provide the programming interface to all the above. This page provides examples of using some of the events supported by CBE. There are other pages that provide examples for the remaining events. Also see the event properties viewer. DOM 2 EventsThese events propagate up and down the parent chain of the event target.
mouseMove ExampleOn this page there are three positioned elements, C, B, and E (they're draggable). They float in the southeast corner of the window. The following will add/remove a mouseMove event listener to document.cbe which will control the three elements. window.C = cbeGetElementById('C'); window.B = cbeGetElementById('B'); window.E = cbeGetElementById('E'); ... document.cbe.addEventListener('mouseMove', mouseMoveListener); ... document.cbe.removeEventListener('mouseMove', mouseMoveListener); ... function mouseMoveListener(e) { C.moveTo(e.pageX+10, e.pageY+10); B.moveTo(e.pageX+20, e.pageY+20); E.moveTo(e.pageX+30, e.pageY+30); } mouseOver/mouseOut ExampleThe following will add/remove mouseOver and mouseOut event listeners to elements C, B, and E which will control the colors of the three elements. C.addEventListener('mouseOver', mouseOverListener); C.addEventListener('mouseOut', mouseOutListener); B.addEventListener('mouseOver', mouseOverListener); B.addEventListener('mouseOut', mouseOutListener); E.addEventListener('mouseOver', mouseOverListener); E.addEventListener('mouseOut', mouseOutListener); ... C.removeEventListener('mouseOver', mouseOverListener); C.removeEventListener('mouseOut', mouseOutListener); B.removeEventListener('mouseOver', mouseOverListener); B.removeEventListener('mouseOut', mouseOutListener); E.removeEventListener('mouseOver', mouseOverListener); E.removeEventListener('mouseOut', mouseOutListener); ... function mouseOverListener(e) { e.cbeCurrentTarget.color('#ffffff'); e.cbeCurrentTarget.background('#3333ff'); } function mouseOutListener(e) { e.cbeCurrentTarget.color('#3333ff'); e.cbeCurrentTarget.background('transparent'); } Custom EventsThese events are non-standard - they are specific to CBE. These events are sent directly to the event target's listener and do not propagate.
DOM 0 EventsThis category includes all events not mentioned above. These events are sent directly to the event target's listener and do not propagate. scroll & resize ExampleCurrently, the only cross-browser support for these events is by window.cbe. In NN4, Gecko and Opera scroll is a custom event. In IE it is a DOM0 event. In NN4 and Opera resize is a custom event. In IE and Gecko it is a DOM0 event. The following code snippet is from this site's home page. It uses the window's scroll event to maintain the menubar at the top of the window. It uses the window's resize event to keep the menubar the same width as the window. var menu, anchor; function windowOnload() { anchor = cbeGetElementById('idAnchor').cbe; with (menu = cbeGetElementById('idMenuBar').cbe) { background('#cccccc'); resizeTo(document.cbe.width(), 22); moveTo(0,anchor.pageY()); zIndex(100); show(); } window.cbe.addEventListener('scroll', windowScrollListener); window.cbe.addEventListener('resize', windowResizeListener); } function windowResizeListener(e) { if (is.nav4 || is.opera) { location.replace('index.html'); return; } var my = document.cbe.scrollTop(); if (my==0) my = anchor.pageY(); with (menu) { resizeTo(document.cbe.width(), height()); moveTo(0, my); } } function windowScrollListener(e) { var my = document.cbe.scrollTop(); if (my==0) my = anchor.pageY(); menu.moveTo(0, my); } OthersAll other event types are assumed to be DOM0 events, and are registered accordingly. |