Csit884 7B JSCSS 2022
Csit884 7B JSCSS 2022
Csit884 7B JSCSS 2022
Web Development
CSS with JavaScript
CSS Transitions, and
Transforms
School of Computing and Information
Technology
University of Wollongong
1
Objectives
2
Change style by JavaScript
○ Step 1: give the HTML element that we want to change
an ID
○ Step 2: use the function
var e = document.getElementById("the-id");
to get the HTML element that we want to change
for example:
e.style.color = "pink";
e.style.fontSize = "25px";
e.style.fontStyle = "italic";
...
3
Change style by JavaScript
background-color e.style.backgroundColor
border-bottom-style e.style.borderBottomStyle
border-left-color e.style.borderLeftColor
padding-right e.style.paddingRight
4
Example: change text style
5
Example: change text style
6
Example: change text style
<script>
function changeStyle(){
// get user input from text fields
var colorTf = document.getElementById("colorInput");
var colorValue = colorTf.value;
7
Example: change image style
The web page displays a text field for user to enter image
opacity value. When the user clicks the button, then we
will change the image style.
8
Example: change image style
<button onClick="changeOpacity()">
Change Image Opacity
</button>
9
Example: change image style
<script>
function changeOpacity(){
// get the opacity value
var opacityField = document.getElementById("opacity");
var opacityValue = Number(opacityField.value);
10
Example: change paragraph style
The web page displays text fields for user to enter font
size, padding, text color, background color, border
style. When the user clicks the button, then we will
change the paragraph style.
11
Example: change paragraph style
12
Example: change paragraph style
<script>
function changeStyle(){
// get user input from text fields
var fontSize = document.getElementById("fontSizeInput").value;
var padding = document.getElementById("paddingInput").value;
var color = document.getElementById("colorInput").value;
var bgColor = document.getElementById("bgInput").value;
var border = document.getElementById("borderInput").value;
// change style
var cityPar = document.getElementById("city");
cityPar.style.fontSize = fontSize;
cityPar.style.padding = padding;
cityPar.style.color = color;
cityPar.style.backgroundColor = bgColor;
cityPar.style.borderStyle = border;
}
</script>
13
CSS Transitions
14
CSS Transitions
15
CSS Transitions . . .
.trans2 {
text-align: center;
padding: 1px;
height: 25px;
width: 200px;
color: yellow;
background-color: green;
transition: background-color 3s ease-in-out 1s;
}
.trans2:hover, .trans1:focus {
background-color: blue;
} 16
</style>
CSS Transforms
17
CSS Transforms <style>
div {
what is the output? margin-left:100px;
width: 300px;
height: 50px;
<div> vertical-align: middle;
background-color: yellow;
This a normal div element.
border: 1px solid black;
</div> }
img:hover {
transform: rotate(-50deg);
}
</style>
18
References
● Jennifer Niederst Robbins, Learning Web Design - A Beginner's guide to
HTML, CSS, JavaScript and Web Graphics, 5th edition, O’Reilly Media, 2018.
● http://www.w3schools.com/css
● http://www.w3schools.com/js
● https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
● http://developer.mozilla.org/en-US/docs/Web/JavaScript
● https://developer.mozilla.org/en-
US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions
19