|
|
(8 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| =TO DO=
| |
|
| |
| * introduce script tag / JS
| |
| * place examples in codepen
| |
|
| |
|
| |
|
| {{:Courses/Design & Technique-Essential Web Design/Q2/02}} | | {{:Courses/Design & Technique-Essential Web Design/Q2/02}} |
| =jQuery=
| |
|
| |
|
| |
| ==What is jQuery==
| |
| jQuery is a JavaScript library makes programming interactive websites with Javascript, a lot simpler.
| |
|
| |
| There are many JavaScript libraries, that make certain aspects of programming in JS easier - e.g. working with SVG, maps, making visualizations, etc, etc.
| |
|
| |
| jQuery is on of the most popular and general purpose JavaScript libraries
| |
|
| |
| http://jquery.com/
| |
|
| |
| ==JS, made easy==
| |
| Like with JS you can use jQuery to:
| |
| * change HTML content
| |
| * change element attributes
| |
| * change an element style
| |
| * retrieve the window size
| |
| * listen to an even (e.g. a mouse click)
| |
| * make a timer
| |
| * validate data
| |
| But jquery makes your life to much easier, let's see how!
| |
|
| |
| == Structure ==
| |
| I will introduce jQuery in 3 steps:
| |
| * '''What''' elements are manipulated? - jQuery selectors
| |
| * '''How''' are elements manipulated? - jQuery manipulation
| |
| * '''When''' are elements manipulated? - jQuery listeners
| |
|
| |
| ==Loading jQuery library==
| |
| '''jQuery is simply an external JavaScript file that you load on to your HTML page.'''
| |
|
| |
| There are different ways to load it, inside the HTML <head></head> with::
| |
| * '''Download the jQuery file''' (latest version & compressed, production)from http://jquery.com/download/ and loading it:
| |
| <source lang="html4strict">
| |
| <head>
| |
| <script src="jquery-1.11.3.min.js"></script>
| |
| </head>
| |
| </source>
| |
|
| |
| * Load it '''from online resource''' (CDN)
| |
| <source lang="html4strict">
| |
| <head>
| |
| <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
| |
| </head>
| |
| </source>
| |
|
| |
| ==loading your code==
| |
| Once you loaded the jQuery library (file), you need to '''load your jQuery code'''.
| |
|
| |
| the most secure way, that ensures the code is run only after all the contents from the page are loaded is:
| |
|
| |
| <source lang="JavaScript" >
| |
| <script>
| |
| $(document).ready(
| |
| function(){
| |
| //your jQuery/JS code goes here
| |
| })
| |
| </script>
| |
| </source>
| |
|
| |
|
| |
|
| |
|
| |
| == ''What'' elements are manipulated? - jQuery selectors==
| |
| http://api.jquery.com/category/selectors/
| |
|
| |
| In order to change or retrieve HTML elements' content, attributes, style, '''we need to select those element(s) precisely'''. jQuery selector methods are masters at that.
| |
|
| |
| === simple selectors ===
| |
| * '''element selector''': <code>$('h1')</code> will select '''all the heading1 elements'''.
| |
| * '''id selector''': <code>$('#large')</code> will select '''the element with id large'''.
| |
| * '''class selector''': <code>$('.small')</code> will select '''all the elements with class small'''.
| |
|
| |
| <source lang="html4strict">
| |
| <!DOCTYPE html>
| |
| <html>
| |
| <head>
| |
| <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
| |
| </head>
| |
| <body>
| |
| <h1>I am a <u>h1</u></h1>
| |
| <p id="large">I am p#large and will become large</p>
| |
| <p class="rotate">I am p.rotate and will tilt</p>
| |
| <h2 class="rotate">I am a h2.rotate and even I will shift position</h2>
| |
|
| |
| <script>
| |
| $(document).ready(
| |
| function(){
| |
| alert("be alert for the changes in the elements' text and CSS");
| |
| $('h1').text('Yes, your a h1');
| |
| $('#large').css('font-size', '50pt');
| |
| $('.rotate').css('transform', 'skew(0deg, 5deg)');
| |
| })
| |
| </script>
| |
| </body>
| |
| </html>
| |
| </source>
| |
|
| |
| === advanced selectors ===
| |
| * Descendant selectors: <code>$('ul a')</code> will select '''all a tags inside the ul tags'''.
| |
| * Child selectors: <code>$('ul > li')</code> will select '''all li tags that are direct children from the ul tags'''.
| |
| * Attribute selectors: <code>$('img[src="img.jpg"]')</code> will select '''all imgs that have as src the image img.jpg'''.
| |
|
| |
| ==== Attribute selectors example ====
| |
| <source lang="html4strict">
| |
| <!DOCTYPE html>
| |
| <html>
| |
| <head>
| |
| <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
| |
| <style>
| |
| img { display: block;}
| |
| </style>
| |
| </head>
| |
| <body>
| |
| <img src="http://i.giphy.com/EsYu229wwE9R6.gif" name="lpb" />
| |
| <img src="http://i.giphy.com/5WUH6YDabP7hK.gif" />
| |
| <img src="http://i.giphy.com/CVHyjsVMGPqq4.gif" name="lpb" />
| |
| <img src="http://i.giphy.com/10HegwKCnl0krS.gif" />
| |
| <img src="http://i.giphy.com/CDMz3fckRXXDG.gif" />
| |
| <img src="http://i.giphy.com/9ZxHNOvMggGl2.gif" name="lpb" />
| |
| <img src="http://i.giphy.com/j50v9vd9rbhjG.gif" />
| |
| <img src="http://i.giphy.com/YP1hn6KUhDIgo.gif" />
| |
| <img src="http://i.giphy.com/VFzAK3xAaUCpW.gif" name="lpb" />
| |
| <img src="http://i.giphy.com/scGSxIFxfOkSs.gif" name="lpb" />
| |
| <img src="http://i.giphy.com/YP1hn6KUhDIgo.gif" />
| |
| <img src="http://i.giphy.com/zzGluVQkDjQXe.gif" />
| |
| <script>
| |
| $(document).ready(
| |
| function(){
| |
| alert("be alert for what will happen to LPB");
| |
| $('img').css('width', '100'); // all imgs will be 100px width
| |
| $('img[name="lpb"]').css('display','block').css('width', '100%'); //but img[name="lpb"] will be 1000px with
| |
|
| |
| })
| |
| </script>
| |
| </body>
| |
| </html>
| |
| </source>
| |
|
| |
|
| |
| ==''How'' are elements manipulated? - jQuery manipulation==
| |
| http://api.jquery.com/category/manipulation/
| |
|
| |
| In the previous example we have already began manipulating the elements within the page, by using the jQuery method <code>.css()</code> and <code>.text()</code>. Let's look at them more in depth. <u>Will be using example simple selectors</u>.
| |
|
| |
| * <code>.html()</code>
| |
| ** read the html content of an element <code>$('h1').html()</code>
| |
| ** inserts html in the place of content <source lang="javascript">$('p.rotate').html('<span style="background:red">replaces <i>html<i> contente</span>')</source> and
| |
|
| |
| * <code>.text()</code>
| |
| ** like <code>.html()</code>, reads the content of an element, but disregards any contained html tags <code>$('h1').text()</code>
| |
| ** inserts text content of a tag <code>$('h1').text('I am changing you')</code>
| |
|
| |
| * <code>.css()</code>
| |
| ** reads values of css attributes <code>$('.rotate').css('transform')</code>
| |
| ** sets css attributes and values <code>$('.rotate').css('transform', 'matrix(0.8, 0.02, 2, 5, 0, 0)').css('color', '#F205FF')</code>
| |
|
| |
| * <code>.attr()</code>
| |
| ** reads the value of a attribute of <code>$('head > script').attr('src')</code> - reads the attribute source of script tag inside the head
| |
| ** sets elements' attributes <code>$('*').attr('dir', 'rtl')</code> - changes attribute dir (writing direction) from default(ltr) to rtl(right-to-left) for all the elements <code>$('*')</code> in the page
| |
|
| |
| ==''When'' are elements manipulated? - jQuery manipulation==
| |
| http://api.jquery.com/category/events/
| |
|
| |
| Actions like the page loading, moving a mouse, typing a key, or resizing the browser window are events that take place in the web page.
| |
| For creating a interactive web-page, your scripts has to write to respond to events.
| |
|
| |
| In the previous examples we already used 1 listener: <code>$(document).ready()</code> that is triggered with the whole page is loaded.
| |
|
| |
| (lets use adventure time example:)
| |
|
| |
| * <code>.click()</code>
| |
| ** is triggered when the user clicks on the element:
| |
| <source lang="javascript">
| |
| $('img[name="lpb"]').click(
| |
| function(){ $(this).css('width', '250'); }
| |
| )
| |
| </source>
| |
|
| |
| * <code>.mouseover()</code>
| |
| <source lang="javascript">
| |
| $('img[name="lpb"]').mouseover(
| |
| function(){ $(this).css('width', '250'); }
| |
| )
| |
| </source>
| |
|
| |
| * <code>.mouseout()</code>
| |
| <source lang="javascript">
| |
| $('img[name="lpb"]').mouseout(
| |
| function(){ $(this).css('width', '500'); }
| |
| )
| |
| </source>
| |
|
| |
| * <code>.resize()</code>
| |
| <source lang="javascript">
| |
| $(window).resize(
| |
| function(){$('body').prepend('<h1>resizing window</h1>')}
| |
| );
| |
| </source>
| |
|
| |
|
| |
| * <code>.keydown()</code>
| |
| <source lang="javascript">
| |
| $('body').keydown(
| |
| function(){
| |
| var size = Math.random() * (4000 - 100) + 100; //random value btw 100-4000
| |
| $('img').css('width', size); //resize
| |
| var deg = 'rotate('+(Math.random() * (360 - 0) + 0)+'deg)'; //random value btw 100-4000
| |
| $('img').css('transform', deg);
| |
| })
| |
| </source>
| |
|
| |
|
| |
| Example: clicking evil cats: resize, add link;
| |
|
| |
| <source lang="html4strict">
| |
| <!DOCTYPE html>
| |
| <html>
| |
| <head>
| |
| <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
| |
| </head>
| |
| <body>
| |
| <h1>Content...</h1>
| |
| <h1>for jQuery</h1>
| |
| <h1></h1>
| |
| <p id="large">I am p#large and will become large</p>
| |
|
| |
| <p class="rotate">I am p.rotate and will tilt</p>
| |
| <h2 class="rotate">I am a h2.rotate and even I will shift position</h2>
| |
| <img class="rotate" src="http://adorablekittens.com/wp-content/uploads/2015/09/evil-cat.jpg" />
| |
| <br/>
| |
| <img class="rotate" src="https://s-media-cache-ak0.pinimg.com/236x/35/98/76/359876a0f522051e747e878b9a6f551f.jpg" />
| |
|
| |
| <script>
| |
| $(document).ready(
| |
| function(){
| |
|
| |
| $('img').click(
| |
| function(){
| |
| var src = $(this).attr('src');
| |
|
| |
| $(this).css('width', '100px')
| |
| .after("<a href='"+ src +"'>link</a>")
| |
| }
| |
| )
| |
|
| |
|
| |
| }
| |
| )
| |
| </script>
| |
| </body>
| |
| </html>
| |
|
| |
| </source>
| |
|
| |
| ==$(this)==
| |
| in <code>.click()</code> you probably saw a new jQuery selector: <code>$(this)</code>
| |
|
| |
| <code>$(this)</code> address the element that is under "focus".
| |
|
| |
| For example:
| |
| <source lang="javascript">$('img').click(
| |
| function(){
| |
| alert( 'This gif url is ' + $(this).attr('src'))
| |
| })
| |
| </source>
| |
|
| |
|
| |
| =Example=
| |
| ==Interacting with special keys (r, Enter) of the Keyboard==
| |
|
| |
| <source lang="html4strict">
| |
| <!DOCTYPE html>
| |
| <html>
| |
| <head>
| |
| <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
| |
| </head>
| |
| <body>
| |
| <h1>Content...</h1>
| |
| <h1>for jQuery</h1>
| |
| <h1></h1>
| |
| <p id="large">I am p#large and will become large</p>
| |
|
| |
| <p class="rotate">I am p.rotate and will tilt</p>
| |
| <h2 class="rotate">I am a h2.rotate and even I will shift position</h2>
| |
| <img class="rotate" src="http://adorablekittens.com/wp-content/uploads/2015/09/evil-cat.jpg" />
| |
| <br/>
| |
| <img class="rotate" src="https://s-media-cache-ak0.pinimg.com/236x/35/98/76/359876a0f522051e747e878b9a6f551f.jpg" />
| |
|
| |
| <script>
| |
| $(document).ready(
| |
| function(){
| |
|
| |
| $('body').keydown(
| |
| function(pressedkey){
| |
| console.log(pressedkey.key);
| |
| if(pressedkey.key == "r" || pressedkey.key == "A"){
| |
| // alert("r was pressed");
| |
| $('img').attr('src','http://www.pumpkin.uk.com/wp-content/uploads/R3.jpg')
| |
| }
| |
|
| |
| if(pressedkey.key == "Enter"){
| |
| alert("Enter");
| |
| }
| |
|
| |
|
| |
|
| |
| })
| |
|
| |
|
| |
| }
| |
| )
| |
| </script>
| |
| </body>
| |
| </html>
| |
|
| |
|
| |
|
| </source>
| | {{:jQuery}} |
Javascript (JS)
Resources
what is JS?
JS is a simple programming language that runs inside webpages when the pages are loaded in the users' browser.
In the complex organism that can be a web-page, you have:
- HTML - content & structure
- CSS - look
- Javascript - (artificial) intelligence.
You can use JS to
- change HTML content
- change element attributes
- change an element style
- retrieve the window size
- make a timer
- validate data
JS in a HTML page
Javascript appears inside the html page inside the <script>
tag
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<h1>starting javascript-ing</h1>
<p id='greetings' >within the script tag</p>
<script>
alert('Hello Javascript!');
</script>
</body>
</html>
JS in a separate file
The JS is imported from another file.
HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src="hello.js"></script>
</head>
<body>
<h1>starting javascript-ing</h1>
<p>within the script tag</p>
</body>
</html>
JavaScript file: hello.js
alert("hello world! Greetings from javascript");
making statements
JS statements are like phrases, that do very specific tasks.
Each statement has to be terminated with ";"
Like the above alert('Welcome to this page');
the browser console
Essential to prototype, and debug your JS code.
What is jQuery?
jQuery is a library for JavaScript, which is a programming language used to make websites interactive. It's like a set of handy tools that makes it easier for developers to work with JavaScript, allowing them to do things like adding animations and making web pages respond to user actions without writing as much code.
Getting started
1. Prepare your HTML file
Open Visual Studio Code and create a new file. Save the file as index.html, inside of a folder that will contain all the files for this website and noting else. In this file, enter the ! symbol and chose the first option that shows up underneath. This should create a default html file, ready to work with.
2. Add a JavaScript page
Open en new file and save it in the same folder as your html file, name it script.js. In de head of your html document, you should add a link to this JS file:
<script src="script.js"></script>
3. Add jQuery
Before you can use jQuery code in your JavaScript file, you need to load the jQuery library file. the easiest way to do that is to copy the following line of code into the head of your document, above the link to your script file.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
4. Test if it works
Enter the code below in your script.js file to check whether everything works correctly:
$(function() {
alert('Hello world!');
});
This should wait for the page to be loaded and then show a message saying Hello world!