jquery插件實現(xiàn)無縫輪播
更新時間:2021年05月07日 07:58:25 作者:阿飛超努力
這篇文章主要為大家詳細介紹了jquery插件說下無縫輪播,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
無縫輪播是一個很常見的效果,理解邏輯之后就很簡單了。
效果如下

代碼部分
<!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;
}
</style>
</head>
<body>
<div id="div">
<div class="rollbox"></div>
</div>
</body>
</html>
<script>
$(document).ready(function() {
for (var 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(first.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 last = $that.children().last();
last.after(first);
$that.css('margin-top','0px');
index=0;
}
}
}
</script>
思路解釋
- 一開始我是打算直接在元素上面進行動畫效果的,不過中間彎彎繞繞還是有點煩,所以直接給一個輔助容器,包住所有子元素,我們讓這個輔助容器上下運動就行
- 就是你緩慢移動輔助容器往上的時候,就已經(jīng)有滾動效果了,然后我們判斷最上面的那個容器是否已經(jīng)完全隱去,然后再把輔助容器復(fù)位,把最上面額元素放到最下面就行了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery實現(xiàn)ctrl+enter(回車)提交表單
本文章來給大家介紹在我們輸入完內(nèi)容之后直接按Ctrl+Enter提交表單實現(xiàn)程序,此方法一般是用于textarea中哦,其它的input這類的就不需了。2015-10-10
jQuery插件imgPreviewQs實現(xiàn)上傳圖片預(yù)覽
這篇文章主要介紹了jQuery插件imgPreviewQs實現(xiàn)上傳圖片預(yù)覽的相關(guān)資料,需要的朋友可以參考下2016-01-01
jQuery中animate()的使用方法及解決$(”body“).animate({“scrollTop”:top})
這篇文章主要介紹了關(guān)于jQuery中animate()的使用方法及解決$("body").animate({"scrollTop":top})不被Firefox支持的問題,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04

