JavaScript学习日记5-处理图像2

创建循环的广告条

    <head>
        <script src="js.js"></script>
        <style>
            img{
                width: 200px;
                height: 200px;
            }
        </style>
    </head>
    <body>
        <div class="centered">
            <img src="images/dog1.JPG" id="dog" alt="dog">
        </div>
    </body>
window.onload = rotate;

var theAD = 0;
var adImage = new Array("images/dog1.jpg","images/dog2.jpg","images/dog3.jpg","images/dog4.jpg",)

function rotate(){
    theAD++;
    if(theAD == adImage.length){
        theAD=0;
    }
    document.getElementById("dog").src = adImage[theAD];
    setTimeout(rotate,1000);//告诉脚本每隔多少时间执行一次,单位为毫秒
}

在循环广告中添加链接

<a href="dog1.html"><img src="images/dog1.JPG" id="dog" alt="dog"></a>
window.onload = initDogLink;

var theAD = 0;
var adURL = new Array("dog1.html","dog2.html","dog3.html","dog4.html");
var adImage = new Array("images/dog1.jpg","images/dog2.jpg","images/dog3.jpg","images/dog4.jpg");
//adURL必须与adImage的数组数量相同
function initDogLink(){
    if(document.getElementById("dog").parentNode.tagName == "A"){
        document.getElementById("dog").parentNode.onclick = newLocation;
    }
    rotate();
}
function newLocation(){
    document.location.href = "http://www." + adURL[theAD];
    return false;
}
function rotate(){
    theAD++;
    if(theAD == adImage.length){
        theAD=0;
    }
    document.getElementById("dog").src = adImage[theAD];
    setTimeout(rotate,1000);
}

建立循环式幻灯片

每次显示一个图像,让用户可以控制显示图像的进度

<a href="dog1.html"><img src="images/dog1.JPG" id="dog" alt="dog"></a>
<h3><a href="previous.html" id="prevLink">&lt;&lt;previous</a>&nbsp;&nbsp;<a href="next.html" id="nextLink">next&gt;&gt;</a></h3>
window.onload = initDogLink;

var thePic = 0;
var adImage = new Array("images/dog1.jpg","images/dog2.jpg","images/dog3.jpg","images/dog4.jpg");

function initDogLink(){
    document.getElementById("prevLink").onclick = processPrevious;
    document.getElementById("nextLink").onclick = processNext;
}
function processPrevious(){
    if(thePic == 0){//此时显示的图片是adImage[0],thePic为0
        thePic = adImage.length;
    }
    thePic--;
    document.getElementById("dog").src = adImage[thePic];
    return false;
}
function processNext(){
    thePic++;
    if(thePic == adImage.length){
        thePic = 0;
    }
    document.getElementById("dog").src = adImage[thePic];
    return false;
}

 

 

 

 

 

 显示随机图像

<img src="images/dog1.JPG" id="dog" alt="dog">
window.onload = choosePic;
var Pic = new Array("images/dog1.jpg","images/dog2.jpg","images/dog3.jpg","images/dog4.jpg");
function choosePic(){
    var randomNum = Math.floor(Math.random()*Pic.length);
    document.getElementById("dog").src = Pic[randomNum];
}

随机开始循环显示图像

<img src="images/dog1.JPG" id="dog" alt="dog">
window.onload = choosePic;
var randomNum = 0;
var Pic = new Array("images/dog1.jpg","images/dog2.jpg","images/dog3.jpg","images/dog4.jpg");
function choosePic(){
    var randomNum = Math.floor(Math.random()*Pic.length);
    document.getElementById("dog").src = Pic[randomNum];
    rotate();
}
function rotate(){
    randomNum++;
    if(randomNum == Pic.length){
        randomNum=0;
    }
    document.getElementById("dog").src = Pic[randomNum];
    setTimeout(rotate,1000);
}

“学习太痛苦了”

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值