# 学习JavaScript笔记「4」 URL: https://chengjinfang.com/posts/2017/web-javascript4/ Author: 程锦方 Date: 2017-09-07 Tags: #note #javascript #web Summary: 整理图像属性,以及使用 JavaScript 操作页面图像的笔记。 ------------------------------------------------------------------------ ## 图像 #### 涉及html: ``` 标 签 属 性 意 义 img 其中包含描述浏览器要显示的图像的属性 src 包含图像的URL,这个URL是相对于网页的URL来说的 width 包含浏览器显示图像所用的宽度(以像素为单位) height 包含浏览器显示图像所用的高度(以像素为单位) alt 用来在非图形化浏览器中替代图像 id 唯一标识符,JavaScript将用它来操纵图像 ``` ### 翻转器 使用JavaScript预先将所有图像加载到浏览器的缓存中,并且将图像放进脚本使用的变量中。 #### html代码: ```html   button1      button2 ``` #### script02.js: ```js window.onload = rolloverInit; function rolloverInit() {   for (var i=0; ibutton1 ``` 它们是用于按钮的典型链接标签,其中嵌入了图像标签。href属性描述当用户单击链接时链接的目标。在img标签中,src属性提供在用户将鼠标移动到图像上之前显示的图像的路径。链接标签也定义了图像的alt文本。注意,这两个按钮都有id属性。id对于每个对象必须是唯一的。脚本使用图像的id使翻转器发挥作用。 ```js window.onload = rolloverInit; ``` 当页面完成加载时,会触发window.onload事件处理程序。这个处理程序调用rolloverInit()函数。 这里使用这个处理程序确保在页面完成加载之前脚本不会执行。这是因为在页面完成加载之前,页面上的某些元素如果还没有加载,引用页面上的元素可能会导致错误。 ```js function rolloverInit() { for (var i=0; i标签,如果是,就说明它是一个链接。这两行中的第一行是函数的开头。第二行开始一个for...next循环,这个循环遍历所有图像。它首先将计数器变量i设置为0。然后,在每次循环迭代中,如果i的值小于文档中的图像数量,就将i递增1。 ```js if (document.images[i].parentNode.tagName == "A") { ``` 这里检查包围图像的标签是否是锚标签。检查方法是查看对象的值是否为A(A是锚标签的名称)。我们来仔细看看这里的表达式。第一部分document.images[i]是当前的图像。它的parentNode属性是包围它的容器标签,而tagName提供容器标签的名称。所以,圆括号中代码的意思是:“对于这个特定的图像,包围它的标签是A吗?” ```js setupRollover(document.images[i]); ``` 调用setupRollover函数并传递当前图像。 ```js function setupRollover(thisImage) { ``` 在逐行查看这个函数之前,先看一下整个函数的作用:这个函数将两个新的属性添加到传递给它的图像对象中。新的属性是outImage(鼠标不在图像上时的图像版本)和overImage(鼠标在图像上时的图像版本),它们本身都是图像对象。因为它们是图像对象,所以被创建后,就可以添加它们的src属性了。outImage的src值是当前图像的src,overImage的src值是根据原图像的id属性计算出来的。 这一行开始setupRollover函数,其参数是前面的rolloverInit()函数传递给它的图像。 ```js thisImage.outImage = new Image(); ``` 这一行获得传递进来的图像对象,并在其中添加新的outImage属性。因为可以在对象上添加任何类型的属性,而且属性本身也是对象,所以这里的操作是将一个图像对象添加到图像中。新的图像对象的圆括号是可选的,但这是良好的做法。如果需要的话,可以通过传递参数设置新图像对象的属性。 ```js thisImage.outImage.src = thisImage.src; ``` 现在,将新的outImage的来源设置为与thisImage的来源相同。页面上的默认图像也就是鼠标不在图像上时看到的图像。 ```js thisImage.onmouseout = function() {  this.src = this.outImage.src; } ``` 第一行开始定义一个匿名函数。可以给它指定一个名称,但是因为它只有一行代码,所以不需要命名。 在这里,我们告诉浏览器当用户把鼠标移出图像时应该触发什么操作。当发生这种情况时,希望把图像源恢复为最初的值。 ```js thisImage.overImage = new Image(); thisImage.overImage.src = "images/" + thisImage.id + "_on.gif"; ``` 在第一行中,创建一个新的图像对象,它将包含图像的overImage版本。第二行设置overImage的来源。它将"images/"和图像的id拼接起来,再加上"_on.gif",从而在运行时构造出源文件名。 ```js thisImage.onmouseover = function() {  this.src = this.overImage.src; } ``` 这是另一个匿名函数。它告诉浏览器,当用户将鼠标移动到图像上时,应该把当前图像的源重新设置为overImage版本。 ##### tagName总是返回大写的值。 #### 构建三状态翻转器 三状态翻转器就是能够显示图像的3个版本的翻转器。除了原始图像和当用户将鼠标移动到图像上时显示的图像之外,图像还有第三个版本,这个版本在按钮本身被单击时显示。 三状态翻转器的HTML实质上与两状态翻转器相同 构建三状态翻转器: ```js window.onload = rolloverInit; function rolloverInit() {   for (var i=0; i   

Next page

  arrow ``` #### 由文本链接触发的翻转器的JavaScript: ```js window.onload = rolloverInit; function rolloverInit() {   for (var i=0; i   
    DaVinci     Text Field   
  
    Leonardo’s Inventions     Flying Machine     Tank     Helicopter   
``` #### CSS: ```css body {   background-color: #EC9; } img {   border-width: 0; } #captionDiv {   float: right;   width: 210px;   margin: auto 50px; } #captionField {   margin: 20px auto;   width: 208px;   height: 27px; } #inventionDiv {   width: 375px;   margin-left: 20px; } #heading {   margin-bottom: 20px;   width: 375px;   height: 26px; } ``` #### 多个链接触发一个翻转器js: ```js window.onload = rolloverInit; function rolloverInit() {   for (var i=0; i   
    Ad Banner   
``` #### 使用JavaScript在广告条中循环显示图像: ```js window.onload = rotate; var thisAd = 0; function rotate() {   var adImages = new Array("images/ reading1.gif","images/reading2. gif","images/reading3.gif");   thisAd++;   if (thisAd == adImages.length) {     thisAd = 0;   }   document.getElementById("adBanner").src = adImages[thisAd];   setTimeout(rotate, 3 * 1000); } ``` ```js var thisAd = 0; ``` 首先创建变量thisAd,它在这段代码中被设置了初始值。 ```js function rotate() { var adImages = new Array("images/reading1.gif","images/reading2.gif","images/reading3.gif"); ``` 先建立一个称为rotate()的新函数。第二行代码创建一个名为adImages的新数组,它包含构成循环广告条的3个GIF文件的文件名。 ```js thisAd++; ``` 这一行将thisAd的值加1。 ```js if (thisAd == adImages.length) { thisAd = 0; ``` 这一行代码检查thisAd的值是否等于adImages数组中成员的数量,如果等于,就将thisAd的值设置为0。 document.getElementById("adBanner").src = adImages[thisAd];页面上循环显示 的图像的id是adBanner。,img标签中定义了这个id。这行代码指出,adBanner图像的来源在adImages数组中,而且thisAd变量的值决定浏览器当前应该使用3个GIF中的哪一个。 ```js setTimeout(rotate, 3 * 1000); ``` 这一行告诉脚本每隔多长时间改变广告条中的GIF。在这个示例中,每3000毫秒(即3秒)调用一次rotate()函数,广告条中的GIF每3秒改变一次。 ### 在循环广告条中添加链接 #### 广告条所需的HTML: ```html   
    ad banner   
``` #### js: ```js window.onload = initBannerLink; var thisAd = 0; function initBannerLink() {   if (document.getElementById("adBanner").parentNode.tagName == "A") {     document.getElementById("adBanner").parentNode.onclick = newLocation;   }   rotate(); } function newLocation() {   var adURL = new Array("negrino.com","sun.com","microsoft.com");   document.location.href = "http://www." + adURL[thisAd];   return false; } function rotate() {   var adImages = new Array("images/ banner1.gif","images/banner2.gif","images/banner3.gif");   thisAd++;   if (thisAd == adImages.length) {     thisAd = 0;   }   document.getElementById("adBanner").src = adImages[thisAd];   setTimeout(rotate, 3 * 1000); } ``` ```js window.onload = initBannerLink; ``` 当窗口完成加载时,触发initBannerLink()函数。 ```js if (document.getElementById("adBanner").parentNode.tagName == "A") {  document.getElementById ("adBanner").parentNode.onclick = newLocation; } rotate(); ``` 这段代码在initBannerLink()函数中,它首先检查adBanner对象是否包围在链接标签中。如果是这样,那么当单击链接时,将调用newLocation()函数。最后,调用rotate()函数。 ```js function newLocation() { var adURL = new Array("negrino.com","sun.com","microsoft.com"); ``` 在新函数newLocation()中,adURL变量被设置为一个包含3个成员的新数组。这里只存储域名,因为下面将组合出完整的URL。 ```js document.location.href = "http://www." + adURL[thisAd]; return false; ``` 这段代码在新函数newLocation中,将document.location.href对象(换句话说,就是当前文档窗口)设置为文本字符串http://www.(注意点号)加上adURL的值。因为adURL是一个数组,所以需要指定数组的一个成员。成员的编号存储在thisAd中,产生的字符串可以是三个链接之一(取决于用户何时进行单击)。最后,它返回false,告诉浏览器不应再加载这个href。如果不这样做,浏览器就会加载这个URL两次。我们已经在JavaScript中处理了所有工作,所以不需要再加载href。 ### 建立循环式幻灯片 #### HTML: ```html   
    

Welcome, Robot Overlords!

    Slideshow     

<< Previous   Next >>

  
``` #### 构建一个幻灯片,用户可以通过单击链接控制图像的前后切换 ```js window.onload = initLinks; var myPix = new Array("images/robot1.jpg","images/robot2.jpg","images/robot3.jpg"); var thisPic = 0; function initLinks() {   document.getElementById("prevLink").onclick = processPrevious;   document.getElementById("nextLink").onclick = processNext; } function processPrevious() {   if (thisPic == 0) {     thisPic = myPix.length;   }   thisPic--;   document.getElementById("myPicture").src = myPix[thisPic];   return false; } function processNext() {   thisPic++;   if (thisPic == myPix.length) {     thisPic = 0;   }   document.getElementById("myPicture").src = myPix[thisPic];   return false; } ``` 这个脚本构建的幻灯片是循环式的(wrap around),也就是说,如果超过了图像列表的末尾,就会返回到开头,反之亦然。图4-14显示这个幻灯片的效果。 ```js function initLinks() {  document.getElementById ("prevLink").onclick = processPrevious;  document.getElementById ("nextLink").onclick = processNext; } ``` 这个函数为Previous或Next链接设置onclick事件处理程序。 ```js function processPrevious() { if (thisPic == 0) {  thisPic = myPix.length; ``` 这个函数使幻灯片向前切换图像。它首先检查thisPic是否等于0。如果是,这个函数就获取myPix数组中的图像数量。 ```js thisPic--; document.getElementById("myPicture").src = myPix[thisPic]; ``` 第一行将thisPic的值减1。下一行将myPicture的src属性设置为myPix数组中与thisPic的当前值对应的元素。 ```js thisPic++; if (thisPic == myPix.length) {   thisPic = 0; } document.getElementById ("myPicture").src = myPix[thisPic]; ``` 这段代码在processNext()函数中,它使幻灯片向后切换图像,其工作方式与processPrevious()函数非常相似。它首先将thisPic的值加1,然后检查thisPic的值是否等于myPix数组中成员的数量。如果是,就将thisPic设置为0。下一行设置myPicture的src属性。 ### 随机图像 #### html: ```html   some image ``` #### js 使用JavaScript的Math.random方法生成一个随机数 ```js window.onload = choosePic; function choosePic() {   var myPix = new Array("images/lion.jpg","images/tiger.jpg","images/bear.jpg");   var randomNum = Math.floor ((Math.random() * myPix.length));   document.getElementById("myPicture").src = myPix[randomNum]; } ``` ```js var myPix = new Array("images/lion.jpg", "images/tiger.jpg","images/bear.jpg"); ``` 与前面一样,建立一个包含三个图像的数组,并且将它赋值给变量myPix。 ```js randomNum = Math.floor((Math.random() * myPix.length)); ``` 变量randomNum被设置为一个数学表达式的值。这个表达式最好是从内向外读。Math.random生成一个0~1的随机数,然后将这个数字乘以myPix.length,即数组中的成员数量(在这个示例中是3)。Math.floor将结果向下取整,这意味着最终产生0、1和2中的一个数字。 ```js document.getElementById("myPicture").src = myPix[randomNum]; ``` 这行代码根据myPix数组设置myPicture图像的来源,当前采用的值由randomNum的值决定。 #### 随机循环显示图像 ```js window.onload = choosePic; var adImages = new Array("images/reading1.gif","images/reading2.gif","images/reading3.gif"); var thisAd = 0; function choosePic() {   thisAd = Math.floor((Math.random() * adImages.length));   document.getElementById("adBanner").src = adImages[thisAd];   rotate(); } function rotate() {   thisAd++;   if (thisAd == adImages.length) {     thisAd = 0;   }   document.getElementById("adBanner").src = adImages[thisAd];   setTimeout(rotate, 3 * 1000); } ``` ------------------------------------------------------------------------ Site: https://chengjinfang.com/ · All Index: https://chengjinfang.com/all/ · RSS: https://chengjinfang.com/rss.xml