新聞中心
知識(shí)點(diǎn)
連山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
輪播圖思想:
① 建立一個(gè)全局變量索引,始終標(biāo)記當(dāng)前顯示圖片。
② 根據(jù)當(dāng)前圖片的數(shù)量,動(dòng)態(tài)創(chuàng)建下方的●圖片指示器。
③ 輪播圖的初始狀態(tài)為第一張圖片在中間,剩余所有圖片均放在即將顯示圖片位置。
④ 當(dāng)點(diǎn)擊>的時(shí)候,當(dāng)前圖片調(diào)用動(dòng)畫移動(dòng)函數(shù)進(jìn)行左移,與此同時(shí)新的一張圖片調(diào)用動(dòng)畫函數(shù)移入到div中,而會(huì)將下一張展示的圖片移動(dòng)到div右側(cè)。
⑤ 需要進(jìn)行邊界判斷,如果當(dāng)前的圖片大于圖片數(shù)量或者小于等于0,重新給索引賦值。
⑥ 當(dāng)點(diǎn)擊圖片指示器的時(shí)候,首先判定點(diǎn)擊的與索引的位置關(guān)系,然后進(jìn)行動(dòng)畫移動(dòng)。
⑦ 給div添加定時(shí)器,自動(dòng)移動(dòng)圖片。當(dāng)鼠標(biāo)進(jìn)入div,刪除定時(shí)器,當(dāng)鼠標(biāo)移出div,設(shè)置定時(shí)器。
?
運(yùn)行效果
自動(dòng)輪播
點(diǎn)擊左右切換圖片
點(diǎn)擊下方圖片指示器切換圖片
代碼
Html
????
????
????
Css
*{margin: 0;padding: 0;}
a{
????color: #999;
????text-decoration: none;
????position: absolute;
????top: 50%;
????transform: translateY(-50%);
????background-color: rgba(0, 0, 0, .4);
}
a:hover{
????color: #f8b62b;
}
i{
????font-size: 50px;
????font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#box{
????height: 482px;
????width: 830px;
????background-color: red;
????position: absolute;
????left: 50%;
????top: 50%;
????transform: translate(-50%,-50%);
????overflow: hidden;
}
#box_content{
????height: 100%;
????width: 100%;
????cursor: pointer;
}
#box_content img{
????position: absolute;
????vertical-align: top;
????height: 100%;
????width: 100%;
????/*left: 830px;*/
}
.box_img{
????width: 100%;
????height: 100%;
????position: absolute;}
.box_control_right{
????position: absolute;
????right: 0;
}
.box_control_left{
????position: absolute;
????left: 0;
}
ul{
????position: absolute;
????bottom: 30px;
????left: 50%;
????transform: translateX(-50%);
????display: flex;
????justify-content:space-evenly;
}
ul>li{
????list-style: none;
????width: 16px;
????height: 16px;
????background-color: #fff;
????margin: 0 3px;
????border-radius: 50%;
????cursor: pointer;
}
ul>li.current{
????background-color: darkorange;
}
Js
window.addEventListener('load',function (ev) {
????// 輪播圖
????(function () {
????????// 1. 獲取需要標(biāo)簽
????????var boxContent = myTool.$('box_content');
????????var contentImg = boxContent.children;
????????var boxControl = myTool.$('box_control');
????????var controlBottom = boxControl.children[2];
????????// 2. 全局索引
????????var iNow = 0;
????????// 3. 根據(jù)圖片個(gè)數(shù)動(dòng)態(tài)添加下方圖片指示器
????????for (var i = 0; i < contentImg.length; i++) {
????????????var li = document.createElement('li');
????????????controlBottom.insertBefore(li,controlBottom.children[0]);
????????}
????????// 4. 讓第一個(gè)圖片指示器選中
????????controlBottom.children[0].className = 'current';
????????// 5. 讓除了第一張圖片以外所有圖片進(jìn)入待顯示區(qū)域
????????var scrollImgWidth = boxContent.offsetWidth;
????????function(){ //黃金代碼?http://www.kaifx.cn/mt4/kaifx/1805.html
????????for (var j = 1; j < contentImg.length; j++) {
????????????contentImg[j].style.left = scrollImgWidth + 'px';
????????}
????????// 6. 處理左右兩側(cè)點(diǎn)擊
????????var cPrev = boxControl.children[0];
????????var cNext = boxControl.children[1];
????????// 6.1 點(diǎn)擊左邊
????????cPrev.addEventListener('click',function (evt) {
????????????// 1. 當(dāng)前可視區(qū)域圖片快速右移
????????????// 2. 上一張幻燈片出現(xiàn)在可視區(qū)域左側(cè)
????????????// 3. 讓這張幻燈片做動(dòng)畫進(jìn)入
????????????myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null);
????????????iNow--;
????????????// 邊界處理
????????????if (iNow < 0){
????????????????iNow = contentImg.length - 1;
????????????}
????????????contentImg[iNow].style.left = -scrollImgWidth + 'px';
????????????myTool.slowMoving(contentImg[iNow],{'left':0},null);
????????????// 切換索引
????????????changeIndex();
?
????????},false);
????????// 6.2 點(diǎn)擊右邊
????????cNext.addEventListener('click',function (evt) {
????????????autoPlay();
????????},false);
????????// 7. 下側(cè)圖片指示器操作
????????for (var k = 0; k < controlBottom.children.length; k++) {
????????????// 取出單個(gè)li標(biāo)簽
????????????var bottomLi = controlBottom.children[k];
????????????// 監(jiān)聽鼠標(biāo)進(jìn)入
????????????(function (index) {
????????????????bottomLi.addEventListener('mouseover',function (evt) {
????????????????????// 比較當(dāng)前索引和點(diǎn)擊指示器位置關(guān)系
????????????????????if (index > iNow){
????????????????????????myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null);
????????????????????????contentImg[index].style.left = scrollImgWidth + 'px';
????????????????????}else if(index < iNow){
????????????????????????myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null);
????????????????????????contentImg[index].style.left = -scrollImgWidth + 'px';
????????????????????}
????????????????????iNow = index;
????????????????????myTool.slowMoving(contentImg[iNow],{'left':0});
????????????????????// 切換索引
????????????????????changeIndex();
????????????????},false);
????????????})(k)
????????}
?
????????/**
?????????* 切換索引操作
?????????*/
????????function changeIndex() {
????????????for (var i = 0; i < controlBottom.children.length; i++) {
????????????????controlBottom.children[i].className = '';
????????????}
????????????// 當(dāng)前的被選中
????????????controlBottom.children[iNow].className = 'current';
????????}
?
????????/**
?????????* 點(diǎn)擊右側(cè)和圖片自動(dòng)運(yùn)動(dòng)操作
?????????*/
????????function autoPlay(){
????????????// 1. 當(dāng)前可視區(qū)域圖片快速左移
????????????// 2. 下一張圖片出現(xiàn)在可視區(qū)域右側(cè)
????????????// 3. 讓這張圖片做動(dòng)畫進(jìn)入
????????????myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null);
????????????iNow++;
????????????// 邊界處理
????????????if (iNow >= contentImg.length) {
????????????????iNow = 0;
????????????}
????????????contentImg[iNow].style.left = scrollImgWidth + 'px';
????????????myTool.slowMoving(contentImg[iNow], {"left": 0},null);
????????????// 切換索引
????????????changeIndex();
????????}
?
????????// 8. 設(shè)置定時(shí)器
????????var timerId = setInterval(autoPlay,2000);
????????// 9. 鼠標(biāo)進(jìn)入圖片div后設(shè)置和清除定時(shí)器
????????myTool.$('box').addEventListener('mouseover',function () {
????????????clearInterval(timerId);
????????});
????????myTool.$('box').addEventListener('mouseout',function () {
????????????timerId = setInterval(autoPlay,2000);
????????});
?
????})();
},false);
?
?
分享標(biāo)題:JS特效輪播圖
文章起源:http://fisionsoft.com.cn/article/jgosip.html