10.05.2018 Views

Web_Designer_UK__May_2018

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPERT ADVICE<br />

Using body labelling<br />

Using JavaScript to apply a status<br />

label to the body section is an easy<br />

way to keep code simple and allowing<br />

visual reactions to be controlled from<br />

CSS. Take this further by applying<br />

additional labels to describe specific<br />

events that have occurred relating to<br />

user interactions or other events.<br />

<br />

What our<br />

experts think<br />

of the site<br />

Create multiple fade background colours<br />

DOWNLOAD TUTORIAL FILES www.filesilo.co.uk/webdesigner<br />

Avoid complexity to avoid headaches<br />

It’s worth spending extra time to identify the simplest way to create effects like<br />

this. Placing too much responsibility within JavaScript increases scope for future<br />

changes breaking functionality, costing time to fix. This example avoids the<br />

problem by keeping JavaScript’s involvement to a minimum.<br />

Leon Brown, freelance web developer and trainer<br />

Technique<br />

1. Initiate HTML document<br />

The first step is to initiate the HTML document that will<br />

be the framework for storing the content and loading<br />

external resources. This document contains a head<br />

section that loads CSS and JavaScript resources, along<br />

with a body section for defining the visible content in<br />

step 2.<br />

<br />

<br />

<br />

Scrolling Background Change<br />

<br />

<br />

<br />

<br />

*** STEP 2 HERE<br />

<br />

<br />

2. Body content<br />

The main content can consist of any HTML, providing<br />

that it covers enough space to allow for scrolling. This<br />

example will use an article container that will be<br />

controlled via CSS to allow for scrolling.<br />

<br />

Content<br />

<br />

3. JavaScript scrolling<br />

Create a new file called ‘code.js’. This JavaScript listens for<br />

a scrolling event, upon which it calculates a screen<br />

number based on the vertical scroll position and sets the<br />

DOM body’s ‘data-screen’ attribute as this value. Any<br />

visual changes to ‘data-screen’ can now be defined via<br />

some CSS.<br />

window.addEventListener(“load”,function(){<br />

window.addEventListener(“scroll”,function(){<br />

var screen = Math.round(window.scrollY/<br />

window.innerHeight);<br />

document.body.setAttribute(“data-screen”,<br />

screen);<br />

})<br />

});<br />

4. CSS screen definition<br />

Create a new file called ‘styles.css’. The ‘data-screen’<br />

attribute applied to the DOM body via the JavaScript is<br />

used to define the default screen properties. Its<br />

background colour is set to white, while a transition is set<br />

to animate changes to the background colour over a<br />

duration of one second.<br />

[data-screen]{<br />

background: #fff;<br />

transition: background-color 1s;<br />

}<br />

5. Screen colour changes<br />

The JavaScript processing of scrolling events from step 3<br />

allows CSS to define presentation rules for specific<br />

positions. Each count of ‘data-screen’ is the full height of<br />

the browser screen. This step sets different background<br />

colours as the user scrolls down the equivalent of a full<br />

screen height.<br />

[data-screen=”1”]{ background-color: red; }<br />

[data-screen=”2”]{ background-color: green; }<br />

[data-screen=”3”]{ background-color: blue; }<br />

6. Example content size<br />

Due to this example not having five screen heights of<br />

content to produce the required scrolling, this step sets<br />

the article container to fit this size. This allows you to test<br />

the effect before committing to inserting the content.<br />

article{<br />

display: block;<br />

min-height: 400vh; }<br />

workshop _____________________________________________ 61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!