323 107 543KB
English Pages [160]
JavaScript and JQuery The Ultimate Beginner's Guide to Learn JavaScript and JQuery Step by Step
By John Bach
1nd Edition
"Programming isn't about what you know; it's about what you can figure out.” - Chris Pine
Memlnc.com
INTRODUCTION
8
EXAMPLE
8
THE SYNTAX DIFFERENCE
9
JAVASCRIPT BASICS AND COMMON ERRORS COMMITTED BY BEGINNERS 9 NOTE ON COMMENTS
11
THE CORE COMPONENTS OF JAVASCRIPT
12
VARIABLES
12
FUNCTIONS
13
FUNCTIONS AND VARIABLE SCOPE
15
OBJECTS
18
ACCESS TO PROPERTIES
19
OBJECTS IN JQUERY
26
MORE DETAILS
27
ARRAYS
27
FOR LOOPS: LIST THE ARRAYS LOGIC AND VALIDATION
29
29
LOGICAL OPERATORS TERNARY OPERATOR JAVASCRIPT BUGS NAME THINGS RESERVED WORDS OPERATIONS ON NUMBERS AND STRINGS
32 33 34 34 35 36
CHAPTER I……………………………………………………………………….38 JQUERY BASICS
38
WHAT IS THIS SYMBOL: $? ACCESS TO ITEMS
38 41
OTHER WAYS TO CREATE A JQUERY OBJECT
41
DOES YOUR SELECTION CONTAIN ANY ELEMENTS?
42
ACCESS INDIVIDUAL ITEMS FROM A SELECTION CREATE NEW OBJECTS
43 44
DEALING WITH SELECTIONS
45
IDENTIFICATION CHECK CHAIN (CHAINING) CONCLUSION
46 48 50
CHAPTER II …………………………………………………………………….50 JQUERY LEARN PAGE NAVIGATION AND EDITING FUNCTIONS IN JQUERY 50 TRAVERSAL FILTER SELECTIONS
51 51
FIND ITEMS STARTING WITH A SELECTION
52
RETURN TO THE ORIGINAL SELECTION
54
MANIPULATION OF ELEMENTS EDIT ITEMS
56 56
ADD OR DELETE CLASSES
57
CHANGE OF APPEARANCE
58
CHANGING THE VALUES OF FORMS
59
CHANGE OTHER ATTRIBUTES
61
GET INFORMATION FROM THE ELEMENTS ADD ITEMS TO THE PAGE COPY ITEMS DELETE ITEMS CONCLUSION
61 62 64 65 67
CHAPTER III …………………………………………………………………..68 JQUERY EVENTS AND HOW TO DELEGATE THEM
68
CONFINE THE EVENTS WITHIN THE NAMESPACE
71
CONNECT MULTIPLE EVENTS SIMULTANEOUSLY
72
PASS THE FUNCTIONS NAMED EVENTS
73
EVENT OBJECT
74
INSIDE THE SPONSORS
74
PREVENTING INITIAL BEHAVIOR
75
EVENT BUBBLING
76
EVENT DELEGATION
77
CONCLUSION
79
CHAPTER IV…………………………………………………………………… MOVE THE PAGE WITH JQUERY
79
EFFECTS INCLUDED WITH JQUERY
80
CUSTOM EFFECTS USING .ANIMATE ()
83
MOVEMENT MANAGEMENT
85
CHAPTER V…………………………………………………………………. INTRODUCED TO AJAX AND DEFERRED OBJECTS ON JQUERY
85
AJAX
85
$ .AJAX
86
A IN AJAX MEANS "ASYNCHRONOUS"
88
X IN AJAX MEANS JSON! SHORT FUNCTIONS SUBMIT DATA AND WORK WITH FORMS
79
89 91 92
85
JQXHR
93
JSONP
95
DEFERRED OBJECTS
97
$ .DEFERRED
97
.PIPE ()
99
DEALING WITH OPERATIONS THAT MAY BE ASYNCHRONOUS 100
CHAPTER V…………………………………………………………
103
HOW TO CREATE A MOVEABLE SLIDESHOW WITH JQUERY 103 EXPLAIN THE STEPS IN THIS LESSON FOR CREATING A JAVASCRIPT SLIDE SHOW 104 NOW LET'S LOOK AT CSS STYLES
107
FINALLY, LET'S LOOK AT JQUERY FUNCTIONS
109
CHAPTER VII…………………………………………………………
111
10 JQUERY TRICKS THAT EVERY DESIGNER SHOULD KNOW 111 BACK TO TOP BUTTON CHECK UPLOAD PHOTOS CORRECT DAMAGED PHOTOS AUTOMATICALLY SUBSTITUTION OF THE VARIETY WHEN HOVER
112 113 113 114
DISABLE INPUT FIELDS
114
STOP DOWNLOADING LINKS
115
SWITCH BETWEEN FADE AND SLIDE COSPLAY
115
FOLDING EFFECT
116
DETERMINE THE HEIGHT OF THE DIV ELEMENT DEPENDING ON THE POST 116 LIST OF DIFFERENT COLORS ACCORDING TO THE EVEN AND ODD ELEMENTS 117 CHAPTER VIII …………………………………………………….. 117
HEADERS AND STATIC COLUMN HTML TABLES WITH JQUERY 117 SOLVE CSS USING POSITION: STICKY?
118
SOLVE USING JQUERY
118
WHAT ARE WE TRYING TO ACCOMPLISH? CSS TO GET STARTED USING JAVASCRIPT STEP 1: COPY THE ELEMENT STEP 2: ENCAPSULATE AND COPY THE TABLE
119 120 122 123 123
STEP 3: SETTING THE CONTENT OF THE COPIED TABLES 124 STEP 4: FUNCTIONS
125
STEP 5: CONNECT EVERYTHING
131
THE DEBATE
132
USE POSITION: FIXED
133
USE POSITION: STICKY
133
MEM T
Introduction
jQuery works by using functions, a library, written in JavaScript. You can use these functions to write powerful things without actually typing much code. jQuery is very lightweight and efficient so you don’t have to worry about it slowing down the user experience.
Referencing jQuery is extremely easy and there are multiple ways to do it. Remember that you can reference JavaScript from anywhere. You could even take the JavaScript that I am writing for this website and use it. The first way uses this principle. Google has a jQuery library available for all developers to use.
Example
. If the conditions are not met, our function will ignore this table. $ (function () { // Here we select all tables on the page // But you are free to specify the tables you want $ ('table'). each (function () { if ($ (this) .find ('thead'). length> 0 && $ (this) .find ('th'). length> 0) { // The rest of the script will be here } }); }); Step 1: Copy the element // Define variables and some shortcuts var $ t = $ (this), $ w = $ (window), $ thead = $ (this) .find ('thead'). clone (), $ col = $ (this) .find ('thead, tbody'). clone (); Step 2: Encapsulate and copy the table In order to support cases where the table is wider than what is permitted (i.e. when it is when a large number of columns, or long columns, we fold the table in so that we allow you to be scrollable on the horizontal axis: // Contain table $t .addClass ('sticky-enabled') .css ({ margin: 0, width: '100%'; }) .wrap (''); // Check if we have specified that the table be scrolling along the horizontal axis if ($ t.hasClass ('overflow-y')) $ t.removeClass ('overflow-y'). parent (). addClass ('overflow-y'); // Create a new table header with a .stiky-head class $ t.after (' // If contains | we create a new column so that the cell is above the table if ($ t.find ('tbody th'). length> 0) { $ t.after (' Step 3: Setting the content of the copied tables What we will do now is take the copied content from the original table and put it in the new tables which will be attached: The new table header will receive all of the content from the copied element. Conjoined columns will receive content from the first | element of and all remaining | elements from . Merging the column with the row (i.e. the column-row common cell) will take content through the top column to the right of the table (assuming we are dealing with the page on RTL basis). // StickyHead gets content from $ stickyHead.append ($ thead); $ stickyCol .append ($ col) .find ('thead th: gt (0)'). remove () .end () .find ('tbody td'). remove (); // StickyIntersect gets content from | in $ stickyInsct.html (' |
---|
' + $ t.find ('thead th: first-child'). html () + ' | elements in the header of a copied table, since we just copied the element, the width of the copied header will not be the actual header width, because the width of was not added since we don't know whether it will affect the header Page or not. Function for locating the fixed header position so that we update the horizontal copied header dimension, which we set position: absolute when we begin to pass the progress bar within the table. Function for locating the static side column and having the same header installation status. A function to calculate the remaining space, and we will explain this function later in more depth. // Function 1: setWidths () // Purpose: To set width of individually cloned element var setWidths = function () { $t .find ('thead th'). each (function (i) { $ stickyHead.find ('th'). eq (i) .width ($ (this) .width ()); }) .end () .find ('tr'). each (function (i) { $ stickyCol.find ('tr'). eq (i) .height ($ (this) .height ()); }); // Set width of sticky table head $ stickyHead.width ($ t.width ()); // Set width of sticky table col $ stickyCol.find ('th'). add ($ stickyInsct.find ('th')). width ($ t.find ('thead th'). width ()) }, // Function 2: repositionStickyHead () // Purpose: To position the cloned sticky header (always present) appropriately repositionStickyHead = function () { // Return value of calculated allowance var allowance = calcAllowance (); // Check if wrapper parent is overflowing along the y-axis if ($ t.height ()> $ stickyWrap.height ()) { // If it is overflowing // Position sticky header based on wrapper's scrollTop () if ($ stickyWrap.scrollTop ()> 0) { // When top of wrapping parent is out of view $ stickyHead.add ($ stickyInsct) .css ({ opacity: 1, top: $ stickyWrap.scrollTop () }); } else { // When top of wrapping parent is in view $ stickyHead.add ($ stickyInsct) .css ({ opacity: 0, top: 0 }); } } else { // If it is not overflowing (basic layout) // Position sticky header based on viewport scrollTop () if ($ w.scrollTop ()> $ t.offset (). top && $ w.scrollTop () 0) { // When left of wrapping parent is out of view // Show sticky column and intersect $ stickyCol.add ($ stickyInsct) .css ({ opacity: 1, left: $ stickyWrap.scrollLeft () }); } else { // When left of wrapping parent is in view // Hide sticky column but not the intersect // Reset left position $ stickyCol .css ({opacity: 0}) .add ($ stickyInsct) .css ({left: 0}); } }, // Function 4: calcAllowance () // Purpose: Return value of calculated allowance calcAllowance = function () { var a = 0; // Get sum of height of last three rows $ t.find ('tbody tr: lt (3)'). each (function () { a + = $ (this) .height (); }); // Set fail safe limit (last three row might be too tall) // Set arbitrary limit at 0.25 of viewport height, or you can use an arbitrary pixel value if (a> $ w.height () * 0.25) { a = $ w.height () * 0.25; } // Add height of sticky header itself a + = $ sticky.height (); return a; }; } Now we are going to explain what we did in the fourth function, we do not want the head of the table to append us to the bottom of the table, it is not necessary and may cover us the last line of the table, so it is necessary to keep an empty space at the bottom. According to what I tried, I discovered that we don't need the header of the table when we get to the last 3 lines of the table because our focus has shifted to the content now. $ t.find ('tbody tr: lt (4)'). each (function () { allowance + = $ (this) .height (); }); Step 5: Connect everything Now that we have finished defining all the necessary functions, all that remains is to link the event handlers with the $ window window. When the DOM is ready, we make the initial calculations for the presentation. When you hold all of the information we calculate the dimensions again, this step is important because your table may contain objects carrying after the DOM such as images and web fonts. When scrolling in the main container but this will happen if the content is greater than the width of the container, then we want to reposition the main column. When the browser window is minimized, we want to recalculate the view. When going down in the browser we want to change the header of the table. What we have just said can be summarized in the following code. Note that miniaturization and scrolling events are controlled with the addition of throttle + debounce. // # 1: DOM when ready setWidths (); // # 2: We monitor the container in the event of a pass in it $ t.parent ('. sticky-wrap'). scroll ($. throttle (250, function () { repositionStickyHead (); repositionStickyCol (); })); // Now we link what we made to the $ (window) element $w // # 3: When all the contents are loaded .load (setWidths) // # 4: When the window is minimized // We have used throttle here so that the event does not fire more than once (by default the event is fired for each minimized part, here we wait until the reduction is complete and then we launch) .resize ($. throttle (250, function () { setWidths (); repositionStickyHead (); repositionStickyCol (); }) // # 5: When going down in the window // We used throttle so the function doesn't launch much .scroll ($. throttle (250, repositionStickyHead); And here we were! The debate We know that nothing is perfect, so we will discuss other methods that have advantages over this method and its disadvantages and we have not used this method. Use position: fixed Using this method may seem tempting for two reasons: No calculations are required for the header of the table, because the fixed element is outside the physical page and will remain in place. We avoid slow calculations because the fixed elements chase the table and do not stick with it, because we do the calculation in fixed periods (via throttle) and therefore it will appear that the fixed element is unresponsive and therefore not normal. But the problem with this method is that we remove the element from the page luster. If the table width exceeds the permissible width and it becomes necessary to add a scroll, a header will not append the content on the horizontal axis because it is fixed on the page. This is a great reason we are not allowed to use most jQuery plugins that offer these features. And I wrote this course to fix exactly this. Use position: sticky The new feature is appropriate for the matter, in fact I built it for this matter in mind, the problem is that it is not supported by the chrome browser (previously it was supported but the support was completely removed) and thus lose all visitors from this browser. |