JS實(shí)現(xiàn)滑動(dòng)條案例
本文實(shí)例為大家分享了JS實(shí)現(xiàn)滑動(dòng)條效果的具體代碼,供大家參考,具體內(nèi)容如下
在完成這個(gè)案例之前需要看一下這個(gè)博客:JS案例-添加緩動(dòng)畫(huà)
這個(gè)案例會(huì)用到上面博客的緩動(dòng)畫(huà)函數(shù)。實(shí)現(xiàn)效果如下:
完整代碼如下:
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>滑動(dòng)條案例</title> ? ? <style> ? ? ? ? body { ? ? ? ? ? ? overflow-x: hidden; ? ? ? ? } ? ? ? ?? ? ? ? ? .slidebar { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 500px; ? ? ? ? ? ? right: 0; ? ? ? ? ? ? width: 40px; ? ? ? ? ? ? height: 40px; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? line-height: 40px; ? ? ? ? ? ? cursor: pointer; ? ? ? ? ? ? color: #fff; ? ? ? ? ? ? background-color: #891383; ? ? ? ? } ? ? ? ?? ? ? ? ? .con { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? left: 0; ? ? ? ? ? ? top: 0; ? ? ? ? ? ? width: 200px; ? ? ? ? ? ? height: 40px; ? ? ? ? ? ? background-color: #891383; ? ? ? ? ? ? color: #fff; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? line-height: 40px; ? ? ? ? ? ? z-index: -1; ? ? ? ? } ? ? </style> ? ? <script src="../js/index.js"></script> </head> ? <body> ? ? <div class="slidebar"> ? ? ? ? <span>?</span> ? ? ? ? <div class="con">問(wèn)題反饋</div> ? ? </div> ? ? <script> ? ? ? ? // 獲取元素 ? ? ? ? var slide = document.querySelector('.slidebar') ? ? ? ? var span = document.querySelector('span'); ? ? ? ? var con = document.querySelector('.con'); ? ? ? ? slide.addEventListener('mouseenter', function() { ? ? ? ? ? ? // 當(dāng)動(dòng)畫(huà)執(zhí)行完畢就把左箭頭改為右箭頭 ? ? ? ? ? ? moves(con, -160, function() { ? ? ? ? ? ? ? ? slide.children[0].innerHTML = '?' ? ? ? ? ? ? }) ? ? ? ? }); ? ? ? ? slide.addEventListener('mouseleave', function() { ? ? ? ? ? ? // 當(dāng)動(dòng)畫(huà)執(zhí)行完畢就把右箭頭又變?yōu)樽蠹^ ? ? ? ? ? ? moves(con, 0, function() { ? ? ? ? ? ? ? ? slide.children[0].innerHTML = '?' ? ? ? ? ? ? }) ? ? ? ? }) ? ? </script> </body> ? </html>
JS代碼:
function moves(obj, target, callback) { ? ? clearInterval(obj.timer); ? ? obj.timer = setInterval(function() { ? ? ? ? // 把步長(zhǎng)值改為整數(shù),不要出現(xiàn)小數(shù)的情況 往上取整 ? ? ? ? var step = (target - obj.offsetLeft) / 10; ? ? ? ? step = step > 0 ? Math.ceil(step) : Math.floor(step); ? ? ? ? // 回調(diào)函數(shù)寫(xiě)在清除定時(shí)器里面 這里只能寫(xiě)等于 ? ? ? ? if (obj.offsetLeft == target) { ? ? ? ? ? ? clearInterval(obj.timer); ? ? ? ? ? ? if (callback) { ? ? ? ? ? ? ? ? callback(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? obj.style.left = obj.offsetLeft + step + 'px'; ? ? ? ? // console.log(obj.style.left); ? ? ? }, 15); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)元素實(shí)例代碼
這篇文章主要介紹了JavaScript 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)元素實(shí)例代碼,需要的朋友可以參考下2014-02-02three.js中正交與透視投影相機(jī)的實(shí)戰(zhàn)應(yīng)用指南
在three.js中攝像機(jī)的作用就是不斷的拍攝我們創(chuàng)建好的場(chǎng)景,然后通過(guò)渲染器渲染到屏幕中,下面這篇文章主要給大家介紹了關(guān)于three.js中正交與透視投影相機(jī)應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-08-08JavaScript實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07原生js實(shí)現(xiàn)jquery函數(shù)animate()動(dòng)畫(huà)效果的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇原生js實(shí)現(xiàn)jquery函數(shù)animate()動(dòng)畫(huà)效果的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08JS彈出對(duì)話框?qū)崿F(xiàn)方法(三種方式)
這篇文章主要介紹了JS彈出對(duì)話框?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式分析了三種方式,包括alert、confirm及prompt,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()
這篇文章主要介紹了JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()的相關(guān)資料,需要的朋友可以參考下2016-08-08JavaScript與Java正則表達(dá)式寫(xiě)法的區(qū)別介紹
這篇文章主要介紹了JavaScript與Java正則表達(dá)式寫(xiě)法的區(qū)別介紹,需要的朋友可以參考下2017-08-08