自动播放 幻灯片效果

body代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>自动播放---幻灯片效果</title>
<link  rel="stylesheet" style="text-align" href="css/styles.css"/>
<script type="text/javascript" src="script/function.js"></script>
</head>

<body>
<div id="box">
    <div class="list">
        <ul>
            <li><img src="image/01.jpg" width="490" height="170" /></li>
            <li><img src="image/02.jpg" width="490" height="170" /></li>
            <li><img src="image/03.jpg" width="490" height="170" /></li>
            <li><img src="image/01.jpg" width="490" height="170" /></li>
            <li><img src="image/02.jpg" width="490" height="170" /></li>
            <li><img src="image/03.jpg" width="490" height="170" /></li>
         
        </ul>
    </div>
</div>
</body>
</html>

css样式:

body,div,ul,li{margin:0;padding:0;}
ul{list-style-type:none;}
body{background:#000;text-align:center;font:12px/20px Arial;}
#box{position:relative;width:492px;height:172px;background:#fff;border-radius:5px;border:5px solid #fff;margin:10px auto;cursor:pointer;}
#box .list{position:relative;width:490px;height:170px;overflow:hidden;}
#box .list ul{position:absolute;top:0;left:0;}
#box .list li{width:490px;height:170px;overflow:hidden;}
#box .count{position:absolute;right:0;bottom:5px;}
#box .count li{color:#fff;float:left;width:20px;height:20px;cursor:pointer;margin-right:5px;overflow:hidden;background:#F90;opacity:0.7;filter:alpha(opacity=70);border-radius:20px;}
#box .count li.current{color:#fff;opacity:1;filter:alpha(opacity=100);font-weight:700;background:#f60;}
#tmp{width:100px;height:100px;background:red;position:absolute;}

javascript:

//自动播放幻灯片
window.onload = function ()
{
    var oBox = document.getElementById("box");
    var oList = oBox.getElementsByTagName("ul")[0];
    var aImg = oBox.getElementsByTagName("img");
    var timer = playTimer = null;
    var index = i = 0;
    var bOrder = true;
    var aTmp = [];
    
    //生成数字按钮
    for (i = 0; i < aImg.length; i++)
    {
        aTmp.push("<li>" + (i + 1) + "</li>")
    }
    
    //插入元素
    var oCount = document.createElement("ul");
    oCount.className = "count"
    oCount.innerHTML = aTmp.join("");
    oBox.appendChild(oCount);
    
    //初始化状态
    var aBtn = oBox.getElementsByTagName("ul")[1].getElementsByTagName("li");
    aBtn[index].className = "current";
    
    //按钮点击切换
    for (i = 0; i < aBtn.length; i++)
    {
        aBtn[i].index = i;
        aBtn[i].onclick = function ()
        {
            index = this.index;
            for (i = 0; i < aBtn.length; i++) aBtn[i].className = "";
            this.className = "current";            
            startMove(-(this.index * aImg[0].offsetHeight))        
        }
    }
    
    //鼠标移入展示区停止自动播放
    oBox.onmouseover = function ()
    {
        clearInterval(playTimer)
    };
    
    //鼠标离开展示区开始自动播放
    oBox.onmouseout = function ()
    {
        autoPlay()
    };
    
    //页面加载即调用自动播放
    oBox.onmouseout();
    
    //自动播放函数
    function autoPlay()
    {
        playTimer = setInterval(function ()
        {
            bOrder ? index++ : index--;            
            index == aBtn.length - 1 && (bOrder = false);
            index == 0 && (bOrder = true);
            aBtn[index].onclick();
            startMove(-(index * aImg[0].offsetHeight))
        }, 3000)
    }
    function startMove(iTarget)
    {
        clearInterval(timer);
        timer = setInterval(function ()
        {
            doMove(iTarget)
        }, 30)    
    }
    function doMove (iTarget)
    {
        var iSpeed = (iTarget - oList.offsetTop) / 10;
        iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);        
        oList.offsetTop == iTarget ? clearInterval(timer) : oList.style.top = oList.offsetTop + iSpeed + "px"
    }
};

图片就自己弄吧,效果很炫的。你可以试试

转载于:https://www.cnblogs.com/cbread/p/3418615.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值