jquery實現圖片輪播淡入淡出效果

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {margin:0; padding:0;}
        #container {width:605px; height:284px; position:relative; border:1px solid; margin:0 auto;}
        .topic {position:absolute; top:0px; left:0px; display:none;}
        #pages {width:100%; height:20px; padding:5px 0; background:black; position:absolute; left:0; bottom:0;}
        #pages div {width:20px; height:20px; border-radius:10px; background:white; margin-left:10px; float:left;}
        #pages div.curr {background:red;}
        #prev, #next { width:50px; height:100px; position:absolute; top:0; bottom:0; margin:auto 0; background:black; opacity:0.5; color:white; font-size:30px; line-height:100px; text-align:center;}
        #next {right:0;}
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(function(){
            var curIndex=0,
                nextIndex=1,
                len=$(".topic").length;

               //小圓點的點擊事件的實現
                for(var i=0;i<len;i++){
                    $("<div></div>").appendTo("#pages");
                }
            $("#pages div").eq(0).addClass("curr");
            //小圓點的點擊事件
            $("#pages div").click(function(){
                if(curIndex===$(this).index()){
                    return;
                }
                nextIndex=$(this).index();
                move();
            });
            //上一頁
            $("#prev").click(function(){
                nextIndex=curIndex-1;
                if(nextIndex<0){
                    nextIndex=len-1;
                }
                move();
            });
            //下一頁
            $("#next").click(function(){
                move();
            });

            //運動函數
            function move(){
                $("#pages div").eq(curIndex).removeClass("curr");
                $("#pages div").eq(nextIndex).addClass("curr");
                $(".topic").eq(curIndex).fadeOut();
                $(".topic").eq(nextIndex).fadeIn();
                curIndex=nextIndex;
                nextIndex++;

                if(nextIndex===len){
                    nextIndex=0;
                }
            }
            //滑鼠移入時停止計時器,移出時開啟計時器
            $("#container").hover(function(){
                clearInterval(time);
            },function(){
                time=setInterval(move,3000);
            }).trigger("mouseleave");
            
        });
    </script>
</head>
<body>
    <div id="container">
        <div class="box">
            <div class="topic" style="display:block;">
                <a href="#"><img src="img/1.jpg" height="284" width="605"></a>
            </div>
            <div class="topic">
                <a href="#"><img src="img/2.jpg" height="284" width="605"></a>
            </div>
            <div class="topic">
                <a href="#"><img src="img/3.jpg" height="284" width="605"></a>
            </div>
            <div class="topic">
                <a href="#"><img src="img/4.jpg" height="284" width="605"></a>
            </div>
            <div class="topic">
                <a href="#"><img src="img/5.jpg" height="284" width="605"></a>
            </div>
            <div class="topic">
                <a href="#"><img src="img/6.jpg" height="284" width="605"></a>
            </div>
        </div>
        <div id="pages"></div>
        <div id="prev">&lt;</div>
        <div id="next">&gt;</div>
    </div>
</body>
</html>

jquery實現圖片輪播淡入淡出效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.