JS實(shí)現(xiàn)滑動條案例
本文實(shí)例為大家分享了JS實(shí)現(xiàn)滑動條效果的具體代碼,供大家參考,具體內(nèi)容如下
在完成這個案例之前需要看一下這個博客:JS案例-添加緩動畫
這個案例會用到上面博客的緩動畫函數(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>滑動條案例</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">問題反饋</div>
? ? </div>
? ? <script>
? ? ? ? // 獲取元素
? ? ? ? var slide = document.querySelector('.slidebar')
? ? ? ? var span = document.querySelector('span');
? ? ? ? var con = document.querySelector('.con');
? ? ? ? slide.addEventListener('mouseenter', function() {
? ? ? ? ? ? // 當(dāng)動畫執(zhí)行完畢就把左箭頭改為右箭頭
? ? ? ? ? ? moves(con, -160, function() {
? ? ? ? ? ? ? ? slide.children[0].innerHTML = '?'
? ? ? ? ? ? })
? ? ? ? });
? ? ? ? slide.addEventListener('mouseleave', function() {
? ? ? ? ? ? // 當(dāng)動畫執(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() {
? ? ? ? // 把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況 往上取整
? ? ? ? var step = (target - obj.offsetLeft) / 10;
? ? ? ? step = step > 0 ? Math.ceil(step) : Math.floor(step);
? ? ? ? // 回調(diào)函數(shù)寫在清除定時器里面 這里只能寫等于
? ? ? ? if (obj.offsetLeft == target) {
? ? ? ? ? ? clearInterval(obj.timer);
? ? ? ? ? ? if (callback) {
? ? ? ? ? ? ? ? callback();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? obj.style.left = obj.offsetLeft + step + 'px';
? ? ? ? // console.log(obj.style.left);
?
? ? }, 15);
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
拖動Html元素集合 Drag and Drop any item
拖動Html元素集合 Drag and Drop any item...2006-12-12
利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法
今天小編就為大家分享一篇利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
基于JavaScript實(shí)現(xiàn)簡單的音樂音譜圖效果
我們經(jīng)??吹皆诼牁芬舻臅r候,會有音譜圖隨著音樂的節(jié)奏不斷變化給人視覺上的享受,那么本文我們就來通過JavaScript來實(shí)現(xiàn)這一效果,感興趣的可以了解下2023-11-11
layui實(shí)現(xiàn)登陸界面驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了layui實(shí)現(xiàn)登陸界面驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
js獲取html的span標(biāo)簽的值方法(超簡單)
下面小編就為大家?guī)硪黄猨s獲取html的span標(biāo)簽的值方法(超簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07

