Jquery实现横滚图片展示框,此文只针对初级开发者

<!doctype html>
<html lang="en">

 <head>
  <meta charset="UTF-8" />
  <title>Document</title>
  <style type="text/css">
   * {
    margin: 0;
    padding: 0;
   }
   
   .fl {
    float: left;
   }
   
   .fr {
    float: right;
   }
   
   ul {
    list-style: none;
   }
   
   #showOuter {
    height: 200px;
    width: 400px;
    overflow: hidden;
    position: relative;
    border: 1px red solid;
    margin: 0 auto;
   }
   
   #bigContenter {
    height: 200px;
    width: 1200px;
    position: absolute;
   }
   
   #first {
    background: red;
    height: 200px;
    width: 400px;
    float: left;
   }
   
   #sencond {
    background: yellow;
    height: 200px;
    width: 400px;
    float: left;
   }
   
   #third {
    background: blue;
    height: 200px;
    width: 400px;
    float: left;
   }
   
   #num {
    height: 20px;
    width: 100%;
    border-bottom: 1px solid black;
    border-top: 1px solid black;
    position: absolute;
    bottom: 20px;
   }
   
   #num li {
    line-height: 20px;
    text-align: center;
    height: 18px;
    width: 20px;
    float: left;
    color: white;
    border-radius: 10px;
    border: 1px white solid;
    margin-left: 10px;
    cursor: pointer;
   }
   
   #num li:first-child {
    margin-left: 50px;
   }
   
   #num .act {
    background: gray;
    color: orange;
   }
   
   .swi {
    height: 40px;
    width: 20px;
    position: absolute;
    top: 90px;
    background: grey;
    text-align: center;
    line-height: 40px;
    display: none;
    cursor: pointer;
   }
   
   .btnL {
    left: 20px;
   }
   
   .btnR {
    right: 20px;
   }
  </style>

 </head>

 <body>
  <div id="showOuter">
   <div id="bigContenter">
    <div id="first">
     <ul>
      <li></li>
     </ul>
    </div>
    <div id="sencond">
     <ul>
      <li></li>
     </ul>
    </div>
    <div id="third">
     <ul>
      <li></li>
     </ul>
    </div>
   </div>
   <ul id="num"></ul>
   <div class="swi btnL">
    <</div>
     <div class="swi btnR">></div>
   </div>
   <script type="text/javascript" src="../jquery/jquery-3.1.0.js"></script>
   <script>
    $(document).ready(function() {
     var num = $('#bigContenter>div').length;
     for(var i = 1; i <= num; i++) {
      var li = '<li>' + i + '</li>';
      $('#num').append(li);
     }
     var s = 0;
     $('#num li:first-child').addClass('act')

     function moveR() {
      s++;
      if(s == num) {
       s = 0;
       $('#num li').eq(s).addClass('act').siblings().removeClass('act');
       $('#bigContenter').animate({
        left: '0'
       }, 600, 'swing')
      } else {
       $('#num li').eq(s).addClass('act').siblings().removeClass('act');
       $('#bigContenter').animate({
        left: '-=400px'
       }, 600, 'swing')
      }

     }

     function moveL() {
      s--;
      var sun=num*400-400;
      if(s == -1) {
       s = num - 1;
       $('#num li').eq(s).addClass('act').siblings().removeClass('act');
       for(var i=0;i<num-1;i++)
       {$('#bigContenter').animate({
         left: '-=400px'
        }, 600/(num-1), 'swing')}
      } else {
       $('#num li').eq(s).addClass('act').siblings().removeClass('act');
       $('#bigContenter').animate({
        left: '+=400px'
       }, 600, 'swing')
      }
     }
     var t = setInterval(moveR, 3000);
     $('#showOuter').mouseover(function() {
      $('.swi').fadeIn(2000);

     })
     $('#showOuter').mouseleave(function() {
      $('.swi').fadeOut(2000)
     })
     $('#showOuter').hover(function(){
      clearInterval(t);
     },function(){
      t=setInterval(moveR, 3000);
     })
     $('.btnL').click(function(){
      moveL();
     })
     $('.btnR').click(function(){
      moveR();
     })

    })
   </script>
 </body>

</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好,以下是一个简单的jQuery横向手风琴图片展示插件的示例代码: HTML代码: ``` <div class="accordion"> <div class="accordion-item"> <img src="image1.jpg"> <div class="accordion-item-title">Image 1</div> </div> <div class="accordion-item"> <img src="image2.jpg"> <div class="accordion-item-title">Image 2</div> </div> <div class="accordion-item"> <img src="image3.jpg"> <div class="accordion-item-title">Image 3</div> </div> </div> ``` CSS代码: ``` .accordion { display: flex; overflow-x: scroll; scroll-snap-type: x mandatory; scroll-behavior: smooth; } .accordion-item { width: 100vw; min-width: 300px; height: 100%; scroll-snap-align: start; position: relative; } .accordion-item img { width: 100%; height: 100%; object-fit: cover; } .accordion-item-title { position: absolute; bottom: 0; left: 0; width: 100%; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 1.5rem; padding: 0.5rem; } ``` jQuery代码: ``` $(document).ready(function() { $('.accordion').on('scroll', function() { // 当前选中的手风琴项 var $activeItem = null; // 手风琴项的宽度 var itemWidth = $('.accordion-item').outerWidth(); // 计算当前选中的手风琴项 $('.accordion-item').each(function() { var itemLeft = $(this).offset().left - $('.accordion').offset().left; if (itemLeft <= $(window).width() / 2 && itemLeft + itemWidth > $(window).width() / 2) { $activeItem = $(this); return false; } }); // 更新手风琴项的样式 $('.accordion-item').removeClass('active'); $activeItem.addClass('active'); }); }); ``` 这个插件使用CSS的scroll-snap-type和scroll-snap-align属性来实现手风琴效果,使用jQuery监听滚动事件并计算当前选中的手风琴项,最后更新手风琴项的样式来实现手风琴效果。你可以根据具体需求对CSS和jQuery代码进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值