Welcome to Javascript / jQuery 101

{ Click one of the links below to get started }

Tutorial 1

{ Highlight an active form field }

Step 1

{ The HTML }

First, we'll begin with the HTML. Here we'll build the form itself. Copy the following code and paste it in between your "body" tags in your HTML doc. Note that while we're using text fields in this particular example, this tutorial will work on buttons, textareas and dropdowns.


<div class="content"> <!-- div to hold the form -->

 <!-- form -->
 <form method="post" name="contact-form" id="contact-form" class="contact-form"> 

  <!-- make sure that for and name match -->
  <label for="first-name">First Name: </label>
   <input id="first-name" name="first-name" type="text" required class=""> 

   <label for="last-name">Last Name: </label>
   <input id="last-name" name="last-name" type="text" required>
  
 </form>

</div> <!-- end content -->

Step 2

{ The CSS }

Next, we'll make the forms look good with CSS. Copy the following code and paste it in your stylesheet (make sure to link your stylesheet to your HTML doc). ".current-field" is where we'll put our input field highlight styles. Note that the styles below are specific to this tutorial; if you want a different look to your form, just change up the CSS.


.content {
	width: 960px;
	margin: 70px auto 0; /* center the form */
}

/* form */

/* general input styles */
input {
	font-family: 'Didact Gothic', sans-serif; /* change to your desired font */
	color: white;
	font-size: 16px;
	background-color: #404040;
	width: 200px;
	min-height: 40px;
	border: 2px solid white;
	letter-spacing: 3px;

}
.contact-form {
	margin: 0 117px;
}
.contact-form label {
	 margin-right: 20px; /* add some spacing after label name*/
}
.contact-form input {
	margin-top: 10px;
	margin-bottom: 30px;
	padding: 10px;

}
.contact-form input:first-of-type {
	margin-right: 40px; /* add some spacing after the first input box */
}

/* highlighted field will take on these styles */
.current-field {
	color: #09abba;
	-webkit-animation: shadow .5s linear both;
	animation: shadow .5s linear both;
	text-align: center;
}
/* add the drop shadow animation */
@-webkit-keyframes shadow {
	0% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
	50% {
		box-shadow: 0 0 20px rgba(0,0,0,.5);
	}
	100% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
}
@-moz-keyframes shadow {
	0% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
	50% {
		box-shadow: 0 0 20px rgba(0,0,0,.5);
	}
	100% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
}
@keyframes shadow {
	0% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
	50% {
		box-shadow: 0 0 20px rgba(0,0,0,.5);
	}
	100% {
		box-shadow: 0 0 0 rgba(0,0,0,.5);
	}
}

Step 3

{ The Javascript }

Finally, we'll implement the Javascript that will link the ".current-field" class to the user's selected field. To do this, we make use of jQuery's "focus" and "blur" methods. If the field is selected, we'll add on the class ".current-field". If the field becomes unselected, we'll remove the class. Note that the script can be placed right before the closing "body" tag or can be linked to an external Javascript file, similar to linking your CSS stylesheet (make sure to remove the "<script>" tags if you're linking to external scripts). Also note that you need to include jQuery library script file before you can implement any other jQuery scripts. You can use the following:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 

<script>

$(document).ready(function(){
	
  // focus = selected field
	$('#first-name').focus(function() { // input field id
		$(this).addClass('current-field') // our class name
  // blur = unselected
	});
	$('#first-name').blur(function() {
		$(this).removeClass('current-field')
	});
	
	$('#last-name').focus(function() {
		$(this).addClass('current-field')
	});
	$('#last-name').blur(function() {
		$(this).removeClass('current-field')
	});

});

</script>

Tutorial 2

{ Parallax Scrolling }

Step 1

{ The HTML }

First, we'll begin with the HTML. Here, we'll load all the images that we're going to use for our day to night scrolling effect. You can download the images I used here:
Stars / Clouds / Sun / Moon


<body class="start">

<main>

	<div id="fg">

    <img src="images/noun_7551_cc.svg" width="700" height="auto" alt="sun" class="sun"/>
    <img src="images/noun_25004.svg" width="500" height="auto" alt="moon" class="moon"/>
  
  </div> <!-- end fg -->
  
  <div id="mg">
  
  	
    <img src="images/noun_57180_cc.svg" width="200" height="auto" alt="clouds" class="clouds clouds-03"/>
    <img src="images/noun_57180_cc.svg" width="200" height="auto" alt="clouds" class="clouds clouds-05"/>
    <img src="images/noun_57180_cc.svg" width="500" height="auto" alt="clouds" class="clouds clouds-06"/>
    
  
    <img src="images/noun_42391_cc.svg" width="200" height="auto" alt="stars" class="stars stars-01"/>
    <img src="images/noun_42391_cc.svg" width="150" height="auto" alt="stars" class=" stars stars-02"/>
    
  </div>> <!-- end mg -->
  
  <div id="bg">
  
  	<img src="images/noun_57180_cc.svg" width="400" height="auto" alt="clouds" class="clouds clouds-02"/>
    <img src="images/noun_57180_cc.svg" width="300" height="auto" alt="clouds" class="clouds clouds-04"/>
  	<img src="images/noun_57180_cc.svg" width="300" height="auto" alt="clouds" class="clouds clouds-01"/>
    
    <img src="images/noun_42391_cc.svg" width="300" height="auto" alt="stars" class="stars stars-03"/>
    <img src="images/noun_42391_cc.svg" width="100" height="auto" alt="stars" class=" stars stars-04"/>
    <img src="images/noun_42391_cc.svg" width="300" height="auto" alt="stars" class=" stars stars-05"/>
    <img src="images/noun_42391_cc.svg" width="300" height="auto" alt="stars" class="stars stars-06"/>
  
  
  </div> <!-- end bg -->


</main> <!-- end main -->

</body>

Step 2

{ The CSS }

Now comes the tricky part. Here is where we will be placing all the images around the screen. Absolutely positioning the different image elements requires a lot of guess and check. The following code positions the elements where I liked them, but feel free to mess around with the top and left values for a different look.


body {
	min-height: 20000px;	
	width: 120%;
	-webkit-transition: background-color 2s ease 0s;
	-moz-transition: background-color 2s ease 0s;
	-ms-transition: background-color 2s ease 0s;
	-o-transition: background-color 2s ease 0s;
	transition: background-color 2s ease 0s;
}

/* our starting background color */
.start {
	background-color: white;
}

/* our blue sky */
.blue {
	background-color: #62D0FF;
	-webkit-transition: background-color 2s ease 0s;
	-moz-transition: background-color 2s ease 0s;
	-ms-transition: background-color 2s ease 0s;
	-o-transition: background-color 2s ease 0s;
	transition: background-color 2s ease 0s;
}

/* our sunset */
.purple {
	background-color: #742D78;
	-webkit-transition: background-color 2s ease 0s;
	-moz-transition: background-color 2s ease 0s;
	-ms-transition: background-color 2s ease 0s;
	-o-transition: background-color 2s ease 0s;
	transition: background-color 2s ease 0s;
}

/* our night sky */
.black {
	background-color: black;
	-webkit-transition: background-color 2s ease 0s;
	-moz-transition: background-color 2s ease 0s;
	-ms-transition: background-color 2s ease 0s;
	transition: background-color 2s ease 0s;
}

#fg,
#mg,
#bg {
	position: fixed;	
}

#fg {
	left: 50%;
  top: 0;
  width: 960px;
  margin-left: -368px;
	z-index: 3;	 /* the foreground elements will lie on top of everything else */
}

.sun {
	position: absolute;
	top: 1700px;
	left: 50px;
	-webkit-animation: rotate linear 60s infinite alternate both;
	animation: rotate linear 60s infinite alternate both;
}

/* animation for rotating the sun */

/* keyframes for rotate */
/* webkit */
@-webkit-keyframes rotate { 
	
	0% {
		-webkit-transform: rotate(0deg);
		transform: rotate(0deg);
	}
	100% {
		-webkit-transform: rotate(360deg);
		transform: rotate(360deg);
	}
	
}

/* moz */
@-moz-keyframes rotate { 
	
	0% {
		-webkit-transform: rotate(0deg);
		transform: rotate(0deg);
	}
	100% {
		-webkit-transform: rotate(360deg);
		transform: rotate(360deg);
	}
	
}

/* keyframes */
@keyframes rotate { 
	
	0% {
		-webkit-transform: rotate(0deg);
		transform: rotate(0deg);
	}
	100% {
		-webkit-transform: rotate(360deg);
		transform: rotate(360deg);
	}
	
}

/* end animation */

.moon {
	position: absolute;
	top: 5000px;
	left: 125px;
}

#mg {
	left: 50%; 
	z-index: 2;	/* our midground elements will be place after the foreground */
	top: 0;
  width: 960px;
  margin-left: -480px;
}

#bg {
	z-index: 1;	 /* our background elements will be placed behind all other elements */
	left: 50%; 
	top: 0;
  width: 960px;
  margin-left: -480px;
}

.clouds-01 {
	position: absolute;
	top: 1100px;
	left: -200px;
}

.clouds-02 {
	position: absolute;
	top: 600px;
	left: 200px;
}

.clouds-03 {
	position: absolute;
	top: 850px;
	left: -200px;
}

.clouds-04 {
	position: absolute;
	top: 975px;
	left: 900px;
}

.clouds-05 {
	position: absolute;
	top: 727px;
	left: 845px;
}

.clouds-06 {
	position: absolute;
	top: 1300px;
	left: 400px;
}

.stars-01 {
	position: absolute;
	top: 5250px;
	left: 800px;
}

.stars-02 {
	position: absolute;
	top: 5370px;
	left: -100px;
}

.stars-03 {
	position: absolute;
	top: 4000px;
	left: -300px;
}

.stars-04 {
	position: absolute;
	top: 4000px;
	left: 900px;
}

.stars-05 {
	position: absolute;
	top: 3680px;
	left: 300px;
}

.stars-06 {
	position: absolute;
	top: 4300px;
	left: 900px;
}

.stars {
	-webkit-animation: rotate-stars linear 10s infinite alternate both;
	animation:  rotate-stars linear 10s infinite alternate both;
}

/* animation to rotate the stars */

/* keyframes for rotate */
/* webkit */
@-webkit-keyframes rotate-stars{ 
	
	0% {
		-webkit-transform: rotate(-10deg);
		transform: rotate(-10deg);
	}
	100% {
		-webkit-transform: rotate(10deg);
		transform: rotate(10deg);
	}
	
}

/* moz */
@-moz-keyframes rotate-stars{ 
	
	0% {
		-webkit-transform: rotate(-10deg);
		transform: rotate(-10deg);
	}
	100% {
		-webkit-transform: rotate(10deg);
		transform: rotate(10deg);
	}
	
}

/* keyframes */
@keyframes rotate-stars{ 
	
	0% {
		-webkit-transform: rotate(-10deg);
		transform: rotate(-10deg);
	}
	100% {
		-webkit-transform: rotate(10deg);
		transform: rotate(10deg);
	}
	
}
 

Step 3

{ The Javascript }

To achieve the both the changing background effect and the parallax scrolling effect, we're going to implement two scripts. The first will allow us to change the background color on scroll. The second will give us the parallax effect. You can add both these scripts right before your closing "body" tag or by externally linking them (make sure to remove the "<script>" tags if you're linking to external scripts). Also note that you need to include jQuery library script file before you can implement any other jQuery scripts. You can use the following:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 

<script>

var scrollPosition = $(window).scrollTop();

var $body = $('body');

$(window).scroll(function(){ // Add a scroll event handler
	
	var pos = $(window).scrollTop(); // position of top of page window
	
	if(pos === 0){
		$body.attr('class','white');
	}
	
	if(pos > 700){
		$body.attr('class','blue'); // as the user scrolls down, we attach a different class to the body
	}
	
	if(pos > 8500){
		$body.attr('class','purple');
	}
	
	if(pos > 13000){
		$body.attr('class','black'); 
	}

});

</script> 

<script>

$(window).on('scroll', function(){ // As the user  scrolls, the function inside will run 
	
	// get scrollTop position
	var scrollPos = $(window).scrollTop(); // position of top of page window
	
	$('#fg').css('top', 0 - (scrollPos * .25)) // the speed at which our foreground moves as the user scrolls
	$('#mg').css('top', 0 - (scrollPos * .3)) // the speed at which our midground moves as the user scrolls
	$('#bg').css('top', 0 - (scrollPos * .2)) // the speed at which our background moves as the user scrolls
	
});

</script>
  

Tutorial 3

{ Add a Click Event to an Image }

Step 1

{ The HTML }

The HTML is fairly straightfoward in this example. However, you can attach click events to anything the user can click on, including buttons or text. Here we're simply going to set up the lightbulb image and the text below it. This code will go between your "body" tags. You can download the images below:
Lightbulb / Ghost / Background


<main>

	<div class="lightbulb">
  
  	<img class="lightbulb-img" src="images/lightbulb.png" width="400" height="400" alt="Lightbulb" />
    
  </div> <!-- end lightbulb -->
  
  <div class="off-on">
  
  	<p>Click the lightbulb to turn the lights off</p<
  
  </div> <!-- off-on -->

</main> <!-- end main -->

Step 2

{ The CSS }

Next, we'll add in the CSS. Here we'll add in the background image and some effects to the images. Though the ghost image isn't visible yet, it will be once we add in the script, so you'll want to style that as well. This is good practice in using CSS animations and filter effects. Feel free to mess around with the CSS to create your own look as well. Make sure to link your stylesheet to your HTML file in between your "head" tags


body {
	background-image:url(../images/black.jpg);
	font-family: 'Medula One', cursive;
	font-size: 30px;
	letter-spacing: 5px;
}

main {
	width: 960px;
	margin: 0 auto;
}

.lightbulb {
	text-align: center;
	margin-top: 40px;
	-webkit-animation-name: glow;
	animation-name: glow;
}

.off-on {
	margin-top: 30px;
	text-align: center;
	color: white;
}

.ghost {
	width: 400px;
	height: 400px;
	-webkit-animation: move linear 10s infinite alternate both;
	animation: move linear 10s infinite alternate both;
	cursor: pointer;
}

@-webkit-keyframes move {
	
	
	25% {
		-webkit-transform: translate(40px,20px);
		-moz-transform: translate(40px,20px);
		transform: translate(40px,20px);	
	}
	
	50% {
		-webkit-transform: translate(-100px,-20px);
		-moz-transform: translate(-100px,-20px);
		transform: translate(-100px,-20px);	
	}
	
	75% {
		-webkit-transform: translate(50x,-40px);
		-moz-transform: translate(50px,-40px);
		transform: translate(50px,-40px);
	}
	
	100% {
		-webkit-transform: translate(0,0);
		-moz-transform: translate(0,0);
		transform: translate(0,0);
		
	}
	
}

@-moz-keyframes move {
	
	
	25% {
		-webkit-transform: translate(40px,20px);
		-moz-transform: translate(40px,20px);
		transform: translate(40px,20px);
	}
	
	50% {
		-webkit-transform: translate(-100px,-20px);
		-moz-transform: translate(-100px,-20px);
		transform: translate(-100px,-20px);
	}
	
	75% {
		-webkit-transform: translate(50x,-40px);
		-moz-transform: translate(50px,-40px);
		transform: translate(50px,-40px);
	}
	
	100% {
		-webkit-transform: translate(0,0);
		-moz-transform: translate(0,0);
		transform: translate(0,0);
		
	}
	
}

@keyframes move {
	
	
	25% {
		-webkit-transform: translate(40px,20px);
		-moz-transform: translate(40px,20px);
		transform: translate(40px,20px);
	}
	
	50% {
		-webkit-transform: translate(-100px,-20px);
		-moz-transform: translate(-100px,-20px);
		transform: translate(-100px,-20px);
	}
	
	75% {
		-webkit-transform: translate(50x,-40px);
		-moz-transform: translate(50px,-40px);
		transform: translate(50px,-40px);
	}
	
	100% {
		-webkit-transform: translate(0,0);
		-moz-transform: translate(0,0);
		transform: translate(0,0);
		
	}
	
}

Step 3

{ The Javascript }

To add the click event and switch between the "lights on" and "lights off" screens, we'll need some Javascript. This script is a modified version of the lightbox script. Note that the script can be placed right before the closing "body" tag or can be linked to an external Javascript file, similar to linking your CSS stylesheet (make sure to remove the "<script>" tags if you're linking to external scripts). Also note that you need to include jQuery library script file before you can implement any other jQuery scripts. You can use the following:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 

<script>

var $light = $('.lightbulb-img');

$light.click(function(e) { // create a click event handler on our lightbulb image
	
	e.preventDefault();
	
	// Disable all scrollbars on the body
	$('body').css('overflow-y', 'hidden')
	
	// Dynamically create an overlay window
	$('
').css({ 'background-color': '#000000', 'position': 'fixed', 'top': 0, 'left': 0, 'width': '100%', 'height': '100%', 'opacity': 1 }).appendTo('body'); // Create a container to contain our image $('
').hide().appendTo('body'); // Load the image into our container $('') // set the newly created image's src attribute to the ghost image .attr('src', 'images/noun_3941.svg') // load() function fires when the image has finished downloading .load(function(){ // once the image has loaded run this code $('.ghost').center().fadeIn(); }) // end load function // attach a click event handler to the newly added image .click(function(){ // remove the ghost container from the screen $('.overlay, .ghost').fadeOut('fast', function(){ // this code will run when the ghost container has faded out $(this).remove(); $('body').css('overflow-y', 'auto'); }) // end fadeOut function }) // end click function // append the image to the ghost container .appendTo('.ghost'); // end img }); // end click function // Adds a center element function to jQuery jQuery.fn.center = function () { this.css('position', 'absolute'); this.css('top', ( $(window).height() - this.height() ) / 2 + $(window).scrollTop()); this.css('left', ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft()); return this; }; </script>

Contact

{ Fill out the form below }

Thank You

{ We'll get back to you as soon as possible! }