JavaScript 学习笔记 - 4 处理图像

在这里插入图片描述

4.1 创建翻转器

<!DOCTYPE html>
<html>
<head>
<title>A Simple Rollover</title>
<link rel="stylesheet" href="script01.css">
</head>
<body>
<a href="next.html" onmouseover="document.images['arrow'].src='images/arrow_on.gif'" onmouseout="document.images['arrow'].src='images/arrow_off.gif'"><img src="images/arrow_off.gif" id="arrow" alt="arrow"></a>
</body>
</html>
  1. <a href=“next.html”
    链接首先指定当用户单击图像时,浏览器将转到哪里,在这个示例中是 next.html 页面。
  2. οnmοuseοver=“document.images[‘arrow’].src=‘images/arrow_on.gif’”
    当用户将鼠标移动到图像上时( id 为 arrow 的 src), images 目录中的替换图像 arrow_on.gif 被写
    到文档窗口中。
  3. οnmοuseοut=“document.images[‘arrow’].src=‘images/arrow_off.gif’”
    当鼠标移走时, arrow_off.gif 图像重新显示。
  4. <img src=“images/arrow_off.gif” id=“arrow” alt="arrow"图像链接为页面定义原始图像的来源

4.2 创建更有效的翻转器

<!DOCTYPE html>
<html>
<head>
<title>A More Effective Rollover</title>
<script src="script02.js"></script>
<link rel="stylesheet" href="script01.css">
</head>
<body>
<a href="next1.html"><img src="images/button1_off.gif" alt="button1" id="button1"></a>&nbsp;&nbsp;
<a href="next2.html"><img src="images/button2_off.gif" alt="button2" id="button2"></a>
</body>
</html>
window.onload = rolloverInit;
function rolloverInit() {
for (var i=0; i<document.images.length; i++) {
if (document.images[i].parentNode.tagName == "A") {
setupRollover(document.images[i]);
}
}
}
function setupRollover(theImage) {
theImage.outImage = new Image();
theImage.outImage.src = theImage.src;
theImage.onmouseout = function() {
this.src = this.outImage.src;
}
theImage.overImage = new Image();
theImage.overImage.src = "images/" + theImage.id + "_on.gif";
theImage.onmouseover = function() {
this.src = this.overImage.src;
}
}

在这里插入图片描述

4.3 构建三状态翻转器

window.onload = rolloverInit;
function rolloverInit() {
for (var i=0; i<document.images.length;i++) {
if (document.images[i].parentNode.tagName == "A") {
setupRollover(document.images[i]);
}
}
}
function setupRollover(theImage) {
theImage.outImage = new Image();
theImage.outImage.src = theImage.src;
theImage.onmouseout = function() {
this.src = this.outImage.src;
}
theImage.clickImage = new Image();
theImage.clickImage.src = "images/" + theImage.id + "_click.gif";
theImage.onclick = function() {
this.src = this.clickImage.src;
}
theImage.overImage = new Image();
theImage.overImage.src = "images/" + theImage.id + "_on.gif";
theImage.onmouseover = function() {
this.src = this.overImage.src;
}
}

theImage.onclick = function() {
this.src = this.clickImage.src;
}

4.4 由链接触发翻转器

在这里插入图片描述

window.onload = rolloverInit;
function rolloverInit() {
for (var i=0; i<document.links.length; i++) {
var linkObj = document.links[i];
if (linkObj.id) {
var imgObj = document.getElementById(linkObj.id + "Img");
if (imgObj) {
setupRollover(linkObj,imgObj);
}
}
}
}
function setupRollover(theLink,theImage) {
theLink.imgToChange = theImage;
theLink.onmouseout = function() {
this.imgToChange.src = this.outImage.src;
}
theLink.onmouseover = function() {
this.imgToChange.src = this.overImage.src;
}
theLink.outImage = new Image();
theLink.outImage.src = theImage.src;
theLink.overImage = new Image();
theLink.overImage.src = "images/" + theLink.id + "_on.gif";
}

theLink.onmouseover = function() {
this.imgToChange.src = this.overImage.src;

4.5 让多个链接触发一个翻转器

window.onload = rolloverInit;
function rolloverInit() {
for (var i=0; i<document.links.length; i++) {
var linkObj = document.links[i];
if (linkObj.className) {
var imgObj = document.getElementById(linkObj.className);
if (imgObj) {
setupRollover(linkObj,imgObj);
}
}
}
}
function setupRollover(theLink,textImage) {
theLink.imgToChange = textImage;
theLink.onmouseout = function() {
this.imgToChange.src = this.outImage.src;
}
theLink.onmouseover = function() {
this.imgToChange.src = this.overImage.src;
}
theLink.outImage = new Image();
theLink.outImage.src = textImage.src;
theLink.overImage = new Image();
theLink.overImage.src = "images/" + theLink.id + "Text.gif";
}
  1. if (linkObj.className) {
    var imgObj = document.getElementById(linkObj.className);
    无法使用翻转图像的 id 计算出改变过的图像的 id,这是因为 id 必须是唯一的,而所有翻转图像必须为改变过的图像的目的地提供同样的值。因此,我们要使用 class 属性(因为多个页面元素可以共享同一个 class)。在这一行上,寻找链接对象的 className。72 第 4 章 处理图像
  2. function setupRollover(theLink, textImage) {
    theLink.imgToChange = textImage;我们将当前链接对象( theLink)和图像对象(我们称之为 textImage)传递给 setupRollover()函数。注意,在传递这些对象(也可以称为变量)时,分别将它们称为 linkObj 和 imgObj。脚本其余部分的工作方式与本章前面的示例相同。

4.6 处理多个翻转器

function setupRollover(thisLink,textImage) {
theLink.imgToChange = new Array;
theLink.outImage = new Array;
theLink.overImage = new Array;
theLink.imgToChange[0] = textImage;
theLink.onmouseout = rollOut;
theLink.onmouseover = rollOver;
theLink.outImage[0] = new Image();
theLink.outImage[0].src = textImage.src;
theLink.overImage[0] = new Image();
theLink.overImage[0].src = "images/" + theLink.id + "Text.gif";
var rolloverObj = document.getElementById(theLink.id + "Img");
if (rolloverObj) {
theLink.imgToChange[1] = rolloverObj;
theLink.outImage[1] = new Image();
theLink.outImage[1].src = rolloverObj.src;
theLink.overImage[1] = new Image();
theLink.overImage[1].src = "images/" + theLink.id + "_on.gif";
}
}
function rollOver() {
for (var i=0; i<this.imgToChange.length; i++) {
this.imgToChange[i].src = this.overImage[i].src;
}
}
function rollOut() {
for (var i=0; i<this.imgToChange.length; i++) {
this.imgToChange[i].src = this.outImage[i].src;
}
}

theLink.imgToChange = new Array;
theLink.outImage = new Array;
theLink.overImage = new Array;

4.7 创建循环的广告条

window.onload = rotate;
var theAd = 0;
var adImages = new Array("images/reading1.gif","images/reading2.gif","images/reading3.gif");
function rotate() {
theAd++;
if (theAd == adImages.length) {
theAd = 0;
}
document.getElementById("adBanner").src = adImages[theAd];
setTimeout(rotate, 3 * 1000);
}

在这里插入图片描述
2. setTimeout(rotate, 3 * 1000);
这一行告诉脚本每隔多长时间改变广告条中的 GIF。可以使用内置的 JavaScript 命令 setTimeout
指定一个操作应该间隔多长时间执行一次(时间的单位是毫秒)。在这个示例中,每 3000 毫秒(即 3秒)调用一次 rotate()函数,所以广告条中的 GIF 每 3 秒改变一次

4.8 在循环广告条中添加链接

在这里插入图片描述

window.onload = initBannerLink;
var theAd = 0;
var adURL = new Array("negrino.com","sun.com","microsoft.com");
var adImages = new Array("images/banner1.gif","images/banner2.gif","images/banner3.gif");
function initBannerLink() {
if (document.getElementById("adBanner").parentNode.tagName == "A") {
document.getElementById("adBanner").parentNode.onclick = newLocation;
}
rotate();
}
function newLocation() {
document.location.href = "http://www." + adURL[theAd];
return false;
}
function rotate() {
theAd++;
if (theAd == adImages.length) {
theAd = 0;
}
document.getElementById("adBanner").src = adImages[theAd];
setTimeout(rotate, 3 * 1000);
}

4.9 建立循环式幻灯片

Web 站点上的幻灯片每次向用户显示一个图像,并且让用户能够控制显示图像的进度(既可以向
前,也可以向后)。

window.onload = initLinks;
var thePic = 0;
var myPix = new Array("images/robot1.jpg","images/robot2.jpg","images/robot3.jpg");
function initLinks() {
document.getElementById("prevLink").onclick = processPrevious;
document.getElementById("nextLink").onclick = processNext;
}
function processPrevious() {
if (thePic == 0) {
thePic = myPix.length;
}
thePic--;
document.getElementById("myPicture").src = myPix[thePic];
return false;
}
function processNext() {
thePic++;
if (thePic == myPix.length) {
thePic = 0;
}
document.getElementById("myPicture").src = myPix[thePic];
return false;
}

4.10 显示随机图像

window.onload = choosePic;
var myPix = new Array("images/lion.jpg","images/tiger.jpg","images/bear.jpg");
function choosePic() {
var randomNum = Math.floor (Math.random() * myPix.length);
document.getElementById("myPicture").src = myPix[randomNum];
}

var randomNum = Math.floor (Math.random() * myPix.length);

4.11 随机开始循环显示图像

window.onload = choosePic;
var theAd = 0;
var adImages = new Array("images/reading1.gif","images/reading2.gif","images/reading3.gif");
function choosePic() {
theAd = Math.floor(Math.random() * adImages.length);
document.getElementById("adBanner").src = adImages[theAd];
rotate();
}
function rotate() {
theAd++;
if (theAd == adImages.length) {
theAd = 0;
}
document.getElementById("adBanner").src = adImages[theAd];
setTimeout(rotate, 3 * 1000);
}

theAd = Math.floor(Math.random() * adImages.length);
rotate();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值