JQuery实现广告效果(滚动切换)

JQuery实现广告效果(滚动切换)Html+css

效果如上图

  代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../jquery-2.1.4.min.js"></script>
    <script language='javascript' src="../JS/guanggao.js">
    </script>
    <style type="text/css">
        /*滚动广告样式*/
        .ad {margin:10px;width:586px; height:150px;overflow:hidden;border:1px solid #AAAAAA;position:relative;}
        .slider,.num{position:absolute;}
        .ad ul{;padding: 0;margin: 0}
        .slider li{ list-style:none;display:inline;}
        .slider img{ width:586px; height:150px;display:block;}
        .num{ right:5px; bottom:5px;}
        .num li{float: left;color: #FF7300;text-align: center;line-height: 16px;width: 16px;height: 16px;font-family: Arial;font-size: 12px;cursor: pointer;overflow: hidden;margin: 3px 1px;border: 1px solid #FF7300;background-color: #fff;}
        .num li.on{color: #fff;line-height: 21px;width: 21px;height: 21px;font-size: 16px;margin: 0 1px;border: 0;background-color: #FF7300;font-weight: bold;}
    </style>
</head>
<body>
<div class="ad" >
    <ul class="slider" >
        <li><a href="#"><img src="../PICTURE/P/asb-160219.gif" alt="ad1"/></a></li>
        <li><a href="#"><img src="../PICTURE/P/htlogo.png" alt="ad2"/></a></li>
        <li><a href="#"><img src="../PICTURE/P/hw-160405.gif" alt="ad3"/></a></li>
        <li><a href="#"><img src="../PICTURE/P/logo_network_power.gif" alt="ad4"/></a></li>
        <li><a href="#"><img src="../PICTURE/P/logo.jpg" alt="ad5"/></a></li>
    </ul>
    <ul class="num" >
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
</div>
</body>
</html>

  

JS代码:

window.onload = function () {
    anime()
};
function anime(){
    $(function() {
        var len = $(".num > li").length;
        var index = 0;
        var adTimer;
        $(".num li").mouseover(function() {
            index = $(".num li").index(this);
            showImg(index);
        }).eq(0).mouseover();
        //滑入 停止动画,滑出开始动画.
        $('.ad').hover(function() {
            clearInterval(adTimer);
        }, function() {
            adTimer = setInterval(function() {
                showImg(index)
                index++;
                if (index == len) { index = 0; }
            }, 3000);
        }).trigger("mouseleave");
    })
// 通过控制top ,来显示不同的幻灯片
    function showImg(index) {
        var adHeight = $(".ad").height();
        $(".slider").stop(true, false).animate({ top: -adHeight * index }, 500);   //翻滚效果
        //$(".slider").css("top", -adHeight * index);     //跳转效果
        $(".num li").removeClass("on")
            .eq(index).addClass("on");
    }
}

相关推荐