原生JS利用transform實(shí)現(xiàn)banner的無(wú)限滾動(dòng)示例代碼
功能

- 默認(rèn)情況無(wú)限循環(huán)向右移動(dòng)
- 點(diǎn)擊數(shù)字切換到對(duì)應(yīng)圖片
- 點(diǎn)擊左右切換可切換圖片
原理
首先說(shuō)下原理。
- 在布局上所有的圖片都是重疊的,即只要保證Y方向?qū)R即可,當(dāng)前可見(jiàn)的圖z-index層級(jí)最高。
- 每隔3s中更換一張圖片,使用setTimeout定時(shí)。
- 使用gIndex記錄當(dāng)前可視區(qū)域的展示的是哪張圖片下標(biāo),每次更換,計(jì)算下一張圖片的下標(biāo)。
- 通過(guò)requestAnimationFrame實(shí)現(xiàn)一次圖片切換的動(dòng)畫。
這種方法也可以做到整個(gè)頁(yè)面始終只有2個(gè)img標(biāo)簽,而不必把所有的img節(jié)點(diǎn)全部創(chuàng)建出來(lái),要點(diǎn)是每次更換不可見(jiàn)img的src。

動(dòng)畫的實(shí)現(xiàn)
- 首先定義一個(gè)timestap,這個(gè)值記錄每個(gè)幀移動(dòng)多少距離。定義初始step=0,記錄移動(dòng)的步數(shù)。
- 每次移動(dòng)的距離moveWidth是timestamp*step,圖片1向右移動(dòng)增加moveWidth,圖片2從左側(cè)進(jìn)入moveWidth。因此,圖片1的transform是translate(moveWidth), 而圖片2的transform則是translate(moveWidth-圖片寬度)。
- step+1
- 如果moveWidth>圖片寬度,步驟5,否則requestAnimationFrame請(qǐng)求下一次執(zhí)行,繼續(xù)2-4.
- 圖片1和2都將位置放置在起始位置,圖片2的z-index設(shè)置為最高。
這樣就完成了一次移動(dòng)的動(dòng)畫。
html代碼
<header>
<div class="box">
<img src="imgs/banner1.jpg">
<img src="imgs/banner2.jpg">
<img src="imgs/banner3.jpg">
<img src="imgs/banner4.jpg">
</div>
<div class="buttons">
<div class="active">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div class="left">
<div class="arrow"></div>
</div>
<div class="right">
<div class="arrow"></div>
</div>
</header>
JS代碼
var timeout = null;
window.onload = function () {
var oLeft = document.querySelector('.left');
var oRight = document.querySelector('.right');
var oButton = document.querySelector('.buttons');
var oButtons = document.querySelectorAll('.buttons div');
var oImgs = document.querySelectorAll('.box img');
var imgWidth = oImgs[0].width;
var gIndex = 0;
begainAnimate();
// 綁定左右點(diǎn)擊事件
oLeft.onclick = function () {
clearTimeout(timeout);
leftMove();
begainAnimate();
};
oRight.onclick = function () {
clearTimeout(timeout);
rightMove();
begainAnimate();
};
// 綁定數(shù)字序號(hào)事件
oButton.onclick = function (event) {
clearTimeout(timeout);
var targetEl = event.target;
var nextIndex = (+targetEl.innerText) - 1;
console.log(nextIndex);
rightMove(nextIndex);
begainAnimate();
}
// 默認(rèn)初始動(dòng)畫朝右邊
function begainAnimate() {
clearTimeout(timeout);
timeout = setTimeout(function () {
rightMove();
begainAnimate();
}, 3000);
}
// 向左移動(dòng)動(dòng)畫
function leftMove() {
var nextIndex = (gIndex - 1 < 0) ? oImgs.length - 1 : gIndex - 1;
animateSteps(nextIndex, -50);
}
// 向右移動(dòng)動(dòng)畫
function rightMove(nextIndex) {
if (nextIndex == undefined) {
nextIndex = (gIndex + 1 >= oImgs.length) ? 0 : gIndex + 1;
}
animateSteps(nextIndex, 50);
}
// 一次動(dòng)畫
function animateSteps(nextIndex, timestamp) {
var currentImg = oImgs[gIndex];
var nextImg = oImgs[nextIndex];
nextImg.style.zIndex = 10;
var step = 0;
requestAnimationFrame(goStep);
// 走一幀的動(dòng)畫,移動(dòng)timestamp
function goStep() {
var moveWidth = timestamp * step++;
if (Math.abs(moveWidth) < imgWidth) {
currentImg.style.transform = `translate(${moveWidth}px)`;
nextImg.style.transform = `translate(${moveWidth > 0 ? (moveWidth - imgWidth) : (imgWidth + moveWidth)}px)`;
requestAnimationFrame(goStep);
} else {
currentImg.style.zIndex = 1;
currentImg.style.transform = `translate(0px)`;
nextImg.style.transform = `translate(0px)`;
oButtons[gIndex].setAttribute('class', '');
oButtons[nextIndex].setAttribute('class', 'active');
gIndex = nextIndex;
}
}
}
}
window.onclose = function () {
clearTimeout(timeout);
}
css布局樣式
<style>
/* 首先設(shè)置圖片box的區(qū)域,將圖片重疊在一起 */
header {
width: 100%;
position: relative;
overflow: hidden;
}
.box {
width: 100%;
height: 300px;
}
.box img {
width: 100%;
height: 100%;
position: absolute;
transform: translateX(0);
z-index: 1;
}
.box img:first-child {
z-index: 10;
}
/* 數(shù)字序列按鈕 */
.buttons {
position: absolute;
right: 10%;
bottom: 5%;
display: flex;
z-index: 100;
}
.buttons div {
width: 30px;
height: 30px;
background-color: #aaa;
border: 1px solid #aaa;
text-align: center;
margin: 10px;
cursor: pointer;
opacity: .7;
border-radius: 15px;
line-height: 30px;
}
.buttons div.active {
background-color: white;
}
/* 左右切換按鈕 */
.left,
.right {
position: absolute;
width: 80px;
height: 80px;
background-color: #ccc;
z-index: 100;
top: 110px;
border-radius: 40px;
opacity: .5;
cursor: pointer;
}
.left {
left: 2%;
}
.right {
right: 2%;
}
.left .arrow {
width: 30px;
height: 30px;
border-left: solid 5px #666;
border-top: solid 5px #666;
transform: translate(-5px, 25px) rotate(-45deg) translate(25px, 25px);
}
.right .arrow {
width: 30px;
height: 30px;
border-left: solid 5px #666;
border-top: solid 5px #666;
transform: translate(50px, 25px) rotate(135deg) translate(25px, 25px);
}
</style>
到此這篇關(guān)于原生JS利用transform實(shí)現(xiàn)banner的無(wú)限滾動(dòng)示例代碼的文章就介紹到這了,更多相關(guān)JS banner無(wú)限滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用javascript實(shí)現(xiàn)全部刪或清空所選的操作
這篇文章主要介紹了利用javascript實(shí)現(xiàn)全部刪或清空所選的操作,需要的朋友可以參考下2014-05-05
微信小程序云開(kāi)發(fā)(數(shù)據(jù)庫(kù))詳解
使用云開(kāi)發(fā)開(kāi)發(fā)微信小程序、小游戲,無(wú)需搭建服務(wù)器,這篇文章主要為大家詳細(xì)介紹了微信小程序云開(kāi)發(fā)數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
JavaScript中三個(gè)等號(hào)和兩個(gè)等號(hào)的區(qū)別(== 和 ===)淺析
javascript中比較運(yùn)算符'=='與'==='可能大家用的比較多,但是大家對(duì)他的區(qū)別不是很清楚,接下來(lái)小編給大家介紹下js中三個(gè)等號(hào)和兩個(gè)等號(hào)的區(qū)別(== 和 ===),感興趣的朋友可以參考下2016-09-09
layui異步加載table表中某一列數(shù)據(jù)的例子
今天小編就為大家分享一篇layui異步加載table表中某一列數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JavaScript+CSS實(shí)現(xiàn)仿Mootools豎排彈性動(dòng)畫菜單效果
這篇文章主要介紹了JavaScript+CSS實(shí)現(xiàn)仿Mootools豎排彈性動(dòng)畫菜單效果,可實(shí)現(xiàn)鼠標(biāo)滑過(guò)菜單項(xiàng)呈現(xiàn)彈性移動(dòng)顯示的效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
layuimini框架實(shí)現(xiàn)點(diǎn)擊菜單欄回到起始頁(yè)的解決方案
這篇文章主要介紹了layuimini框架實(shí)現(xiàn)點(diǎn)擊菜單欄回到起始頁(yè)的解決方案,本文通過(guò)圖文示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06

