SHOW:
|
|
- or go back to the newest paste.
1 | /* | |
2 | New Perspectives on JavaScript, 2nd Edition | |
3 | Tutorial 6 | |
4 | Case Problem 2 | |
5 | Author: Kenneth Hitchcock | |
6 | Date: 6/16/2011 | |
7 | - | Filename: slideshow.js |
7 | + | Filename: flibrary.js |
8 | - | Global Variables: |
8 | + | |
9 | - | scrollButton |
9 | + | getStyle(object, styleName) |
10 | - | References the scrolling button in the slide show |
10 | + | Returns the computed style value for a specified styleName applied to |
11 | - | diffX |
11 | + | an object. |
12 | - | Stores the horizontal distance in pixels between the |
12 | + | addEvent(object, evName, fnName, cap) |
13 | - | mouse pointer when the scrolling button is clicked |
13 | + | Assigns an event handers to object where evName is the name of the event, |
14 | - | and the left edge of the scrolling button. |
14 | + | fnName is the name of the function, and cap is the capture phase. |
15 | This function works for both the IE and W3C event models. | |
16 | - | setup() |
16 | + | removeEvent(object, evName, fnName, cap) |
17 | - | Initializes the contents of the Web page. Creates |
17 | + | Removes an event handers from object where evName is the name of the event, |
18 | - | event handlers for the mouse and keyboard events |
18 | + | fnName is the name of the function, and cap is the capture phase. |
19 | - | grabIt(e) |
19 | + | This function works for both the IE and W3C event models. |
20 | - | "Grabs" the scrolling button to set up the |
20 | + | |
21 | - | horizontal scrolling of the slide show |
21 | + | function getStyle(object, styleName) { |
22 | - | moveIt(e) |
22 | + | if (window.getComputedStyle) { |
23 | - | Moves the scrolling button horizontally through |
23 | + | return document.defaultView.getComputedStyle(object, null).getPropertyValue(styleName); |
24 | - | the scrollbar |
24 | + | } else if (object.currentStyle) { |
25 | - | showSlide(x) |
25 | + | return object.currentStyle[styleName] |
26 | - | Shows the image corresponding the to the x coordinate |
26 | + | |
27 | - | on the scrollbar |
27 | + | |
28 | - | dropIt(e) |
28 | + | |
29 | - | Drops the scrolling button after the user releases the |
29 | + | Assigns an event handers to object where evName is the name of the event, |
30 | - | mouse button |
30 | + | fnName is the name of the function, and cap is the capture phase. |
31 | - | keyShow(e) |
31 | + | This function works for both the IE and W3C event models. |
32 | - | Uses the left and right arrow keys to move the scrolling |
32 | + | |
33 | - | button through the scrollbar |
33 | + | function addEvent(object, evName, fnName, cap) |
34 | { | |
35 | - | window.onload = setup; |
35 | + | if (object.attachEvent) |
36 | - | var scrollButton; |
36 | + | |
37 | - | var diffX = null; |
37 | + | object.attachEvent("on" + evName, fnName); |
38 | } | |
39 | - | Initializes the contents of the Web page. Creates |
39 | + | else if (object.addEventListener) |
40 | - | event handlers for the mouse and keyboard events. |
40 | + | |
41 | object.addEventListener(evName, fnName, cap); | |
42 | - | function setup() |
42 | + | |
43 | } | |
44 | - | scrollButton = document.getElementById("button"); |
44 | + | |
45 | - | scrollButton.style.top = getStyle(scrollButton, "top"); |
45 | + | Removes an event handers from object where evName is the name of the event, |
46 | - | scrollButton.style.left = getStyle(scrollButton, "left"); |
46 | + | fnName is the name of the function, and cap is the capture phase. |
47 | - | scrollButton.style.cursor = getStyle(scrollButton, "pointer"); |
47 | + | This function works for both the IE and W3C event models. |
48 | - | addEvent(scrollButton, "mousedown", grabIt, false); //310 |
48 | + | |
49 | - | addEvent(document, "keydown", keyShow, false); |
49 | + | function removeEvent(object, evName, fnName, cap) |
50 | { | |
51 | if (object.detachEvent) | |
52 | - | "Grabs" the scrolling button to set up the |
52 | + | |
53 | - | horizontal scrolling of the slide show. |
53 | + | object.detachEvent("on" + evName, fnName); |
54 | } | |
55 | - | function grabIt(e) |
55 | + | else if (object.removeEventListener) |
56 | { | |
57 | - | var evt = e || window.event; //314 |
57 | + | object.removeEventListener(evName, fnName, cap); |
58 | - | var mouseX = evt.clientX; //x-coordinate of pointer. 318 |
58 | + | |
59 | - | diffX = parseInt(scrollButton.style.left) - mouseX //319 |
59 | + |