More javascript. Where is the cursor? (similar to my example at end of last ppt)

Slides:



Advertisements
Similar presentations
© Park University, 2006 Creating an Interactive Web Page with DHTML and JavaScript Park University.
Advertisements

CS 142 Lecture Notes: EventsSlide 1 Access to Event Object ● event variable (HTML): ● Passed as argument to function (DOM/Firefox): element.onclick = mouseClick;
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Chapter 6 © 2012 by Addison Wesley Longman, Inc Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element.
Javascript and dynamic doc presentation. Absolute positioning using CSS Absolute positioning /* A style for a paragraph of text */.regtext {font-family:
Cascading Style Sheets SP.772 May 6, CSS Useful for creating one unified look for an entire web site. Helps to seperate style from content. Can.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 12.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
Using color and fonts in HTML and XHTML Please use speaker notes for additional information!
Tutorial #8 – Creating Data Tables. Tutorial #6 Review – Layouts Two Column Fixed Width, Three Column Fixed Width Class VS. ID Container Universal Selector.
HIGHER COMPUTING CSS. WHAT IS CSS? CSS: Cascaded Style Sheets used to separate a web site’s content(information) from its style(how it looks).
Javascript and the Web Whys and Hows of Javascript.
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
W. W. Milner JavaScript. W. W. Milner Chapter 1 - Intro Why use JavaScript? Interactivity LiveScript JavaScript Jscript ECMAScript JavaScript is not Java!
Tutorial #3 Cascading Style Sheets. Review: Last Class Image sizing Pathnames Project Default Path Relative Path Absolute Path Blackboard Homework Submission.
Tutorial 4 Creating Special Effects with CSS
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Dynamic Documents With JavaScript.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
CS346 Javascript-7A Events1 DOM Document Object Model and Events.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
CIS67 Foundations for Creating Web Pages Professor Al Fichera Rev. September 14, 2010—All HTML code brought to XHTML standards. Reference for CIS127 and.
Lab 3: Language Structures User Interface Lab: GUI Lab Sep. 9 th, 2014.
CSS My favourite ICT lesson By Federico Boschi Cascading Style Sheets.
Just A Few More Fun Objectives 1 Having Some Fun With Java Script 2 Using Style Sheets.
Tutorial #3 Cascading Style Sheets. Tutorial #2 Review - Anchors Links to Site DMACC Internal Links Go to Top Mail To me Local.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Cascading Style Sheets Class 2, Lecture 2 Rachel A Ober
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.
More CSS.
Chapter 8 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 8 Sebesta: Programming the World Wide Web.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Web Development 101 Presented by John Valance
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JavaScript Challenges Answers for challenges
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Web Technologies Beginning Cascading Style Sheets (CSS) 1Copyright © Texas Education Agency, All rights reserved.
Project Two Adding Scrolling Messages! Page 2.4 to 2.18 Today’s Agenda Look at scrolling message coding. Create a web page with a text box. Create a web.
Host Objects: Browsers and the DOM
Chapter 6 © 2014 by Pearson Education Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties.
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
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.
JavaScript and Ajax (JavaScript Dynamic HTML (DHTML)) Week 7 Web site:
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Chapter 6 © 2003 by Addison-Wesley, Inc Introduction - Dynamic HTML is not a new markup language - A dynamic HTML document is one whose tag attributes,
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Changing CSS Styles via JavaScript No readings?. 2 Review? You can change the CSS styling of an element with JavaScript Syntax is similar to accessing.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - Dynamic HTML: Path Sequencer and Sprite ActiveX Controls Outline 18.1 Introduction 18.2.
6.1 Introduction 6.2 Element Positioning
Introduction to.
Introduction to JavaScript Events
Introduction to Programming the WWW I
CSE 154 Lecture 11: More Events.
Events Comp 205 Fall 2017.
Introduction to DHTML, the DOM, JS review
Web Programming and Design
Web Programming and Design
6.1 Introduction 6.2 Positioning Elements
Presentation transcript:

More javascript

Where is the cursor? (similar to my example at end of last ppt)

Where.html text example <!-- // ****************************************************** // // The event handler function to get and display the // coordinates of the cursor, both in an element and // on the screen function findIt(evt) { document.getElementById("xcoor1").value = evt.clientX; document.getElementById("ycoor1").value = evt.clientY; document.getElementById("xcoor2").value = evt.screenX; document.getElementById("ycoor2").value = evt.screenY; } // ****************************************************** // // -->

Html body Within the client area: x: y: Relative to the origin of the screen coordinate system: x: y:

Anywhere you click…

Anywhere.html functions <!-- // **************************************************** // The event handler function to display the message function displayIt(evt) { var dom = document.getElementById("message"); dom.style.left = (evt.clientX - 130) + "px"; dom.style.top = (evt.clientY - 25) + "px"; dom.style.visibility = "visible"; } // **************************************************** // The event handler function to hide the message function hideIt() { document.getElementById("message").style.visibility = "hidden"; } // **************************************************** // -->

body <body onmousedown="displayIt(event);" onmouseup = "hideIt();" <span id= "message" style = "color: red; visibility: hidden; position: relative; font-size: 20pt; font-style: italic; font-weight: bold;"> Please don't click here!

Moving something (like text) across the screen Moving text <script type = "text/javascript" src = "moveTextfuns.js"> <!-- Call the initializing function on load, giving the destination coordinates for the text to be moved --> <span id = 'theText' style = "position: absolute; left: 100px; top: 100px; font: bold 20pt 'Times Roman'; color: blue;"> Jump in the lake!

The js file var dom, x, y, finalx = 300, finaly = 300;//globals // ************************************************* // // A function to initialize the x and y coordinates // of the current position of the text to be moved, // and then call the mover function function initText() { dom = document.getElementById('theText').style; /* Get the current position of the text */ var x = dom.left; var y = dom.top; /* Convert the string values of left and top to numbers by stripping off the ‘ px’ units */ x = x.match(/\d+/); y = y.match(/\d+/); /* Call the function that moves it */ moveText(x, y); } /*** end of function initText */

Js file continued // A function to move the text from its original // position to (finalx, finaly) function moveText(x, y) { /* If the x coordinates are not equal, move x toward finalx */ if (x != finalx) if (x > finalx) x--; else if (x < finalx) x++; /* If the y coordinates are not equal, move y toward finaly */ if (y != finaly) if (y > finaly) y--; else if (y < finaly) y++; /* As long as the text is not at the destination, call the mover with the current position */ if ((x != finalx) || (y != finaly)) { /* Put the units back on the coordinates before assigning them to the properties to cause the move */ dom.left = x + "px"; dom.top = y + "px"; /* Recursive call, after a 1-millisecond delay */ setTimeout("moveText(" + x + "," + y + ")", 1); } } /*** end of function moveText */

Alternatively? I spent some time fiddling with this recursive version trying to get an iterative version working. I never got one which worked as nicely but the next couple of slides show code for iterating through the changes instead of recursing.

Js file for iterative move-text // This is moveTextAlt.js - used with moveText2.html var dom, x, y, finalx = 500, finaly = 500; // ************************************************* // // A function to initialize the x and y coordinates // of the current position of the text to be moved, // and then call the mover function function initText() { dom = document.getElementById('theText').style; /* Get the current position of the text */ var x = dom.left; var y = dom.top; /* Convert the string values of left and top to numbers by stripping off the units */ x = x.match(/\d+/); y = y.match(/\d+/); /* Call the function that moves it */ moveText(x, y); } /*** end of function initText */ function update(x,y){ dom.left=x + "px"; dom.top=y +"px"; }

Js file for iterative move-text // ************************************************* // // A function to move the text from its original // position to (finalx, finaly) function moveText(x, y) { while(x!=finalx||y!=finaly){ if (x > finalx) x--; else if (x < finalx) x++; if (y > finaly) y--; else if (y < finaly) y++; setTimeout("update("+x+","+y+")", 1000); // if(x!=finalx||y!=finaly) // setTimeout("moveText("+x+","+y+")", 5); }//while } /*** end of function moveText */

Continuously moving image

Just the recursive function function movePic(x, y) { /* If the x coordinates are not equal, move x toward finalx */ if (x >= finalx||x<=10) xinc=-1*xinc; if (y >= finaly||y<=10) yinc=-1*yinc; x=x+xinc; y=x+xinc; /* Put the units back on the coordinates before assigning them to the properties to cause the move */ dom.left = x + "px"; dom.top = y + "px"; /* Recursive call, after a 1-millisecond delay */ setTimeout("movePic(" + x + "," + y + ")", 1); } /*** end of function movePic */

Magnetic poetry drag-n-drop

Javascript functions: grabber var diffX, diffY, theElement;//globals // The event handler function for grabbing the word function grabber(event) { // Set the global variable for the element to be moved theElement = event.currentTarget; // Determine the position of the word to be grabbed, // first removing the units from left and top var posX = parseInt(theElement.style.left); var posY = parseInt(theElement.style.top); // Compute the difference between where it is and where the mouse click occurred diffX = event.clientX - posX; diffY = event.clientY - posY; // Now register the event handlers for moving and dropping the word document.addEventListener("mousemove", mover, true); document.addEventListener("mouseup", dropper, true); // Stop propagation of the event and stop any default browser action event.stopPropagation(); event.preventDefault(); } //** end of grabber

Move and drop functions // ******************************************************* // The event handler function for moving the word function mover(event) { // Compute the new position, add the units, and move the word theElement.style.left = (event.clientX - diffX) + "px"; theElement.style.top = (event.clientY - diffY) + "px"; // Prevent propagation of the event event.stopPropagation(); } //** end of mover // ********************************************************* // The event handler function for dropping the word function dropper(event) { // Unregister the event handlers for mouseup and mousemove document.removeEventListener("mouseup", dropper, true); document.removeEventListener("mousemove", mover, true); // Prevent propagation of the event event.stopPropagation(); } //** end of dropper

A lot of this in the html <span style = "position: absolute; top: 200px; left: 0px; background-color: lightgrey;" onmousedown = "grabber(event);"> candy <span style = "position: absolute; top: 200px; left: 75px; background-color: lightgrey;" onmousedown = "grabber(event);"> cats <span style = "position: absolute; top: 200px; left: 150px; background-color: lightgrey;" onmousedown = "grabber(event);"> cows And so on…see text