JavaScript 實(shí)現(xiàn)輪播圖特效的示例
效果展示
1.頁(yè)面截圖
2.相關(guān)效果
html 頁(yè)面
從微信讀書(shū)上找了幾張書(shū)籍封面來(lái)做輪播的圖片。
index.html
<body> <div id="container"> <div class="big_pic_div"> <div class="prev"></div> <div class="next"></div> <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="mark_left"></a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="mark_right"></a> <div class="big_pic" style="z-index: 1;"><img src="img/1.jpg" alt=""></div> <div class="big_pic"><img src="img/2.jpg" alt=""></div> <div class="big_pic"><img src="img/3.jpg" alt=""></div> <div class="big_pic"><img src="img/4.jpg" alt=""></div> <div class="big_pic"><img src="img/5.jpg" alt=""></div> <div class="big_pic"><img src="img/6.jpg" alt=""></div> </div> <div class="small_pic_div"> <div class="small_pic" style="filter: opacity(100); opacity: 1;"><img src="img/1.jpg" alt=""></div> <div class="small_pic"><img src="img/2.jpg" alt=""></div> <div class="small_pic"><img src="img/3.jpg" alt=""></div> <div class="small_pic"><img src="img/4.jpg" alt=""></div> <div class="small_pic"><img src="img/5.jpg" alt=""></div> <div class="small_pic"><img src="img/6.jpg" alt=""></div> </div> </div> </body>
css 樣式
grid 布局的 gap 不兼容 IE,惹不起。
style.css
body { margin: 0; padding: 0; background: skyblue; } #container { position: relative; overflow: hidden; width: 350px; height: 390px; margin: 50px auto 0; padding: 0 15px; background: goldenrod; box-shadow: 2px 1px 5px 1px #666; } .mark_left { position: absolute; left: 0; z-index: 3000; width: 65px; height: 360px; } .mark_right { position: absolute; right: 0; z-index: 3000; width: 65px; height: 360px; } .prev { position: absolute; top: 150px; left: 5px; z-index: 3001; width: 60px; height: 60px; background: url(img/btn.gif) olivedrab; /* transform: translateY(50%); */ /* alpha 兼容IE8及以下的IE瀏覽器 */ filter: alpha(opacity=0); opacity: 0; } .next { position: absolute; top: 120px; right: 5px; z-index: 3001; width: 60px; height: 60px; background: url(img/btn.gif) olivedrab; background-position-y: 60px; transform: translateY(50%); filter: alpha(opacity=0); opacity: 0; } .big_pic_div { position: relative; width: 250px; height: 360px; padding: 15px 0; } .big_pic { position: absolute; /* height 從 0 到 360px 下滑 */ overflow: hidden; height: 360px; box-shadow: 1px 1px 2px #777; } .small_pic_div { display: grid; grid-template: repeat(6, 110px) / 80px; gap: 15px; position: absolute; top: 0; left: 273px; padding: 15px 0; } .small_pic { height: 110px; filter: alpha(opacity = 60); opacity: 0.6; } .small_pic img { width: 80px; height: 100%; }
JavaScript 實(shí)現(xiàn)
多物體運(yùn)動(dòng)框架
move.js
// 獲取樣式 function getStyle(obj, name) { if (obj.currentStyle) { // IE... return obj.currentStyle[name]; } else { // Chrome... return getComputedStyle(obj, false)[name]; } } function startMove(obj, attr, target) { clearInterval(obj.timer); obj.timer = setInterval(function () { var cur = 0; // 透明度 if (attr == 'opacity') { cur = Math.round(parseFloat(getStyle(obj, 'opacity')) * 100); } else { cur = parseInt(getStyle(obj, attr)); } // 緩沖運(yùn)動(dòng),速度和距離成正比 var speed = 0; speed = (target - cur) / 6; // 1px 是最小的,1.9px 會(huì)被當(dāng)做 1px;得把速度取整,不然并未真正到達(dá)目標(biāo)值 target speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (cur == target) { clearInterval(obj.timer); } else { // 透明度沒(méi)有單位,單獨(dú)考慮 if (attr == 'opacity') { obj.style.filter = 'alpha(opacity = ' + (cur + speed) + ')'; obj.style.opacity = (cur + speed) / 100; } else { obj.style[attr] = cur + speed + 'px'; } } }, 30); }
輪播圖功能實(shí)現(xiàn)
window.onload = function () { var markLeft = document.getElementsByClassName('mark_left')[0]; var markRight = document.getElementsByClassName('mark_right')[0]; var btnPrev = document.getElementsByClassName('prev')[0]; var btnNext = document.getElementsByClassName('next')[0]; var smallPicDiv = document.getElementsByClassName('small_pic_div')[0]; var smallPic = document.getElementsByClassName('small_pic'); var bigPic = document.getElementsByClassName('big_pic'); var nowZIndex = 2; var now = 0; var container = document.getElementById('container'); // 左右按鈕透明度設(shè)置 btnPrev.onmouseover = markLeft.onmouseover = function () { startMove(btnPrev, 'opacity', 100); }; btnPrev.onmouseout = markLeft.onmouseout = function () { startMove(btnPrev, 'opacity', 0); }; btnNext.onmouseover = markRight.onmouseover = function () { startMove(btnNext, 'opacity', 100); }; btnNext.onmouseout = markRight.onmouseout = function () { startMove(btnNext, 'opacity', 0); }; // 點(diǎn)擊小圖時(shí),大圖自動(dòng)切換 for (var i = 0; i < smallPic.length; i++) { smallPic[i].index = i; smallPic[i].onclick = function () { if (now == this.index) return; // 使用 now 來(lái)表示當(dāng)前選擇的小圖,當(dāng)前選中的小圖再次點(diǎn)擊時(shí)不會(huì)讓大圖下滑 now = this.index; bigPic[this.index].style.zIndex = nowZIndex++; bigPic[this.index].style.height = 0; startMove(bigPic[this.index], 'height', 360); // 點(diǎn)擊后其他小圖變透明,當(dāng)前選中的為不透明 for (var i = 0; i < smallPic.length; i++) { startMove(smallPic[i], 'opacity', 60); } startMove(smallPic[this.index], 'opacity', 100); }; // 鼠標(biāo)移動(dòng)到小圖上時(shí),淡入淡出 smallPic[i].onmouseover = function () { startMove(this, 'opacity', 100); }; smallPic[i].onmouseout = function () { if (now != this.index) { startMove(this, 'opacity', 60); } }; } // tab 函數(shù):當(dāng)前選中圖片不透明;圖片下滑;小圖區(qū)域的滾動(dòng) function tab() { bigPic[now].style.zIndex = nowZIndex++; for (var i = 0; i < smallPic.length; i++) { startMove(smallPic[i], 'opacity', 60); } startMove(smallPic[now], 'opacity', 100); bigPic[now].style.height = 0; startMove(bigPic[now], 'height', 360); if (now == 0) { startMove(smallPicDiv, 'top', 0); } else if (now == smallPic.length - 1) { startMove(smallPicDiv, 'top', -(now - 2) * (smallPic[0].offsetHeight + 15)); } else { startMove(smallPicDiv, 'top', -(now - 1) * (smallPic[0].offsetHeight + 15)); } } // 左右按鈕點(diǎn)擊 btnPrev.onclick = function () { now--; if (now == smallPic.length) { now = smallPic.length - 1; } else if (now < 0) { now = smallPic.length - 1; // return; } tab(); }; btnNext.onclick = function () { now++; if (now == smallPic.length) { now = 0; } tab(); }; var timer = setInterval(btnNext.onclick, 3000); container.onmouseover = function () { clearInterval(timer); }; container.onmouseout = function () { timer = setInterval(btnNext.onclick, 3000); }; };
以上就是JavaScript 實(shí)現(xiàn)輪播圖特效的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 輪播圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 原生js實(shí)現(xiàn)簡(jiǎn)單輪播圖
- js輪播圖之旋轉(zhuǎn)木馬效果
- javascript實(shí)現(xiàn)點(diǎn)擊按鈕切換輪播圖功能
- js代碼編寫(xiě)無(wú)縫輪播圖
- 如何使用JavaScript實(shí)現(xiàn)無(wú)縫滾動(dòng)自動(dòng)播放輪播圖效果
- JS+css3實(shí)現(xiàn)幻燈片輪播圖
- js實(shí)現(xiàn)輪播圖效果 純js實(shí)現(xiàn)圖片自動(dòng)切換
- js實(shí)現(xiàn)無(wú)縫輪播圖插件封裝
- 原生JS實(shí)現(xiàn)無(wú)縫輪播圖片
- js實(shí)現(xiàn)輪播圖特效
- js實(shí)現(xiàn)無(wú)縫輪播圖特效
- js代碼實(shí)現(xiàn)輪播圖
相關(guān)文章
JS 驗(yàn)證密碼 不能為空,必須含有數(shù)字、字母、特殊字符,長(zhǎng)度在8-12位
這篇文章主要介紹了JS 驗(yàn)證密碼 不能為空,必須含有數(shù)字、字母、特殊字符,長(zhǎng)度在8-12位的相關(guān)資料,需要的朋友可以參考下2017-06-06JavaScript鍵盤(pán)事件超詳細(xì)總結(jié)
這篇文章主要給大家介紹了關(guān)于JavaScript鍵盤(pán)事件的相關(guān)資料,鍵盤(pán)事件是指在網(wǎng)頁(yè)中當(dāng)用戶按下鍵盤(pán)上的按鍵時(shí)所觸發(fā)的事件,在JavaScript中可以通過(guò)監(jiān)聽(tīng)鍵盤(pán)事件來(lái)實(shí)現(xiàn)一些交互效果,需要的朋友可以參考下2023-10-10如何寫(xiě)出一個(gè)驚艷面試官的JavaScript深拷貝
淺拷貝是面試中經(jīng)常會(huì)被問(wèn)到的問(wèn)題,這篇文章主要給大家介紹了關(guān)于如何寫(xiě)出一個(gè)驚艷面試官的JavaScript深拷貝的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05JavaScript鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)使用方法以及實(shí)例效果演示
最近工作中遇到了鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)的相關(guān)需求,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)使用方法以及實(shí)例效果演示的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05JavaScript實(shí)現(xiàn)前端實(shí)時(shí)搜索功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)前端實(shí)時(shí)搜索功能 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04簡(jiǎn)單了解JavaScript中的new?Function
這篇文章主要介紹了簡(jiǎn)單了解JavaScript中的new?Function,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09前端高頻面試題之JS中堆和棧的區(qū)別和瀏覽器的垃圾回收機(jī)制
本文給大家分享前端高頻面試題JS中堆和棧的區(qū)別和瀏覽器的垃圾回收機(jī)制,本文分文別類(lèi)給大家介紹了棧(stack)和堆(heap)的區(qū)別基本類(lèi)型和引用類(lèi)型的相關(guān)知識(shí),瀏覽器垃圾回收機(jī)制包括基本概念給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10