jquery向上轮播

jquery向上轮播


html代码:

<div class="box">
 <ul>
  <li>1111111111111111111111111111</li>
  <li>2222222222222222222222222222</li>
  <li>3333333333333333333333333333</li>
  <li>4444444444444444444444444444</li>
  <li>5555555555555555555555555555</li>
  <li>6666666666666666666666666666</li>
  <li>7777777777777777777777777777</li>
  <li>8888888888888888888888888888</li>
  <li>9999999999999999999999999999</li>
 </ul>

</div>


css代码:

*{margin:0;padding:0;font-size:12px;}
li{list-style:none;}
.box{margin:20px;width:320px;height:195px;border:1px solid #ddd;padding:5px 5px 10px;overflow:hidden;}

.box ul li{line-height:25px;}


js代码:

/*
* textSlider 0.1
* Dependence jquery-1.7.1.js
*/
;(function($){
 $.fn.textSlider = function(options){
   var defaults = { //初始化参数
      scrollHeight:25,
      line:1,
      speed:'normal',
      timer:2000
   };
   var opts = $.extend(defaults,options);
   this.each(function(){
     var timerID;
     var obj = $(this);
     var $ul = obj.children("ul");
     var $height = $ul.find("li").height();
     var $Upheight = 0-opts.line*$height;
     obj.hover(function(){
       clearInterval(timerID);
     },function(){
       timerID = setInterval(moveUp,opts.timer);
       });
     function moveUp(){
       $ul.animate({"margin-top":$Upheight},opts.speed,function(){
          for(i=0;i<opts.line;i++){ //只有for循环了才可以设置一次滚动的行数
           $ul.find("li:first").appendTo($ul);
          }
         $ul.css("margin-top",0);
       });
     };
     timerID = setInterval(moveUp,opts.timer);
     });
   };

})(jQuery)


$(function(){
  $(".box").textSlider({
    line:2
    });
  })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的jQuery图片轮播的例子,其中使用了时间阀来控制图片的滚动速度,并且添加了左右按钮来控制图片的滚动方向和速度: ```html <!DOCTYPE html> <html> <head> <title>jQuery图片轮播</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> #slider { width: 600px; height: 400px; margin: 0 auto; position: relative; overflow: hidden; } #slider img { width: 600px; height: 400px; position: absolute; top: 0; left: 0; display: none; } #slider img:first-child { display: block; } #slider .prev, #slider .next { position: absolute; top: 50%; margin-top: -20px; width: 40px; height: 40px; background-color: #fff; border-radius: 50%; text-align: center; line-height: 40px; font-size: 24px; color: #333; cursor: pointer; opacity: 0.5; transition: opacity 0.3s ease-in-out; } #slider .prev:hover, #slider .next:hover { opacity: 1; } #slider .prev { left: 20px; } #slider .next { right: 20px; } </style> </head> <body> <div id="slider"> <img src="img/1.jpg" alt="图片1"> <img src="img/2.jpg" alt="图片2"> <img src="img/3.jpg" alt="图片3"> <img src="img/4.jpg" alt="图片4"> <img src="img/5.jpg" alt="图片5"> <a href="javascript:;" class="prev"><</a> <a href="javascript:;" class="next">></a> </div> <script type="text/javascript"> $(function() { var $slider = $('#slider'); var $img = $slider.find('img'); var len = $img.length; var index = 0; var interval = 3000; // 时间阀,控制图片滚动速度 var timer; // 左右按钮点击事件 $slider.on('click', '.prev', function() { index--; if (index < 0) { index = len - 1; } showImg(index); }).on('click', '.next', function() { index++; if (index >= len) { index = 0; } showImg(index); }); // 显示图片 function showImg(index) { $img.eq(index).fadeIn().siblings().fadeOut(); } // 自动轮播 function autoPlay() { timer = setInterval(function() { index++; if (index >= len) { index = 0; } showImg(index); }, interval); } autoPlay(); // 鼠标移入停止轮播,移出继续轮播 $slider.hover(function() { clearInterval(timer); }, function() { autoPlay(); }); }); </script> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值