轮播图的动画效果

实现的效果:图片匀速滑动,右下角的小图片也跟着变色。

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播图-焦点图</title>
</head>
<style>
    *{ padding:0; margin:0; list-style:none; border:0;}
    .all{
        width:500px;
        height:200px;
        padding:7px;
        border:1px solid #ccc;
        margin:100px auto;
        position:relative;
    }
    .screen{
        width:500px;
        height:200px;
        overflow:hidden;
        position:relative;
    }

    .screen li{ width:500px; height:200px; overflow:hidden; float:left;}
    .screen ul{ position:absolute; left:0; top:0px; width:3000px;}
    .all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
    .all ol li{ float:left; width:20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
    .all ol li.current{ background:yellow;}
</style>
<script type="text/javascript">
    /*匀速动画封装结束*/
    function animate(obj, target) {
        clearInterval(obj.timer);
        var speed = obj.offsetLeft < target ? 15 : -15;
        obj.timer = setInterval(function () {
            var result = target - obj.offsetLeft;
            obj.style.left = obj.offsetLeft + speed + "px";
            if (Math.abs(result) <= 15) {
                //说明到位置了,直接跳转到target
                clearInterval(obj.timer);
                obj.style.left = target + "px";
            }
        }, 30);
    }
    /*匀速动画封装结束*/

    window.onload = function () {
        var box = document.getElementById("all");  // 大盒子
        var ul = document.getElementById("ul");
        var ulLis = ul.children;

        //克隆第一张图片到最后一张图片的后面
        ul.appendChild(ul.children[0].cloneNode(true));

        //动态生成ol 和 li
        var ol = document.createElement("ol");
        box.appendChild(ol);
        for(var i=0;i<ulLis.length-1;i++) {
            var li = document.createElement("li");
            li.innerHTML = i + 1;
            ol.appendChild(li);
        }
        ol.children[0].className = "current";


        //开始动画部分
        var olLis = ol.children;
        var square = 0;
        for (var i = 0; i < olLis.length; i++) {
            olLis[i].index = i;
            olLis[i].onmouseover = function () {
                for (var j = 0; j < olLis.length; j++) {
                    olLis[j].className = "";
                }
                this.className = "current";
                //500为图片的宽度
                animate(ul, -this.index*500);
            }
        }

        var timer = null; //轮播图的定时器
        var key = 0;
        timer = setInterval(autoPlay, 2000);
        function autoPlay() {
            key++;
            if (key > ulLis.length - 1) {
                //迅速调回第一张图片
                ul.style.left = 0;
                //第六张就是第一张,所以此时key直接给1
                key = 1;
            }
            animate(ul, -key*500);

            square++;
            if (square > olLis.length - 1) {
                square = 0;
            }
            for (var i = 0; i < olLis.length; i++) {
                olLis[i].className = "";
            }
            olLis[square].className = "current";
        }

        //鼠标经过图片停止定时器
        box.onmousemove = function () {
            clearInterval(timer);
        }
        box.onmouseout = function () {
            //鼠标离开开始进行轮播
            timer = setInterval(autoPlay, 2000);
        }



    }

</script>
<body>
<div class="all" id='all'>
    <div class="screen">
        <ul id="ul">
            <li><img src="images/1.jpg" width="500" height="200" /></li>
            <li><img src="images/2.jpg" width="500" height="200" /></li>
            <li><img src="images/3.jpg" width="500" height="200" /></li>
            <li><img src="images/4.jpg" width="500" height="200" /></li>
            <li><img src="images/5.jpg" width="500" height="200" /></li>
        </ul>
    </div>
</div>
</body>
</html>

图片的宽度与代码中数值密切相关(本实例中宽度为500)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值