简单的轮播图


<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="Generator" content="EditPlus®">
        <meta name="Author" content="">
        <meta name="Keywords" content="">
        <meta name="Description" content="">
        <title>轮播图2title>
        <style>
            *{margin:0px;padding:0px;font-size:12px;color:#666;}
            li{list-style: none;}
            #warp{position:relative;width: 970px;height: 300px;margin:  50px auto;}
            #warp #box{position: relative;width: 970px;height: 270px;overflow: hidden;}

            #warp #box li{position: absolute;width: 0;height: 0;top: 135px;left: 485px;opacity:0;
                filter:alpha(opacity=0);}

            #warp #box li span{background: rgba(0,0,0,.4);filter:alpha(opacity=50);position: absolute;left: 0;top: 0;width: 100%;height: 100%;display: block;}

            #warp #box li img{width: 100%;height: 100%;}

            #warp #box li.img1{width: 640px;height: 270px;left: 165px;top: 0px;z-index: 10;opacity: 1;filter:alpha(opacity=100);}
            #warp #box li.img2{ width:530px; height:220px; left:440px; top:25px; z-index:9;opacity:1;
                filter:alpha(opacity=100);}
            #warp #box li.img0{ width:530px; height:220px; left:0px; top:25px; z-index:9;opacity:1;
                filter:alpha(opacity=100);}

            #warp #box li.img3{ width:300px; height:150px; left:970px; top:60px; z-index:8;}
            #warp #box li.img9{ width:300px; height:150px; left:-300px; top:60px; z-index:8;}

            #warp #w-but .but{position:absolute;width: 45px;height: 45px;top: 50%;margin-top: -22px;z-index: 11;cursor: pointer;}
            #warp #w-but #w-b-left{left:10px;background: url(img/left.png);}
            #warp #w-but #w-b-right{right:10px;background: url(img/right.png);}

            #warp #tab{position: absolute;left: 50%;margin-left: -250px;bottom: 10px;}
            #warp #tab:after{content: '';display: block;clear: both;}
            #warp #tab li{width: 40px;height: 8px;margin: 0px 5px;float: left;background: rgba(0,0,0,.8);border-radius: 1px;cursor: pointer;}
            #warp #tab li.on{background: rgba(0,0,0,.1);}

        style> 
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js">script>
    head>
    <body>
        <div id="warp">
            <div id="box">
                <li class= "img1"><img src="img/1.jpg">li>
                <li class= "img2"><img src="img/2.jpg">li>
                <li class= "img3"><img src="img/3.jpg">li>
                <li class= ""><img src="img/4.jpg">li>
                <li class= ""><img src="img/5.jpg">li>
                <li class= ""><img src="img/6.jpg">li>
                <li class= ""><img src="img/7.jpg">li>
                <li class= "img8"><img src="img/8.jpg">li>
                <li class= "img9"><img src="img/9.jpg">li>
                <li class= "img0"><img src="img/10.jpg">li>
            div>
            <div id="w-but">
                <div id="w-b-left" class="but">div>
                <div id="w-b-right" class="but">div>
            div>
            <ul id="tab">
                <li class="on">li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
                <li>li>
            ul>
        div>

        <script>

        (function(){
            var $butLeft = $("#w-b-left");
            var $butRight = $("#w-b-right");

            var $li = $("#box li");
            $li.append("");
            $("#box li span").eq(0).css("display","none");
            //index 是为了 锁定当前的图片我抛锚点的下标。
            var index= 0;
            var $tabLi = $("#tab li");
            var arryW = [];var arryH = [];var arryL = [];var arryT = [];var arryO = [];var arryZ = [];var arryS = [];
            // timenow  和 timers是为了控制点击的时间间隔
            var timenow = 0;
            var timers = 0;
            //定义抛锚点的点击事件。

                $tabLi .on("click" , function(){

                    if (new Date()-timers>1  000) {


                        $(this).addClass('on').siblings().removeClass('on');
                        var nowIndex = $(this).index();
                        var i = nowIndex-index;

                        //如果是两个相邻的话
                        if (i == 1 || i == -($tabLi.length-1)) {
                            fn(true);
                            index = nowIndex;
                        }
                        if (i == -1) {
                            fn(false);
                            index = nowIndex;
                        }
                        //如果两个抛锚点不相邻的话:
                        else if (nowIndex != index) {

                            //将当前的css样式全部都保存下来。
                            $li.each(function(i){
                                arryW[i] = $(this).css("width");
                                arryH[i] = $(this).css("height");
                                arryL[i] = $(this).css("left");
                                arryT[i] = $(this).css("top");
                                arryO[i] = $(this).css("opacity");
                                arryZ[i] = $(this).css("z-index");
                                arryS[i] = $(this).find('span').css('display');
                            });

                            //将所有的元素全都怼回去
                            $li.each(function(){
                                $(this).animate({
                                    width : 0,
                                    left:"485px",
                                    top: "135px",
                                    opacity: 0,
                                    height : 0
                                },300);             
                            });

                            //就nowindex 的图片显示在中间。
                            $li.each(function(i){
                                //得到当前元素要调转之后的下表    但是有可能是负数也有可能是大于总下表的数  需要进行处理。

                                var a = i-(nowIndex-index);
                                if (a<0) {a += $li.length;}
                                if (a>=$li.length) {a -= $li.length;}

                                $(this).find("span").css('display' , arryS[a]);
                                $(this).css("zIndex" , arryZ[a]).animate({
                                    width:arryW[a],
                                    height:arryH[a],
                                    left:arryL[a],
                                    top:arryT[a],
                                    opacity:arryO[a]
                                },500);
                            });
                            index = nowIndex;
                        }   
                        timers = new Date();
                    }

                });


            /*切换图片的功能
                1.对所有的图片的状态进行保存。
                1.对所有的li 的状态经行更换  即是用的是前一个的css

            */
            function fn (x){

                $li.each(function(i){
                    arryW[i] = $(this).css("width");
                    arryH[i] = $(this).css("height");
                    arryL[i] = $(this).css("left");
                    arryT[i] = $(this).css("top");
                    arryO[i] = $(this).css("opacity");
                    arryZ[i] = $(this).css("z-index");
                    arryS[i] = $(this).find('span').css('display');
                });

                $li.each(function(i){
                    //判断点击的是那个按键
                    if (x) {
                        var a= i-1;
                        if (a<0) {a=$li.length-1;}
                    }else{
                        var a = i+1;
                        if (a == $li.length) {a = 0;}
                    }
                    $(this).find('span').css("display" , arryS[a]);
                    $(this).css("zIndex",arryZ[a]).animate({
                        width:arryW[a],
                        height:arryH[a],
                        left:arryL[a],
                        top:arryT[a],
                        opacity:arryO[a]
                    },500);
                });
            }

            //对right按钮添加点击事件
            $butLeft.on("click",function (){
                //这里的index是指在父级元素的的序号。
                if (new Date()-timenow>500) {
                    timenow = new Date();
                    index --;
                    if(index<0)index=$tabLi.length-1;
                    $tabLi.eq(index).addClass('on').siblings().removeClass('on');
                    fn($(this).index());
                }
            });
            //对left按钮添加点击事件
            $butRight.on("click",function (){
                if (new Date()-timenow>500) {
                    timenow = new Date();  
                    index ++;
                    index %= $tabLi.length;
                    $tabLi.eq(index).addClass('on').siblings().removeClass('on');
                    fn($(this).index());
                }           
            });
        })();
        script>
    body>
html>

你可能感兴趣的:(js特效)