幻灯片代码

[复制链接]
查看: 219   回复: 0

67

主题

67

帖子

368

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
368
2022-9-26 02:18:50   显示全部楼层   阅读模式  
幻灯片代码.png


效果查看网址

https://www.xiefansq.cn/jsTest/2022/9/9-26lunbotupian/





  1. <!DOCTYPE html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title>图片轮播js-threejs百宝箱</title>
  6.                 <style>
  7.                         body { margin: 0; }
  8.                 </style>
  9.             <style>
  10.                 .outer{
  11.                     width: 1000px;
  12.                     height: 450px;
  13.                     /* border:4px solid black; */
  14.                     position: relative;
  15.                     border-radius: 10px;
  16.                     overflow: hidden;
  17.                     margin: 0 auto ;
  18.                     top:20px;
  19.                
  20.                 }
  21.             
  22.                 .inner{
  23.                     height: 100%;
  24.                     width:  100%;
  25.                     background-color: aquamarine;
  26.                     position: relative;
  27.                     left: 0px;
  28.                     display: flex;
  29.                     /* 过度动画 */
  30.                     transition: all 1s;

  31.                 }
  32.                 .outer .inner .a {
  33.                     height: 100%;
  34.                     width:  100%;

  35.                     flex-shrink: 0;
  36.                     object-fit: cover;
  37.                 }
  38.                 .outer .inner .a .img{
  39.                     height: 100%;
  40.                     width:  100%;

  41.                     flex-shrink: 0;
  42.                     object-fit: cover;
  43.                 }

  44.                 .outer .next .btn{
  45.                     /* 绝对定位并且居中切换按钮 */
  46.                     position: absolute;
  47.                     top: 0;
  48.                     bottom: 0;
  49.                     margin: auto 0;


  50.                     background-color: blue;
  51.                     color: white;
  52.                     width: 50px;
  53.                     height: 50px;
  54.                     border-radius: 8px;
  55.                     font-size: 40px;
  56.                     font-weight: bold;
  57.                     /* 字符水平居中 */
  58.                     line-height: 50px;
  59.                     text-align: center;
  60.                     user-select: none;
  61.                     display: flex;
  62.                 }
  63.                 .outer .next .left{
  64.                     left: 20px;
  65.                 }
  66.                 .outer .next .right{
  67.                     right: 20px;
  68.                 }
  69.                
  70.                .outer .nextBiao{
  71.                   /* 绝对定位并且居中切换按钮 */
  72.                   bottom: 20px;
  73.                   position: absolute;
  74.                    left: 0;
  75.                    right: 0;
  76.                  width: max-content;
  77.                     margin: 0 auto ;
  78.                     opacity: 0.2;

  79.                }
  80.                 .nextBiao .btn{
  81.                   

  82.                     height: 5px;
  83.                     width: 40px;
  84.                     background-color: blue;
  85.                     cursor: pointer;
  86.                   
  87.                 }
  88.                 .outer:hover .nextBiao{
  89.                     opacity:1;
  90.                 }
  91.             
  92.             
  93.             </style>
  94.         </head>
  95.         <body>

  96.           <div class="outer">
  97.            
  98.             <div class="inner">
  99.                 <a href="https://www.xiefansq.cn/" target="_blank"><img src="./img/1.jpg"></a>
  100.                 <a href="https://www.xiefansq.cn/" target="_blank"> <img src="./img/2.jpg"></a>
  101.                 <a href="https://www.xiefansq.cn/" target="_blank"> <img src="./img/3.jpg"></a>
  102.                
  103.                
  104.             
  105.             </div>
  106.             <div class="next">
  107.                 <button class="btn left" onclick="leftS()"><</button>
  108.                 <button class="btn right" onclick="rightS()">></button>
  109.             </div>
  110.          
  111.          
  112.             <div class="nextBiao">
  113.                 <button class="btn" onclick="setindex(0)"></button>
  114.                 <button class="btn" onclick="setindex(1)"></button>
  115.                 <button class="btn" onclick="setindex(2)"></button>
  116.             </div>
  117.           </div>
  118.           <script>
  119.             //自动滚动
  120.             // setInterval(()=>{
  121.             //     index++
  122.             //     nextImg()
  123.             // },3000 )
  124.             //重置自动滚动
  125.             function createAuto(){
  126.                 return  setInterval(()=>{
  127.                 index++
  128.                 nextImg()
  129.             },3000 )
  130.             }
  131.             let autoTimer=createAuto()
  132.             //___________
  133.             let index=0
  134.             function nextImg(){
  135.                 //获取轮播框元素
  136.                 let outDiv =document.querySelector(".outer")

  137.                 //获取轮播框宽度

  138.                 //限制下标记范围
  139.                 if(index>2){
  140.                     index=2
  141.                 }else if(index<0){
  142.                     index=0
  143.                 }
  144.                 let width = getComputedStyle(outDiv).width
  145.                 width = Number(width.slice(0,-2))
  146.                 outDiv.querySelector(".inner").style.left=index*width*-1+"px"
  147.             }

  148.             nextImg()

  149.             function leftS(){
  150.                 index--
  151.               
  152.                 nextImg()
  153.                 clearInterval(autoTimer)
  154.                 let autoTimer=createAuto()
  155.             }
  156.             function rightS(){
  157.                 index++
  158.               
  159.                 nextImg()
  160.                 clearInterval(autoTimer)
  161.                 let autoTimer=createAuto()

  162.             }
  163.             function setindex(num){
  164.                 index=num
  165.                 nextImg()
  166.             }
  167.           </script>
  168.         </body>
  169. </html>
复制代码





回复

使用道具 举报

您需要登录后才可以回帖   登录 立即注册

高级模式

南通谢凡软件科技有限公司