DHTML Utopia Modern Web Design Using JavaScript & DOM- P11
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
DHTML Utopia Modern Web Design Using JavaScript & DOM- P11 Chapter 7: Advanced Concepts and Menus function mover(e) { var el = window.event ? window.event.srcElement : e ? e.currentTarget : null; ... This works fine for browsers that support the standard event model, but remember that Internet Explorer doesn’t. And although IE uses window.event.srcElement as equivalent to the standard target property, IE has no equivalent to the currentTarget property. In order to get the element to which the event listener was assigned in IE, we’ll have to be slightly more creative. Instead of using the mover and mout functions directly as the mouse event listeners for all of the submenu headers, we’ll create a custom pair of listener functions for each one. Those custom listener functions will, in turn, call mover and mout, but will pass them the reference to the partic- ular li that we need. Let’s look at the code changes. First, in our init function, we alter the addEvent calls that assign our event listeners: File: sliding-menu-6.js (excerpt) addEvent(node, mouseover, getMoverFor(node), false); addEvent(node, mouseout, getMoutFor(node), false); Instead of assigning mover and mout as the listeners, we call new functions getMoverFor and getMoutFor. These functions will create custom listener func- tions for the submenu header in question (node): File: sliding-menu-6.js (excerpt) function getMoverFor(node) { return function(e) { mover(e, node); }; } function getMoutFor(node) { return function(e) { mout(e, node); }; } As you can see, getMoverFor and getMoutFor create and return new event listener functions that call mover and mout, respectively, passing not only the event object, but a reference to the submenu header element, node. Because this listener function is created inside the getMoverFor/getMoutFor function, it can access any of the local variables that exist in that environment, including the node argument. The act of taking a function that has access to a 180Licensed to siowchen@darke.biz Making Submenus Appear private environment and making it accessible (as an event listener) from outside that environment is known in computer science circles as “creating a closure.” We’ve actually done this once before, in Chapter 5, when we created a function that had access to a local variable and passed it to setTimeout. If you’re curious about closures, an excellent (if heavy-going) discussion of them, written by Richard Cornford, is available online.2 For the purposes of this example, however, it’s sufficient to understand that creating a custom event listener for each of the submenu headers allows us to reference that header when the listener is called. Speaking of referencing the header, we now need to modify mover and mout to make use of the reference that’s passed as a second argument by the custom event listeners: File: sliding-menu-6.js (excerpt) function mover(e, targetElement) { var el = window.event ? targetElement : e ? e.currentTarget : null; if (!el) return; ... } function mout(e, targetElement) { var el = window.event ? targetElement : e ? e.currentTarget : null; if (!el) return; ... } These functions use the currentTarget property on W3C DOM-compliant browsers; in Internet Explorer, where this property is not available, the second argument, targetElement, contains the needed value.3 Try this updated script and you’ll find that it works rather well (though not quite perfectly). The changes we’ve made allow the submenus to appear and stay visible, but the succession of events still hides the submenu, then shows it again very quickly, which causes a lot of flicker. 2 http://jibbering.com/faq/faq_notes/closures.html 3 Indeed, you could use targetElement on all browsers and do away completely with ...
Tìm kiếm theo từ khóa liên quan:
thiết kế flash giáo trình css thiết kê web kỹ thuật cắt html đồ họa máy tính 3d giáo trình photoshopGợi ý tài liệu liên quan:
-
Báo cáo thực tập: Đề tài thiết kế Web
77 trang 568 2 0 -
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 267 2 0 -
vray for sketchup vietnamese PHẦN 3
10 trang 213 0 0 -
182 trang 175 0 0
-
Giáo trình CorelDRAW dành cho người mới học
48 trang 141 0 0 -
Giáo trình CorelDraw 10 - Tham khảo toàn diện: Phần 2
528 trang 133 0 0 -
Bài giảng Đồ họa máy tính: Khử mặt khuất - Ngô Quốc Việt
28 trang 127 0 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 113 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 103 0 0 -
Giáo trình Nhập môn thiết kế website
58 trang 83 0 0 -
Tài liệu giảng dạy Thiết kế giao diện Web - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
88 trang 72 0 0 -
Giáo trình môn học Lý thuyết thông tin
136 trang 71 0 0 -
81 trang 68 0 0
-
112 trang 64 0 0
-
Hướng dân sử dụng Navicat để Create , Backup , Restore Database
7 trang 63 0 0 -
Giáo trình môn Kỹ thuật vi điều khiển: Thiết kế web và vi điều khiển - Chương 2
39 trang 59 0 0 -
Giáo trình Photoshop (Nghề: Lập trình máy tính-CĐ) - CĐ Cơ Giới Ninh Bình
231 trang 53 1 0 -
Bài giảng Đồ họa máy tính: Chương 3 - ThS. Trần Thị Minh Hoàn
29 trang 53 0 0 -
Giáo trình photoshop - Chương 3: Cơ bản về chỉnh sửa ảnhPhotoshop CS
19 trang 48 0 0 -
Lecture Computer graphics - Lecture 32
35 trang 46 0 0