|
||||||||
Week Four JavaScripting NotesExample of a dropdown menu<!DOCTYPE html> <html> <head> <title> Dropdown Menuing </title> <style> .dropbtn { background-color: #3498DB; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropbtn:hover, .dropbtn:focus { background-color: #2980B9; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 160px; overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover {background-color: #ddd;} .show {display: block;} </style> </head> <body> <h2>Clickable Dropdown</h2> <p>Click on the button to open the dropdown menu.</p> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown</button> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> </div> <script> /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } // Close the dropdown if the user clicks outside of it window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i=0; i<dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } </script> </body> RolloversLet's look at rollovers and windowing techniques in order to gain some insight into their evolution. Introduction to RolloversPopular first-generation JavaScript Rollovers are well presented by Joe Maller on the Web. The Remember that comments in JavaScript can consist of the /* */ sequence anywhere within a script or consist two forward slashes // to indicate that all text that follows on the same line is ignored in processing the script. Comments are a good place to put any notes you would want to have should you inherit code from someone else. Declaring Images in the ScriptThe first step in creating a rollover is to declare to your JavaScript the images the rollover will use. Declaring the images causes the JavaScript interpreter to load the images into browser memory before they become visible. Once the images are cached, the image swaps can happen instantaneously. It is possible to use a rollover technique without declaring and preloading the images, but before an image changes the browser must first open a new connection, download the image, and load for viewing. This connection-download-load process takes long enough, even on fast connections, to make the rollover less effective than when you preload every image. To preload the images efficiently, you can declare each with two lines of code in conjunction with an array. Arrays
are like mini files in computer memory, storing several pieces of information in one
sequential place for efficient access via a common labeled name for the array's components. Our array, named Rollimage = new Array(); Rollimage[0] = new Image(100,150); Rollimage[0].src = "your_image.gif"; Rollimage[1] = new Image(100,150); Rollimage[1].src = "your_other_image.gif"; The first line above initializes the array, and following numbered sets of lines are the declaration of each additional image. The first line of each set declares the image's size and the second assigns its address. The numbers inside the parentheses refer to the width and height of the image, in that order. A non-local image would need to have its full path presented within a well-formed URL in the second line of the code. The src property of an HTML IMG element holds the source of an image to be presented in a Web browser. Early versions of JavaScript were case sensitive. More recent version
claim to be partly case sensitive but the boundaries of when case matters and when it doesn't are unclear. To be on the safe side, be sure to refer to your variables
and arrays with consideration of case. Naming Image ObjectsTo replace an image upon rolling over it's visual bounds, you need to give your JavaScript a way to identify which image to
replace in each instance. Do this by including a name attribute in the
By default, a Web browser counts every image on a page and refers to them numerically starting from the first image encountered sequentially in the code. The above refers to the third image tag the browser encounters when loading the HTML document. Consider that the browser conventionally counts starting with zero as the first item in memory is offset by zero objects from the location of the Array's label address. I've found named images easier to keep track of so that's what I use in this example. The following image is named "Rupert":
Rollovers are often used to provide visual feedback during link selection on a menu. As a result, the rollover images typically nest inside open and closing anchor (A) tags:
What's "onMouseOver"?
FunctionsFunctions are a predefined set of commands that JavaScript only executes when called. To be ready for use at the necessary time, they are usually defined in the first<SCRIPT>
section of the document. The standard format for defining a function is:
That function's name is
Putting It All TogetherThat's basically it. Following is the complete code for a simple document with one rollover image. Try it out:
Notes on JavaScript rollovers:
CSS based rolloversCompare the JavaScript technique of creating a rollover with the CSS technique whereby we change the style of a Web page component via cascading style sheet features than via swapping images: Here is the HTML used to create this rollover. You may notice that it is much shorter, and easier to remember than a JavaScript rollover. Essentially, you just need to define a class and everything else is handled with CSS
The Style Sheet The key to making this work in all modern browsers is to define your links as block elements in your style sheet ( display: block ), and making the width the same as your image’s ( eg. width: 32px ). Choose a background colour for your link that matches your page background and a second colour that you want for the rollover effect ( defined in a:hover ). For example:
This will create one rollover image per line. You can then use nested Why would you use CSS instead of Javascript?
Try an on-line rollover automation tool output exampleThere are many JavaScript automation tools available on-line. Once you've learned JavaScript, you'll be able to look behind the scenes to see how the JavaScript works and reverse engineer how each tool works (should you be interested). Here's the result of my use of a simple tool that created a simple, two item menu (the tool no longer exists online):
which generates the following code: <style type="text/css"> document.getElementById(which).style.background='#99FF00'; document.getElementById("description").innerHTML=' '; } </script> <!-- Place code above between your head tags--> <!-- Place code below between your body tags--> <table bgcolor="black" border="1" cellpadding="2" cellspacing="0" width="150"><tr> <td class="menu" bordercolor="black" id="choice1" style="cursor:hand;background-color:#99FF00" onMouseOver="movein('choice1','Connecting 3-D Cyberspace')" onMouseOut="moveout('choice1')" onClick="location.href='http://www.oworld.org/'"> <div align="center">OWorld</div> </td> </tr> <td class="menu" bordercolor="black" id="choice2" style="cursor:hand;background-color:#99FF00" onMouseOver="movein('choice2','Center for Environmental Visualization')" onMouseOut="moveout('choice2')" onClick="location.href='http://www.cev.washington.edu/'"> <div align="center">CEV</div> </td> </tr> <tr> JQuery RolloversThere are many rollover techniques to try with JQuery. We'll take a look at the code behind just one of seventeen time-tested examples you can admire. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Rollover Fade Method</title> <style type="text/css" media="screen"> <!-- BODY { margin: 10px; padding: 0; font: 1em "Trebuchet MS", verdana, arial, sans-serif; font-size: 100%; } H1 { margin-bottom: 2px; } --> </style> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> <!-- // wrap as a jQuery plugin and pass jQuery in to our anonymous function (function ($) { $.fn.cross = function (options) { return this.each(function (i) { // cache the copy of jQuery(this) - the start image var $$ = $(this); // get the target from the backgroundImage + regexp var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, ''); // nice long chain: wrap img element in span $$.wrap('<span style="position: relative;"></span>') // change selector to parent - i.e. newly created span .parent() // prepend a new image inside the span .prepend('<img>') // change the selector to the newly created image .find(':first-child') // set the image to the target .attr('src', target); // the CSS styling of the start image needs to be handled differently for different browsers if ($.browser.msie || $.browser.mozilla) { $$.css({ 'position' : 'absolute', 'left' : 0, 'background' : '', 'top' : this.offsetTop }); } else if ($.browser.opera && $.browser.version < 9.5) { $$.css({ 'position' : 'absolute', 'left' : 0, 'background' : '', 'top' : "0" }); } else { // Safari $$.css({ 'position' : 'absolute', 'left' : 0, 'background' : '' }); } // similar effect as single image technique, except using .animate // which will handle the fading up from the right opacity for us $$.hover(function () { $$.stop().animate({ opacity: 0 }, 250); }, function () { $$.stop().animate({ opacity: 1 }, 250); }); }); }; })(jQuery); // note that this uses the .bind('load') on the window object, rather than $(document).ready() // because .ready() fires before the images have loaded, but we need to fire *after* because // our code relies on the dimensions of the images already in place. $(window).bind('load', function () { $('img.fade').cross(); }); //--> </script> </head> <body> <h1>Fade Method - Single Image Technique</h1> <p>This technique uses jQuery to modify the markup and to animate to fade transition.</p> <p><a href="http://jqueryfordesigners.com/image-cross-fade-transition/">Read the article, and see the screencast this demonstration relates to</a></p> <div> <img class="fade" src="images/who.jpg" style="background: url(images/who_ro.jpg);" alt="Who we are" /> <img class="fade" src="images/who.jpg" style="background: url(images/who_ro.jpg);" alt="Who we are" /> <img class="fade" src="images/who.jpg" style="background: url(images/who_ro.jpg);" alt="Who we are" /> </div> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-1656750-8"; urchinTracker(); </script> </body> </html> JavaScript Window Popups with the window.open FunctionTake a look at the following simple primer from tizag.com. The window.open() function creates a new browser window, customized to your specifications, without the use of an HTML anchor tag. In this example, we will be making a function that utilizes the window.open() function. HTML & JavaScript Code:<head> <script type="text/javascript"> <!-- function myPopup() { Display:CLICK ME TOO! This works with pretty much any tag that can be clicked on, so please go ahead and experiment with this fun little tool. Afterwards, read on to learn more about the different ways you can customize the JavaScript window that pops up. JavaScript Window.Open ArgumentsThere are three arguments that the window.open function takes:
Naming a window is very useful if you want to manipulate it later with JavaScript. However, this is beyond the scope of this lesson, and we will instead be focusing on the different properties you can set with your brand spanking new JavaScript window. Below are some of the more important properties:
Dependent, fullscreen, resizable, and status are all examples of ON/OFF properties. You can either set them equal to zero to turn them off, or set them to one to turn them ON. There is no inbetween setting for these types of properties. Upgraded JavaScript Popup Window!Now that we have the tools, let's make a sophisticated JavaScript popup window that we can be proud of! HTML & JavaScript Code:<head> <script type="text/javascript"> <!-- function myPopup2() { Display:CLICK ME TOO! Now, that is a prime example of a worthless popup! When you make your own, try to have them relate to your content, like a small popup with no navigation that just gives the definition or explanation of a word, sentence, or picture! Expanded TooltipsIf you want to show additional information in a new layer but not have to generate a new window, you can expand on the tooltip concept like this framework here. Basically, you can add new content to a page through an event handler. Let's look at a function that adds content via the document.getElementById() function and the innerHTML property available in JavaScript. Take a look at the code: <script> function showme() { var txt=document.getElementById("popup"); txt.innerHTML="<img src=yinyang.png>"; } function removeme() { var txt=document.getElementById("popup"); txt.innerHTML=""; } </script> which works with an existing DIV element: <div id="popup" style="position:relative;top:0px;left:700px;height:182px;width:169px;z-index:5"></div> JQuery windowingLet's try an example of internal page popups with JQuery. The code is here:<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Draggable - Default functionality</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <style type="text/css"> body { font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px } #layer1 { position: absolute; left:200px; top:100px; width:250px; background-color:#f0f5FF; border: 1px solid #000; z-index: 50; visibility: hidden; cursor: pointer; } #layer1_handle { background-color:#5588bb; padding:2px; text-align:center; font-weight:bold; color: #FFFFFF; vertical-align:middle; } #layer1_content { padding:5px; } #close { float:right; text-decoration:none; color:#FFFFFF; } h1, h2 { font-size:20px; } </style> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $( function() { $("#layer1").draggable(); $("#preferences").click(function() { $("#layer1").css("visibility", "visible"); }); $("#close").click(function() { $("#layer1").css("visibility", "hidden"); }); } ); </script> </head> <body> <h2>JQuery Tutorial Demo: Floating Dialog Windows</h2> <p>Here is the demo on using floating dialog windows by using the form and interface plug-in of jQuery</p> <div id="content"><input type="button" id="preferences" value="Edit Preferences" /></div> <div id="layer1"> <div id="layer1_handle"> <a href="#" id="close">[ x ]</a> Preferences </div> <div id="layer1_content"> <form id="layer1_form" method="post" action="save_settings.php"> Display Settings<br /> <input type="radio" name="display" checked="checked" value="Default" />Default<br /> <input type="radio" name="display" value="Option A" />Option A<br /> <input type="radio" name="display" value="Option B" />Option B<br /><br /> Autosave settings<br /> <input type="radio" name="autosave" checked="checked" value="Enabled" />Enabled<br /> <input type="radio" name="autosave" value="Disabled" />Disabled<br /><br /> <input type="submit" name="submit" value="Save" /> </form> </div> </div> </body> </html> Workbook Available for Skills PracticeThe relevant workbook is here for you to practice skills with the ideas presented in this document. |
||||||||