Events Part I Mouse Events. Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate.

Slides:



Advertisements
Similar presentations
Microsoft Expression Web-Illustrated Unit K: Working with Behaviors.
Advertisements

Space Man Sam: Grammar Mistakes By Aleis Murphy Duke University, Under the direction of Professor Susan Rodger July 2010.
PowerPoint Basics   Tutorial 5: Navigation
Modifying existing content Adding/Removing content on a page using jQuery.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
1. Tuesday: Halloween Shoot due TOMORROW. You must make a contact sheet of your photos and print it from my computer tomorrow. -5 pts for every day I don’t.
2-May-15 GUI Design. 2 HMI design There are entire college courses taught on HMI (Human-Machine Interface) design This is just a very brief presentation.
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
Word Processing First Steps
Microsoft ® Office Word 2007 Training Bullets, Numbers, and Lists ICT Staff Development presents:
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
JQuery: Fun ‘n Games with Selectors. Learning Objectives By the end of this lecture, you should be able to: – Comfortably use jQuery to select based on.
ABC’s of PowerPoint (Office 2007) Part 1: Basic Vocabulary Part 2: Cursors Part 3: Insert Your Text Part 4: Insert Your Pictures Part 5: Basic Tools &
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Alice Learning to program: Part Two by Ruthie Tucker and Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2008.
Random Stuff Colors Sizes CSS Shortcuts. Learning Objectives By the end of this lecture, you should be able to: – Identify the 3 most common ways in which.
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
HBar OR Reader Documentation A copy of the PowerPoint Viewer is shipped with the HBar OR Reader on the HBar Official Records [OR] CD. The PowerPoint Viewer.
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
JQuery Page Slider. Our goal is to get to the functionality of the Panic Coda web site.Panic Coda web site.
KRAD makes it easy to handle HTML events in your webpage. Kuali University: Apply Now Lab 6: Fun with HTML Events Lab Objectives HTML Events – what are.
Spreadsheets in Finance and Forecasting Presentation 8: Problem Solving.
Selectors. Learning Objectives By the end of this lecture, you should be able to: – Select items using jQuery based on ID, class name, or HTML tag. –
I Didn’t Know You Could Do That in Articulate Quizmaker! Debbie Richards Creative Interactive Ideas Samples and Resources:
1 Direct Manipulation Proposal 17 Direct Manipulation is when physical actions are used instead of commands. E.g. In a word document when the user inputs.
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
CIS—100 Chapter 15—Windows Vista 1. Parts of a Window 2.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
How to use the internet The internet is a wide ranging network that thousands of people use everyday. It is a useful tool in modern society that once one.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
Events Part II Browser Variations Document / Window Events Form Events --- Using named functions (instead of anonymous)
Microsoft ® Office Excel 2003 Training Using XML in Excel SynAppSys Educational Services presents:
Return values Efficiency in Coding. Learning Objectives By the end of this lecture, you should be able to: – Be able to apply an ‘object literal’ when.
Game Maker – Getting Started What is Game Maker?.
Build-A-Button Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, October 8, 2003.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
1 PivotTables and Pivot Charts Cookie Setton for lesson downloads.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Session 2: Basic HTML HTML Coding Spring 2009 The LIS Web Team Presents.
Wimba Pronto Setting Pronto Preferences of the ND University System April 2009.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
When the program is first started a wizard will start to setup your Lemming App. Enter your company name and owner in the fields designated “Company Name”
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
Microsoft® Office FrontPage® 2003 Training
Adding Assignments and Learning Units to Your TSS Course
Removing events Stopping an event’s normal behavior
Web Programming and Design
Events Part I Mouse Events.
Events Part II Browser Variations Document / Window Events Form Events
Events Part III The event object.
Modifying HTML attributes and CSS values
Random Stuff Colors Sizes CSS Shortcuts.
Presentation transcript:

Events Part I Mouse Events

Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate that events are being constnantly generated (“fired”) behind the scenes – Comfortably write simple code to respond to some basic mouse events

JavaScript and Interactivity Perhaps the most powerful and popular feature of JavaScript is it’s ability to make web pages ‘interactive’. By interactive, we mean that the web page can somehow respond to users’ behaviors. You have all seen many of these in action already, though you probably were not thinking about what was going on ‘under the hood’: – Moving a mouse over a button causes the button to change color and a menu to drop down. – Moving a mouse over an image causes the image to pop out in a miniature temporary window. When you move the mouse off of the image, the mini-window disappears. – Checking a checkbox causes a form to be submitted and a small ‘Thank you’ box to be displayed before fading out. – Etc, etc EVENTS: Even though you may not be aware of it, events are occuring constantly as you visit a web page. – Every time you move the mouse over something – even something completely static such as a paragraph of text – an event object (called ‘mouseover’) is generated behind the scenes. – Every time you click on something, an event object (called a ‘mouseclick’) is generated behind the scenes. – Every time you double-click on something, an event object (called a ‘doubleclick’) is generated behind the scenes. – Every time you resize a window, an event object (called a ‘resize’) is generated behind the scenes. – Same thing with key presses, windows being closed, new windows opening, moving a scroll-bar, and many, many others. – Because web browsers automatically generate these event objects in response to certain things happening, we can write code to monitor for these event objects and then react to them when they occur.

Intro to Mouse Events Let’s begin with the all-important mouse click. When you click a mouse anywhere on a web page (it does not have to be over an image or hyperlink), the web browser notes that a “click event” has occurred. As we have mentioned, a user clicking their mouse results in a ‘mouseclick event’ being generated. In fact, it’s even more detailed then that. The browser will actually generate several events in response to what you and I may think of as a simple click of the mouse. A mouse click will certainly result in a ‘mouseclick’ event being generated, but it will also result in each of the following events being generated as well: – Mouse Down event – Mouse Up event – Mouse Move event If we wanted to, we could have our scripts react to every single one of these events. In the real world, however, because there are SO many different events being generated every second, we only write code to respond to the particular events we are interested in. Perhaps you are starting to see how these events can be used to make a page more interactive. For example, you might have a page with a series of images on it. You could then write some code so that whenever the user hovers over an image and presses the mouse button down (but doesn’t let go), the image is increased in size and a highlighted border appears. In other words, you would write the code to respond to a ‘mousedown’ event. Then when the user releases the button (the ‘mouseup’ event), you might write code that responds to that particular event by causing the image to be resized back to its original size and the highlighted border to be removed.

MouseEvents contd: The click() function Recall that whenever a user clicks a mouse on your page, the browser generates several events, one of which is a ‘ mouseclick ’ event. In order to respond to a mouseclick event, we invoke a jQuery function called click(). EXAMPLE: Suppose we wanted to cause an image to have a red border when a user clicks on it. The first step is to add the click function to our image selector: $('img'). click(); Simple enough… However, the key is the code that we want to provide as the argument to the ‘click()’ function. We provide this code using our new friend, the anonymous function! So: $('img').click( anonymous function goes here ); Let’s set up our anonymous function. Remember that we always start with THE OUTLINE ONLY in order to ensure that all of our parentheses and braces, etc are correctly set up: $('img').click ( function() { }) ; //closes the anonymous function, then the ‘)’ of the click() function Once our outline is carefully and correctly done, we can go ahead and write the code for our anonymous function. $('img').click( function() { $(this).css('border','solid'); //why ‘this’?? $(this).css('border-color','red'); }); Finally, we would probably place this code inside our document.ready() function. That way, as soon as the page loads, this command would also be loaded. Now, whenever a person clicks on an image on the page, a red border will appear. TRY IT: Download ‘ page_of_stuff.htm ’ from the class web page (along with the various images) and add the code above block of code inside the document.ready() function.

Pop Quiz Question: In the previous example also shown here, why should we use the ‘this’ keyword to modify the image? Why not simply use ‘img’ as the selector for the modifications? $('img').click( function() { $(this).css('border','solid'); $(this).css('border-color','red'); }); Answer: Had we used ‘img’ as our selector for the modifications like so: $('img').click( function() { $('img').css('border','solid'); $('img').css('border-color','red'); }); then when clicking on any one of the images, EVERY image on the page would have been given a red border. By using ‘ this ’ we are notifying jQuery that whenever an image is clicked, only that particular image should be modified. As always…. Don’t just VIEW it……. TRY IT!!!

‘mouseover’ and ‘mouseout’ events Whenever you move a mouse over an item, a ‘ mouseover ’ event is generated. So if you wanted, say, any ‘h3’ tag on the ‘ page_of_stuff.htm ’ page to change color to red when you move a mouse over it, you could use: $('h3').mouseover(function(){ $(this).css('color','red'); }); NOTES: 1.As with the click() function, the argument to mouseover() is frequently an anonymous function. 2.Again, we use ‘ this ’. Otherwise, when you move a mouse of any ONE h3 item, then EVERY h3 item would become red. 3.As is usually the case, this code should probably be placed inside the document.ready() function so that it will kick in as soon as the page has finished loading. POP-EXERCISE: Once you’ve tried the above code (you DID try it, right??), use the corresponding mouseout() function to return the color back to ‘black’. The idea is that when the user moves the mouse over an h3, the text will turn red, but when they move the mouse off of the h3, the text will return to black. Again, feel free to use the ‘ page_of_stuff.htm ’ page to do this exercise since there are a couple of ‘h3’ tags on it. The solution is on the next slide. But DO NOT look at the solution until you’ve made a genuine attempt to figure it out on your own. Feel free to look at any of the examples up to this point to help you.

Pop-Exercise solution The easiest answer is to simply write a second function for ‘mouseout()’ that will return the ‘h3’ text to black: $('h3').mouseover(function(){ $(this).css('color','red'); }); $('h3').mouseout(function(){ $(this).css('color','black'); });

Quick review of document.ready() Just to illustrate/remind you, there is nothing wrong with having lots of different functionaity written into your document.ready() function. For example, all of the functions described so far in this discussion can be included like so: $(document).ready(function() { //make all images 50x50 pixels $('img').attr('height','50px').attr('width','50px'); $('h3').mouseover(function(){ $(this).css('color','red'); }); $('h3').mouseout(function(){ $(this).css('color','black'); }); $('img').click( function() { $(this).css('border','solid'); $(this).css('border-color','red'); }); }); //end document.ready()

Other mouse events There are a series of other mouse events that you can look for and respond to. A few additional useful or interesting functions are listed here. Only a brief description is given. Refer to a reference to find out more about the particulars of each. Of course, the jQuery API will give you all kinds of information, example, and opportunity to practice. dblclick() : Whereas click() responds to mouse clicks, dblclick() responds to double- clicks. However, double-clicking is not common behavior on web pages. Therefore, writing code to respond to double-clicks should only be used in special situations. mousedown() : This event is generated whenever the user presses the left (on a regular mouse) button down and remains active until they release the button. The most common use for this function in the real world is for dragging items around a page. mouseup() : Not surprisingly, this event is generated whenever the user releases the mouse button. mousemove() : This event fires whenever a mouse moves around the page – even if no button is being pressed. In other words, this event is being continuously fired. Do NOT have an alert pop-up on mouseove() !! Otherwise, you’ll get hundreds and hundreds of alert boxes as you move the mouse around your page! This event is sometimes used to keep track of exactly where the mouse is on the page at a given moment. It is not a commonly used function. Remember that learning to program is a ‘hands-on’ endeavor. That is, the only way to really get the hang of concepts, to reinforce them, and to unmask things you may have missed is to TRY THE CODE! So be sure to experiment with these at least briefly before moving on.