JQuery轮播图

平移轮播:

预览:
https://jirengu-inc.github.io/jrg-renwu8/homework/%E5%BC%A0%E8%BD%A9/renwu27-1.html#
源码:

<html>
<meta http-equiv="content-Type" content="text/html;charset=utf-8"/>
<script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js'>
</script>
<style>
        *{
            padding:0px;
            margin:0px;
        }
    li{
        list-style:none;
    }
    .ul{
        width:2880px;
        position:relative;
        left:-960px;
    }
    .ul li{
        float:left;
        left:100px; 
    }
    .clearfloat::after{
        content: ' ';
        display: block;
        clear: both;
    }
    .clearfloat::before{
        content: ' ';
        display: block;
        clear: both;
    }
    .wrap{
        width:480px;
        height:360px;
        position:absolute; 
        overflow:hidden;
        margin:20px;
    }
    .wrap>a{
        text-decoration:none;
        width:30px;
        height:30px;
        line-height:30px;
        padding-left:0px;
        margin:15px;    
        border-radius:30px;
        position:absolute;
        z-index:10;
        top:40%;
        font-size:15px;
        text-align: center;
        background-color:black;
        color:#fff;
        opacity:0.4;
    }
    .wrap .right{
        right:0px;
    }
    .wrap>a:hover{
        opacity:0.7;
    }
    .choosebtn{
        height: 12px;
        width: 200px;
        position: absolute;
        top: 90%;
        left:30%;
    }

    .choosebtn a{
        float: left;
        height: 10px;
        width: 40px;
        z-index: 10px;
        background-color: grey;
        border-radius: 5px;
        margin-left: 8px;
        opacity: 0.6;
    }
    .choosebtn a:hover{
        opacity: 1;
    }
    .choosebtn .choosebtn-1{
        opacity: 1;
    }
    
</style>
</head>
<body>
    <div class='wrap'>
        <ul class='ul clearfloat'>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-357ec1b8222eb27b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-9e3549423ccad79e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-cdbf88bac9a43133.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-6e94ce704ce7a36f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-357ec1b8222eb27b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-9e3549423ccad79e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
        </ul>
        <ul class='choosebtn clearfloat'>
            <li><a href='#' class='choosebtn-1'></a></li>
            <li><a href='#'></a></li>
            <li><a href='#'></a></li>
            <li><a href='#'></a></li>
        </ul>
        <a href='#' class='left'><</a>
        <a href='#' class='right'>></a>
    </div>
    
    <script>
        var $l=$('.left'),
            $r=$('.right'),
            $ul=$('.ul'),
            $choosebtn=$('.choosebtn'),
            state=true;
        function leftmove(){
            clearInterval(animating);
            $ul.animate({left:'+=480'},1500, backdiv);
            animating=setInterval(rightmove, 4000);
        }
        function rightmove(){
            clearInterval(animating);
            $ul.animate({left:'-=480'},1500, backdiv);
            animating=setInterval(rightmove, 4000);
        }
        function backdiv(){
            if($ul.css('left')=='0px'){
                $ul.css({left:'-1920px'});
            }
            if($ul.css('left')=='-2400px'){
                $ul.css({left:'-480px'});
            }
            btnclass(btn.location());
            state=true;
            
        }
        function btnclass(num){
            $choosebtn.children().children().removeClass('choosebtn-1');
            $choosebtn.children().eq(num).children().addClass('choosebtn-1');
        }
        var btn={
            location:function(){
                var num;
                if(parseInt($ul.css('left'))/480+2==0){
                    num=0;
                };
                if(parseInt($ul.css('left'))/480+2==-1){
                    num=1;
                };
                if(parseInt($ul.css('left'))/480+2==-2){
                    num=2;
                };
                if(parseInt($ul.css('left'))/480+2==1){
                    num=3;
                };
                return num;
            },
            clicklocation: function(p){
                return p.index('a')-6
            }
        }
        
        

        $l.on('click', function(){
            if(!state){return;};
            state=false;
            leftmove(); 
        })
        $r.on('click', function(){
            if(!state){return;};
            state=false;
            rightmove();
        })
        $choosebtn.on('click', 'a', function(){
            clearInterval(animating);
            var tmp=btn.location()-btn.clicklocation($(this));
            if(tmp==2||tmp==3){
                $ul.css({left:'-2400px'});
            }
            if(tmp>0){
                $ul.animate({left:'+='+tmp*480},1500, backdiv);
            }
            if(tmp<0){
                $ul.animate({left:'-='+Math.abs(tmp)*480},1500, backdiv);
            }
            else{
                return;
            }
            animating=setInterval(rightmove, 4000);
        });
        var Timeout=setTimeout(rightmove, 2000),animating;
    </script>
</body>
</html>

渐变轮播

预览:
https://jirengu-inc.github.io/jrg-renwu8/homework/%E5%BC%A0%E8%BD%A9/renwu27-2.html
源码:

<html>
<meta http-equiv="content-Type" content="text/html;charset=utf-8"/>
<script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js'>
</script>
<style>
        *{
            padding:0px;
            margin:0px;
        }
    li{
        list-style:none;
    }
    .ul{
        width:2880px;
        position:relative;
    }
    .ul li{
        position:absolute;
        float:left;
        display:none;
    }
    .ul li:first-child{
        display:block;
    }
    .clearfloat::after{
        content: ' ';
        display: block;
        clear: both;
    }
    .clearfloat::before{
        content: ' ';
        display: block;
        clear: both;
    }
    .wrap{
        width:480px;
        height:360px;
        position:absolute; 
        overflow:hidden;
        margin:20px;
    }
    .wrap>a{
        text-decoration:none;
        width:30px;
        height:30px;
        line-height:30px;
        padding-left:0px;
        margin:15px;    
        border-radius:30px;
        position:absolute;
        z-index:10;
        top:40%;
        font-size:15px;
        text-align: center;
        background-color:black;
        color:#fff;
        opacity:0.4;
    }
    .wrap .right{
        right:0px;
    }
    
    .choosebtn{
        height: 12px;
        width: 200px;
        position: absolute;
        top: 90%;
        left:30%;
    }
    .choosebtn a{
        float: left;
        height: 10px;
        width: 40px;
        z-index: 10px;
        background-color: grey;
        border-radius: 5px;
        margin-left: 8px;
        opacity: 0.6;
    }
    .choosebtn a:hover{
        opacity: 1;
    }
    .choosebtn .choosebtn-1{
        opacity: 1;
    }
    
</style>
</head>
<body>
    <div class='wrap'>
        <ul class='ul clearfloat'>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-6d5d84005da77b16.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-4e058bfbad78fd36.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-bf7ac22d91c04f47.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
            <li><a href='#'>![](http://upload-images.jianshu.io/upload_images/1844479-30efc129e9f5b48c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
        </ul>
        <ul class='choosebtn clearfloat'>
            <li><a href='#' class='choosebtn-1'></a></li>
            <li><a href='#'></a></li>
            <li><a href='#'></a></li>
            <li><a href='#'></a></li>
        </ul>
        <a href='#' class='left'><</a>
        <a href='#' class='right'>></a>
    </div>
    
    <script>
        var $l=$('.left'),
                $r=$('.right'),
                $ul=$('.ul'),
                $ul_child=$('.ul').children(),
                $choosebtn=$('.choosebtn');
        function leftmove(){
      clearInterval(time);
      if(picstart()==3){btnclass(0);}
      else{btnclass(picstart()+1);}
            changeopacity('left', picstart());

        }
        function rightmove(){
      clearInterval(time);
      if(picstart()==3){btnclass(0);}
      else{btnclass(picstart()+2);}
            changeopacity('right', picstart());
      time=setInterval(rightmove, 3000);
        }
        function clickbtn(){
      clearInterval(time);
            btnclass($(this).index());
      $ul_child.hide('slow');
      $ul_child.eq($('.choosebtn a').index($(this))).show('slow');
      time=setInterval(rightmove, 3000);


        }
        function btnclass(num){
            $choosebtn.children().children().removeClass('choosebtn-1');
            $choosebtn.children().eq(num).children().addClass('choosebtn-1');
        }
        function picstart(){
            return $ul.children(":visible").index();

        }
        function changeopacity(action, piclocation){
      $ul_child.hide('slow');
            if(action=='left'){
                if(piclocation==0){
                    $ul_child.last().show('slow');
                }
                else{
                    $ul_child.eq(piclocation-1).show('slow');
                }
            }
            if(action=='right'){
                if(piclocation==$ul_child.length-1){
                    $ul_child.first().show('slow');
                }
                else{
                    $ul_child.eq(piclocation+1).show('slow');
                }
            }
        }   
        
        $l.on('click', leftmove);
        $r.on('click', rightmove);
        $choosebtn.on('click', 'a', clickbtn);
    var time=setTimeout(rightmove, 3000);
        
    </script>
</body>
</html>

全屏轮播:

预览:
https://jirengu-inc.github.io/jrg-renwu8/homework/%E5%BC%A0%E8%BD%A9/renwu27-3.html
源码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>无线轮播</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
  
    <style type="text/css">

    
    html,body,ul,li,p{
      margin: 0;
      padding: 0;
    }
    li{
      list-style: none;
    }
    a{
      text-decoration: none;
    }
    html, body, #header, #header .carousel, #header .img-ct{
      height: 100%;
      width: 100%;
    }
    .carousel{
      position: relative;
      width: 310px;
      height: 206px;
      overflow: hidden;
    }
    #header .img-ct{
      position: absolute;
    }
    #header .img-ct:after{
      content: '';
      display: table;
      clear: both;
    }
    #header .item{
      position: relative;
      float: left;
      height: 100%;
    }
    #header .main{
      position: absolute;
      width: 600px;
      left: 50%;
      top: 50%;
      z-index: 1;
      transform: translate(-50%, -50%);
      color: #fff;
      text-align: center;
    }
    #header .main h3{
      font-size: 34px;
    }
    #header .main p{
      font-size: 16px;
      line-height: 2;
    }
      
    #header .cover{
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background-repeat: no-repeat;
      background-position: center center;
      background-size: cover;
      opacity: 0.8;
    }



  
    .arrow{
      position: absolute;
        top: 50%;
        margin-top: -15px;
        width: 30px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        background: #4E443C;
        color: #fff;
        border-radius: 30px;
        box-shadow: 0 0 2px #999;
        opacity: 0.8
    }
    .arrow:hover {
        opacity: 1;
    }
    .pre{
      left: 10px;
    }
    .next{
      right: 10px;
    }
    ul.bullet {
          position: absolute;
        bottom: 10px;
        left: 50%;
        transform: translateX(-50%);
    }
    ul.bullet li {
        width: 36px;
        height: 10px;
        border-radius: 4px;
        background: #fff;
        display: inline-block;
        cursor: pointer;
    }

    ul.bullet li.active {
        background: #666;
    }


    </style>

  </head>
  <body>
  <div id="header">
    <div class="carousel">
      <ul class="img-ct">

            <li class="item">
              <div class="cover" data-bg-img="http://cdn.jirengu.com/book.jirengu.com/img/6.jpg" style="width: 1920px; background-image: url("http://cdn.jirengu.com/book.jirengu.com/img/6.jpg");"></div>
              <div class="main">
                <h3>夜空中最亮的星</h3> 
                <p>是否在意</p>
              </div>
            </li>
            <li class="item">
              <div class="cover" data-bg-img="http://cdn.jirengu.com/book.jirengu.com/img/7.jpg" style="width: 1920px; background-image: url("http://cdn.jirengu.com/book.jirengu.com/img/7.jpg");"></div>
              <div class="main">
                <h3>只是因为在人群中看了你一眼</h3> 
                <p>是否在意</p>
              </div>
            </li>
            <li class="item">
              <div class="cover" data-bg-img="http://cdn.jirengu.com/book.jirengu.com/img/8.jpg" style="width: 1920px; background-image: url("http://cdn.jirengu.com/book.jirengu.com/img/8.jpg");"></div>
              <div class="main">
                <h3>往城市边缘开</h3> 
                <p>把车窗都咬下来</p>
              </div>
            </li>
            <li class="item">
              <div class="cover" data-bg-img="http://cdn.jirengu.com/book.jirengu.com/img/9.jpg" style="width: 1920px; background-image: url("http://cdn.jirengu.com/book.jirengu.com/img/9.jpg");"></div>
              <div class="main">
                <h3>夜空中最亮的星</h3> 
                <p>是否在意</p>
              </div>
            </li>
      </ul>
      <ul class="bullet">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>    
  </div>



    
    <script type="text/javascript">
    //$(function(){
      
      var $ct = $('.img-ct'),
        $items = $ct.children(),
        $pre = $('.pre'),
        $next = $('.next'),
        $bullet = $('.bullet'),
        imgWidth = $(window).width(),
        imgCount = $ct.children().length;

      $ct.prepend($items.last().clone());
      $ct.append($items.first().clone());
      $ct.find('.item').css('width', imgWidth);
      $ct.find('.cover').css('width', imgWidth);
      imgRealCount = $ct.children().length;
      $ct.css({left: 0-imgWidth, width: imgRealCount*imgWidth})


      var curIdx=0;

      function startClock(){
        clock= setInterval(function(){
          playNext();
        },3000);
      }
      function stopClock(){
        clearInterval(clock);
      }

      startClock();

      $bullet.find('li').on('click', function(){
        var idx=$(this).index();
        if(idx> curIdx){
          playNext(idx- curIdx)
        }
        else if(idx< curIdx){
          playPrev(curIdx- idx)
        }
      })


      function playNext(idx){
        $ct.animate({left:'-='+idx*imgWidth},function(){
          curIdx=(curIdx+ idx)%imgCount;
          if(curIdx==0){
            $ct.css({left:'0'-imgWidth});
          }
          setBullet();
        });
      }

      function playPrev(idx){
        $ct.animate({left:'+='+idx*imgWidth},function(){
          curIdx=(imgCount+ curIdx- idx)%imgCount;
          if(curIdx==(imgCount-1)){
            $.css({left:'0'-imgCount*imgWidth});
          }
          setBullet();
        })
      }

      function setBullet(){
        $bullet.children().removeClass('active').eq(curIdx).addClass('active');
      }


    
    </script>
  </body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容