原文:http://dojotoolkit.org/documentation/tutorials/1.7/dialogs_tooltips/
原文作者:David Walsh
译者:Elaine Liu
Dojo 1.7 难度级别:初级
用户交互是构建富客户端网络应用的第一要素。浏览器提供了alert和对话框作为基本的用户交互方式,但这些基本的交互非常简陋并且不够灵活。因此,Dijit,这个Dojo工具包提供的用户界面框架,提供了由dijit/Tooltip, dijit/Dialog, 以及dijit/TooltipDialog等widget在内的一系列跨浏览器的,可扩展的,可定制主题的控件,来弥补了浏览器的基本交互功能存在的不足。在本教程中,你将会学习以上控件,包括使用范例以及相关细节。
<head> <!-- use the "claro" theme --> <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css"> <!-- load dojo and provide config via data attribute --> <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js" data-dojo-config="async: true, parseOnLoad:true"></script> <script> // Load the Tooltip widget class require(["dijit/Tooltip", "dojo/parser", "dojo/domReady!"], function(Tooltip){ }); </script> </head> <!-- add the "claro" CSS class to the body --> <body class="claro"> </body>
// Create a new Tooltip var tip = new Tooltip({ // Label - the HTML or text to be placed within the Tooltip label: '<div class="myTipType">This is the content of my Tooltip!</div>', // Delay before showing the Tooltip (in milliseconds) showDelay: 250, // The nodes to attach the Tooltip to // Can be an array of strings or domNodes connectId: ["myElement1","myElement2"] });
dijit/Tooltip对象还有一个可以配置的defaultPosition队列属性,可用于控制Tooltip实例显示位置的顺序。这个队列可以根据开发者的需要而改变。
Tooltip.defaultPosition = ["above", "below", "after", "before"];
注意:改变Tooltip.defaultPosition将适用于所有的Tooltip实例
下面是dijit/Tooltip最普通的使用例子
<button id="TooltipButton" onmouseover="dijit.Tooltip.defaultPosition=['above', 'below']">Tooltip Above</button> <div class="dijitHidden"><span data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'TooltipButton'">I am <strong>above</strong> the button</span></div> <button id="TooltipButton2" onmouseover="dijit.Tooltip.defaultPosition=['below','above']">Tooltip Below</button> <div class="dijitHidden"><span data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'TooltipButton2'">I am <strong>below</strong> the button</span></div> <button id="TooltipButton3" onmouseover="dijit.Tooltip.defaultPosition=['after','before']">Tooltip After</button> <div class="dijitHidden"><span data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'TooltipButton3'">I am <strong>after</strong> the button</span></div> <button id="TooltipButton4" onmouseover="dijit.Tooltip.defaultPosition=['before','after']">Tooltip Before</button> <div class="dijitHidden"><span data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'TooltipButton4'">I am <strong>before</strong> the button</span></div>
// Add Tooltip of his picture new Tooltip({ connectId: ["nameTip"], label: "<img src='rod-stewart.jpg' alt='Rod Stewart' width='300' height='404' />" }); // Add Tooltip of North London new Tooltip({ connectId: ["londonTip"], label: "<img src='emirates-stadium.jpg' alt='The Emirates in London' width='400' height='267' />" }); //Add Tooltip of record new Tooltip({ connectId: ["recordsTip"], label: "<img src='every-picture.jpg' alt='Every Picture Tells a Story' width='200' height='197' />" }); // Add custom Tooltip var myTip = new Tooltip({ connectId: ["hoverLink"], label: "Don't I look funky?", "class": "customTip" });
<ul> <li><a href="http://www.imdb.com/title/tt0112573/" id="movieBraveheart">Braveheart</a></li> <li><a href="http://www.imdb.com/title/tt0237534/" id="movieBrotherhood">Brotherhood of the Wolf</a></li> <li><a href="http://www.imdb.com/title/tt0245844/" id="movieCristo">The Count of Monte Cristo</a></li> </ul> <div class="dijitHidden"> <div data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'movieBraveheart'"> <img style="width:100px; height:133px; display:block; float:left; margin-right:10px;" src="../images/braveheart.jpg" /> <p style="width:400px;"><strong>Braveheart</strong><br />Braveheart is the partly historical, partly mythological, story of William Wallace, a Scottish common man who fights for his country's freedom from English rule around the end of the 13th century...</p> <br style="clear:both;"> </div> </div> <div class="dijitHidden"> <div data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'movieBrotherhood'"> <img style="width:100px; height:133px; display:block; float:left; margin-right:10px;" src="../images/brotherhood.jpg" /> <p style="width:400px;"><strong>Brotherhood of the Wolf</strong><br />In 1765 something was stalking the mountains of central France. A 'beast' that pounced on humans and animals with terrible ferocity...</p> <br style="clear:both;"> </div> </div> <div class="dijitHidden"> <div data-dojo-type="dijit.Tooltip" data-dojo-props="connectId:'movieCristo'"> <img style="width:100px; height:133px; display:block; float:left; margin-right:10px;" src="../images/count.jpg" /> <p style="width:400px;"><strong>The Count of Monte Cristo</strong><br />'The Count of Monte Cristo' is a remake of the Alexander Dumas tale by the same name. Dantes, a sailor who is falsely accused of treason by his best friend Fernand, who wants Dantes' girlfriend Mercedes for himself...</p> <br style="clear:both;"> </div> </div>
// Create a new instance of dijit/Dialog var myDialog = new Dialog({ // The dialog's title title: "The Dojo Toolkit", // The dialog's content content: "This is the dialog content.", // Hard-code the dialog width style: "width:200px;" });
<script> // Require the Dialog class require(["dijit/registry", "dijit/Dialog"], function(registry) { // Show the dialog showDialog = function() { registry.byId("terms").show(); } // Hide the dialog hideDialog = function() { registry.byId("terms").hide(); } }); </script> <button onclick="showDialog();">View Terms and Conditions</button> <div class="dijitHidden"> <div data-dojo-type="dijit.Dialog" style="width:600px;" data-dojo-props="title:'Terms and Conditions'" id="terms"> <p><strong>Please agree to the following terms and conditions:</strong></p> <div style="height:160px;overflow-y:scroll;border:1px solid #769dc4;padding:0 10px;width:600px"><p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed suscipit massa. Aenean vel turpis tincidunt velit gravida venenatis. In iaculis urna non quam tincidunt elementum. Nunc pellentesque aliquam dui, ac facilisis massa sollicitudin et. Donec tincidunt vulputate ultrices. Duis eu risus ut ipsum auctor scelerisque non quis ante. Nam tempor lobortis justo, et rhoncus mauris cursus et. Mauris auctor congue lectus auctor ultrices. Aenean quis feugiat purus. Cras ornare vehicula tempus. Nunc placerat, lorem adipiscing condimentum sagittis, augue velit ornare odio, eget semper risus est et erat.... </p></div> <button onclick="hideDialog();">I Agree</button> <button onclick="alert('You must agree!');">I Don't Agree</button> </div> </div>
<script> // Require the Dialog class require(["dijit/Dialog"], function(Dialog) { // Create counter var counter = 1; // Create a new Dialog createDialog = function(first) { // Create a new dialog var dialog = new Dialog({ // Dialog title title: "New Dialog " + counter, // Create Dialog content content: (!first ? "I am a dialog on top of other dialogs" : "I am the bottom dialog") + "<br /><br /><button onclick='createDialog();'>Create another dialog.</button>" }); dialog.show(); counter++; } }); </script> <button onclick="createDialog(true);">Create New Dialog</button>
<style> /* colors the underlay black instead of white * We're using '.claro .dijitDialogUnderlay' as our selector, * to match the specificity in claro.css */ .claro .dijitDialogUnderlay { background:#000; } </style> <script> // Require the Dialog class require(["dijit/registry", "dojo/parser", "dijit/Dialog"], function(registry) { // Show the dialog showDialog = function() { registry.byId("ajaxDialog").show(); } }); </script> <button onclick="showDialog();">Load Ajax Dialog</button> <div class="dijitHidden"> <!-- dialog that gets its content via ajax, uses loading message --> <div data-dojo-type="dijit.Dialog" style="width:600px;" data-dojo-props="title:'Ajax Dialog',href:'dialog-ajax.php',loadingMessage:'Loading dialog content...'" id="ajaxDialog"></div> </div>
<script> // Require the Button, TooltipDialog, DropDownButton, and TextBox classes require(["dijit/form/DropDownButton", "dijit/TooltipDialog", "dijit/form/TextBox", "dojo/parser"]); </script> <div data-dojo-type="dijit.form.DropDownButton"> <span>Login</span><!-- Text for the button --> <!-- The dialog portion --> <div data-dojo-type="dijit.TooltipDialog" id="ttDialog"> <strong><label for="email" style="display:inline-block;width:100px;">Email:</label></strong> <div data-dojo-type="dijit.form.TextBox" id="email"></div> <br /> <strong><label for="pass" style="display:inline-block;width:100px;">Password:</label></strong> <div data-dojo-type="dijit.form.TextBox" id="pass"></div> <br /> <button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:doAlert" type="submit">Submit</button> </div> </div>