+ All Categories
Home > Documents > JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Date post: 17-Jan-2018
Category:
Upload: cecily-bruce
View: 225 times
Download: 0 times
Share this document with a friend
Description:
Manipulating style We can manipulate style dynamically just like other element properties Each element has an associated style object setting the properties of this object change the element's displayed style editing embedded style
23
JavaScript V Robin Burke ECT 270
Transcript
Page 1: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

JavaScript V

Robin BurkeECT 270

Page 2: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Outline

Manipulating style Special effect examples

Page 3: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Manipulating style

We can manipulate style dynamicallyjust like other element properties

Each element has an associated style objectsetting the properties of this objectchange the element's displayed styleediting embedded style

Page 4: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Manipulating element class

Instead of changing style directlychange an element's classlet CSS define the transformation

Benefitstyle information not buried in

JavaScript elem.className = 'new class'

why not elem.class?!

Page 5: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Example

class-based rollover expandable outline

Page 6: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Visibility

Style property controls whether an element is shown on the page

Why would I want to hide content?so I can show it later, dynamically

Page 7: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Controlling visibility

Visibilityelem.style.visibility = "hidden";

• or "visible", "inherit"• element takes up space but can't be seen

elem.style.display = "none";• or ""• element takes up no space

Page 8: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Example

validation

Page 9: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Modifying a style associated with a class Stylesheets themselves are objects

Accessed via styleSheets[] array Each style sheet has an array of rules

cssRules[] (NS)rules [] (IE)

We can inspect and modify the styleschange affects every element using

the style

Page 10: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Example

rollover

Page 11: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Manipulating position and boundary Position is another style property

also boundary via clip property Many effects possible

animationdrop down menus

Page 12: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Implementation

Relevant DOM properties elem.style.left elem.style.top

Problem these are string values with units

• "5 px", "10 in" Solution

IE-specific• elem.style.pixelLeft• integer valued

use text processing• parseInt to get value• value + "px" to set

Page 13: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Animation

Repeated display with transformation Example animation loop

repeat until animation ended• draw current frame• delay• change frame

Page 14: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Animation, cont'd

Problemlooping wastes computer resourcesworse, prevents user interaction

Solution"animation callback"uses JavaScript event mechanism

Page 15: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Animation callback

Caller sendsthe name of a functiona delay

Event mechanismwaits as specified by the delaycalls the function

SetTimeout (fn, delay)

Page 16: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Animation examples

linear shift path animation snowflakes

Page 17: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Controlling display Clipping

Can set the size of the container smaller than its contents

elem.style.clip = rect(x1, y1, x2, y2);

Page 18: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Controlling display, cont'd

Clipping creates a viewing areacan't access the rest of the image or

contents To shrink viewing area, but allow

accesschange size of the elementset overflow property

Page 19: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

What to do with excess

Use overflow property

Page 20: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Drop-down effects

Clipping can be used for drop-down menuspurely in HTMLno images

Page 21: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Before clipping

After clipping

Page 22: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Note

Book's DOM API is not needed with Mozilla

Book's code is really uglymine is better

Page 23: JavaScript V Robin Burke ECT 270. Outline Manipulating style Special effect examples.

Next Week

No class Monday Wednesday XML

Reading: w3schools tutorial (XML Basic only)


Recommended