• html+css+javascript实现简易轮播图片


    html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../css/test2.css">
    <script type="text/javascript" src="../js/test2.js"></script>
    <title>图片轮播</title>
    </head>
    <body onload="onPageLoaded()">
         <div class="s1"> 
            <div class="s2"><img src="../img/left.png" onclick="goLeftClick()"></img></div>
            <div class="s3"><img src="../img/right.png" onclick="goRightClick()"></img></div>
           
             <ul id="imgList" >        
                          <li > <img  src="../img/img1.jpg"></img></li>
                          <li > <img src="../img/img2.jpg"></img></li>
                           <li > <img  src="../img/img3.jpg"></img></li>  
                           <li > <img  src="../img/img4.jpg"></img></li>     
             </ul>
        
         </div>
    </body>
    </html>

    css:

    @CHARSET "UTF-8";
    
    
    body{
        
        width:950px;
        height:800px;
        background-color: silver;
        margin: 0 auto;
        border:1px solid red;
    }
    
    
    .s1{
        width:950px;
        height:250px;
        margin-top: 100px;
        background-color: orange;
        position:relative; /* 先将外面的div定位 */
        left:0;
        top:0;
        overflow: hidden;/* 自动隐藏超出的内容 */
        
    }
    
    .s2{
        /* background-color: blue;*/
        
         position:absolute;/* 再将里面的左右导航div定位 */
         left:30px;
         top:93px;
         z-index: 1;
    }
    .s3{
        /* background-color: blue;*/
         position:absolute;
         left:856px;
         top:93px;
         z-index: 1;
    }
    
    /*图像ul */
    .s1 ul{
        width:3800px; /* ul 宽度设置 所有图像的宽的总和 */
        height:250px;
        padding:0;         /* padding 设置0 */
        margin:0;        /* margin 设置0 */
        background-color: purple;
        overflow: hidden; /* 自动隐藏超出的内容 */
        
    }
    
    .s1 ul > li{
         width:950px;
         list-style-type: none;
         float: left;
    }
    .s1 ul img{
        width:950px;
        height:250px;
        /*max- 100%;*/
    }

    javascript:

    /**
     *  @description:
     *  @author Chenchen Yu
     *  @date 2016年11月23日
     *  @time 下午9:01:21
     */
    
     var k=0;
     var imgNum=4;//图片数目
     var imgWidth=950;
     function onPageLoaded(){
    
         setTimeout('goLeft()',2000);
        
     }
    
     //自动向左滑动图片
    function goLeft(){
           
          var imgList=document.getElementById('imgList');     
          marginLeft=-((k+1)%imgNum)*imgWidth;                
          if(marginLeft==0)
              {      
               
                imgList.style.marginLeft='0px';     
                k++;
                setTimeout('goLeft()',2000);
                return;
              } 
             slideLeft(imgList,marginLeft+imgWidth,marginLeft);
         // k++;
    }
    
    function slideLeft(imgList,start,marginLeft){
        //模拟滑动
        //var start=marginLeft+950;
        setTimeout('slideLeftByStep('+'imgList'+','+start+','+marginLeft+')',10);
    
    }
    
    function slideLeftByStep(imgList,dis,marginLeft){
        if(dis<marginLeft)
            {    
              k++;             
              setTimeout('goLeft()',2000);  
              return;
            }
          imgList.style.marginLeft=dis+'px';
    
          dis=dis-50;//step size
          slideLeft(imgList,dis,marginLeft);
    }
    //点击向右滑动图片
    function goRightClick(){
         var imgList=document.getElementById('imgList');
        
         if(k<=0||(k)%imgNum==0)
             {
              // imgList.style.marginLeft='0px';
               k=0;
               return;
             }
         k=k-2;//后退
        
         marginLeft=-((k+1)%imgNum)*imgWidth;    
         clickSlideRight(imgList,marginLeft-imgWidth,marginLeft);
         console.log('kk',marginLeft);
    //     imgList.style.marginLeft='0px';
    }
    
    
    function clickSlideRight(imgList,start,marginLeft){
    
        setTimeout('clickSlideRightByStep('+'imgList'+','+start+','+marginLeft+')',5);
    
    }
    
    function clickSlideRightByStep(imgList,dis,marginLeft){
        if(dis>marginLeft)
        {    
          k++; //
          return;
        }
        imgList.style.marginLeft=dis+'px';
        dis=dis+50;//step size
        clickSlideRight(imgList,dis,marginLeft);
    }
    
    
    //点击向左滑动图片
    function goLeftClick(){
         var imgList=document.getElementById('imgList');
         if((k+1)%imgNum==0)
             {
               k=0;
               return;
             } 
         marginLeft=-((k+1)%imgNum)*imgWidth;    
         clickSlideLeft(imgList,marginLeft+imgWidth,marginLeft);
    }
    
    
    function clickSlideLeft(imgList,start,marginLeft){
    
        setTimeout('clickSlideLeftByStep('+'imgList'+','+start+','+marginLeft+')',5);
    
    }
    
    function clickSlideLeftByStep(imgList,dis,marginLeft){
        if(dis<marginLeft)
        {    
          k++;//保持自动滑动同步
          return;
        }
        imgList.style.marginLeft=dis+'px';
        dis=dis-50;//step size
        clickSlideLeft(imgList,dis,marginLeft);
    }

    效果图:

  • 相关阅读:
    枚举、函数关于oracle函数listagg的使用说明by小雨
    执行、Mongodb MapReduce示例1个by小雨
    事务、异常TSQL 编码时应该注意的10个问题by小雨
    源、执行GoldenGate 单向DDL同步by小雨
    Oracle中的所有权限by小雨
    数据库、版本数据库学习从此开始by小雨
    统计、案例深入理解Oracle索引(10):索引列字符类型统计信息的32位限制by小雨
    字段、数据库表三大范式及存储方式by小雨
    数据库、用户第二章Getting Start with the Oracle Server(oracle入门)by小雨
    搜索、关键字截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果by小雨
  • 原文地址:https://www.cnblogs.com/njust-ycc/p/6107692.html
Copyright © 2020-2023  润新知