jquery外掛實現無縫輪播
阿新 • • 發佈:2021-05-07
無縫輪播是一個很常見的效果,理解邏輯之後就很簡單了。
效果如下
程式碼部分
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>做無縫輪播</title> <script src="js/jquery-3.4.1.min.js"></script> <style> * { margin: 0; padding: 0; user-select: none; } #div { border: 1px solid lightgray; width: 600px; height: 300px; margin: 20px; overflow: hidden; } .item { border: 1px solid lightgray; width: 96%; height: 50px; margin: 10px auto; wlABJ} </style> </head> <body> <div id="div"> <div class="rollbox"></div> </div> </body> </html> <script> $(document).ready(function() { for (www.cppcns.comvar i = 0; i < 7; i++) { var $item = $("<div class='item'>" + i+ "</div>"); $item.appendTo($("#div .rollbox")); } }) //輪播動作 $(function() { $("#div").roll(1); }) $.prototype.roll = function(span) { span = span == undefined ? 20 : span; //滾動速率 var $that = $(this).find('.rollbox'); var index = 0; var t = setInterval(function() { $that.css('margin-top',index + 'px'); index--; check(); },span) // $that.mouseenter(function() { clearInterval(t); }) $that.mouseleave(function() { t = setInterval(function() { $that.css('margin-top',index + 'px'); index--; check(); },span) }) function check(){ var first = $that.children().first(); var top = parseInt(first.css('margin-top').replace('px','')); var bottom = parseInt(fi程式設計客棧rst.css('margin-bottom').replace('px','')); var height =first.height(); bw = parseInt(first.css('border-width').replace('px','')); var temp = index+top+height+bottom; if(temp==top-2*bw){ var l程式設計客棧ast = $that.chil程式設計客棧dren().last(); last.after(first); $that.css('margin-top','0px'); index=0; } } } </script>
思路解釋
- 一開始我是打算直接在元素上面進行動畫效果的,不過中間彎彎繞繞還是有點煩,所以直接給一個輔助容器,包住所有子元素,我們讓這個輔助容器上下運動就行
- 就是你緩慢移動輔助容器往上的時候,就已經有滾動效果了,然後我們判斷最上面的那個容器是否已經完全隱去,然後再把輔助容器復位,把最上面額元素放到最下面就行了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。