這篇文章主要介紹“JavaScript怎么實現輪播圖”,在日常操作中,相信很多人在JavaScript怎么實現輪播圖問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JavaScript怎么實現輪播圖”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
html 和 css 代碼如下所示(文件名:index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="animate.js"></script>
<script src="輪播圖.js"></script>
<style>
body {
background-color: rgb(151, 147, 147);
}
* {
margin: 0;
padding: 0;
}
div {
overflow: hidden;
position: relative;
width: 500px;
height: 500px;
background-color: skyblue;
margin: 20px auto;
}
ul {
list-style: none;
}
.img {
width: 600%;
position: absolute;
left: 0;
}
li {
float: left;
}
img {
width: 500px;
height: 500px;
}
.yuan>li {
cursor: pointer;
width: 10px;
height: 10px;
background-color: rgb(190, 185, 185);
border-radius: 50%;
margin: 0 5px;
border: 1px solid rgb(119, 114, 114);
}
.yuan {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.yuan .color {
background-color: rgb(228, 135, 135);
}
a {
text-decoration: none;
color: black;
background-color: rgb(112, 111, 111);
display: inline-block;
width: 30px;
height: 30px;
text-align: center;
line-height: 30px;
position: absolute;
top: 50%;
transform: translateY(-50%);
display: none;
}
.left {
left: 0;
}
.right {
right: 0;
}
</style>
</head>
<body>
<div class="box">
<ul class="img">
<li><img src="images/1.webp" alt=""></li>
<li><img src="images/2.jpg" alt=""></li>
<li><img src="images/3.webp" alt=""></li>
<li><img src="images/4.webp" alt=""></li>
<li><img src="images/5.webp" alt=""></li>
</ul>
<a href="JavaScript:;" rel="external nofollow" rel="external nofollow" class="left"><</a>
<a href="JavaScript:;" rel="external nofollow" rel="external nofollow" class="right">></a>
<ul class="yuan"></ul>
</div>
</body>
</html>這樣寫好以后,一個基本的樣子就算是有了。
接下來就是讓他動起來,想想什么可以讓圖片動起來,是不是我們學過的動畫效果呀,所有這個地方要用緩動動畫來實現一個切換圖片的效果,因為 js 代碼較多,所以得新建一個 js 文件,把代碼封裝起來!
下面就是封裝得 js 代碼 (文件名:輪播圖.js)
window.addEventListener('load', function () {
var img = document.querySelector('.img');
var yuan = document.querySelector('.yuan');
var box = document.querySelector('.box');
var left = document.querySelector('.left');
var right = document.querySelector('.right');
var imgwidth = box.offsetWidth; //獲取盒子的寬度(圖片的寬度)
// 經過鼠標觸發 停止自動滾動圖片效果
box.addEventListener('mouseenter', function () {
left.style.display = 'block';
right.style.display = 'block';
clearInterval(timer);
})
// 離開鼠標觸發 自動滾動圖片再次觸發
box.addEventListener('mouseleave', function () {
left.style.display = 'none';
right.style.display = 'none';
timer = setInterval(function () {
right.click();
}, 2000)
})
// 根據圖片添加下面的小圓點
for (var i = 0; i < img.children.length; i++) {
var li = document.createElement('li');
yuan.appendChild(li);
li.setAttribute('date-index', i);
li.addEventListener('click', function () {
for (var j = 0; j < yuan.children.length; j++) {
yuan.children[j].className = '';
}
this.className = 'color';
var index = this.getAttribute('date-index');
var imgwidth = box.offsetWidth;
// animate(obj,target,function(){})
animate(img, -index * imgwidth);
nums = index;
colors = index;
})
}
// 默認第一個小圓點有顏色
yuan.children[0].className = 'color';
var nums = 0;
var colors = 0;
// 復制第一張圖片到最后,給無縫滾動做準備
var li = img.children[0].cloneNode(true);
img.appendChild(li);
var flag = true;
//右邊按鈕,切換下一張,小圓點一起變化
right.addEventListener('click', function () {
if (flag) {
flag = false;
if (nums == img.children.length - 1) {
nums = 0;
img.style.left = 0;
}
nums++;
animate(img, -nums * imgwidth, function () {
flag = true;
});
colors++;
if (colors == yuan.children.length) {
colors = 0;
}
for (var j = 0; j < yuan.children.length; j++) {
yuan.children[j].className = '';
}
yuan.children[colors].className = 'color';
}
})
// 左邊按鈕,切換下一張,小圓點一起變化
left.addEventListener('click', function () {
if (flag) {
flag = false;
if (nums == 0) {
nums = img.children.length - 1;
img.style.left = -nums * imgwidth + 'px';
}
nums--;
colors--;
animate(img, -nums * imgwidth, function () {
flag = true;
});
if (colors < 0) {
colors = yuan.children.length - 1;
}
// if (colors == 0) {
// colors = yuan.children.length;
// }
// colors--;
for (var j = 0; j < yuan.children.length; j++) {
yuan.children[j].className = '';
}
yuan.children[colors].className = 'color';
}
})
// 鼠標不經過圖片實現自動滾動
var timer = setInterval(function () {
right.click();
}, 2000)
})關鍵的來了,要讓動起來怎么少得了動畫效果呢,我單獨封裝了一個 js 文件,這樣以后寫其他案例的時候也可以直接引用了。
代碼如下(文件名:animate.js)
function animate(obj, target, callback) { //移動的對象(誰移動),移動的目的位置,回調函數
// 先清除以前的定時器,只保留當前的一個定時器執行
clearInterval(obj.timer);
obj.timer = setInterval(function () {
// 步長寫到定時器里面
var step = (target - obj.offsetLeft) / 10; //步長公式:(目標位置-現在的位置)/10
step = step > 0 ? Math.ceil(step) : Math.floor(step); //步長改為整數,不要出現小數,正數向上取整,負數向下取整
if (obj.offsetLeft == target) {
clearInterval(obj.timer) //停止動畫,本質是停止定時器
if (callback) {
callback(); // 回調函數寫到定時器結束里面
}
}
//步長作為一個慢慢變小的值才能實現從快到慢的緩動停止的效果
obj.style.left = obj.offsetLeft + step + 'px';
},15)
}到此,關于“JavaScript怎么實現輪播圖”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。