您好,登錄后才能下訂單哦!
這篇文章主要介紹js如何實現水平和豎直滑動條,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
首先來看水平滑動條,效果圖如下:
代碼如下:
<html> <head> <meta charset="UTF-8"> <title>水平滑動條</title> <style> * { margin: 0; padding: 0; } .scroll { margin: 100px; width: 500px; height: 5px; background: #ccc; position: relative; } .bar { width: 10px; height: 20px; background: #369; position: absolute; top: -7px; left: 0; cursor: pointer; } p{ margin-left: 100px; } </style> </head> <body> <div class="scroll" id="scroll"> <div class="bar" id="bar"> </div> </div> <p></p> <script> //獲取元素 var scroll = document.getElementById('scroll'); var bar = document.getElementById('bar'); var ptxt = document.getElementsByTagName('p')[0]; bar.onmousedown = function(event) { var event = event || window.event; //頁面事件的X減去當前相對于最近的祖先定位元素 var x = event.clientX - this.offsetLeft; document.onmousemove = function(event) { var event = event || window.event; var left = event.clientX - x; if (left < 0) left = 0; else if (left > scroll.offsetWidth - bar.offsetWidth) { left = scroll.offsetWidth - bar.offsetWidth; } //改變滑塊的left bar.style.left = left + "px"; ptxt.innerHTML = "當前滑塊的移動的百分比:" + parseInt(left / (scroll.offsetWidth - bar.offsetWidth) * 100) + "%"; //防止選擇內容 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); } } //當鼠標彈起的時候,不做任何操作 document.onmouseup = function() { document.onmousemove = null; } </script> </body> </html>
豎直滑動條效果圖如下:
代碼如下:
<html> <head> <meta charset="UTF-8"> <title>豎直滑動條</title> <style> * { margin: 0; padding: 0; } .scroll{ margin: 100px; width: 5px; height: 320px; background: #ccc; position: relative; } .bar { width: 15px; height: 5px; background: #369; position: absolute; top: 0px; left: -5; cursor: pointer; } p{ margin-left: 100px; } </style> </head> <body> <div class="scroll" id="scroll"> <div class="bar" id="bar"> </div> </div> <p></p> <script> //獲取元素 var scroll = document.getElementById("scroll"); var bar = document.getElementById("bar"); var ptxt = document.getElementsByTagName('p')[0]; //添加事件 bar.onmousedown = function(event) { var event = event || window.event; //頁面事件的Y減去當前相對于最近的祖先定位元素 var y = event.clientY - this.offsetTop; // 拖動需要寫到down里面 document.onmousemove = function(event) { var event = event || window.event; //獲取移動的距離 var top = event.clientY - y; if (top < 0){ top = 0; } else if (top > scroll.offsetHeight - bar.offsetHeight){ top = scroll.offsetHeight - bar.offsetHeight; } //改變滑塊的top bar.style.top = top + "px"; //按照百分比得到當前滑動的距離 ptxt.innerHTML = "當前滑塊的移動的百分比:" + parseInt(top/(scroll.offsetHeight - bar.offsetHeight) * 100) + "%"; //防止選擇內容 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); } } //當鼠標彈起的時候,不做任何操作 document.onmouseup = function() { document.onmousemove = null; } </script> </body> </html>
這里之所以加入移動百分比的展示效果,主要是考慮到后續如果需要對接后臺的數據就可以達到動態控制的目的。
以上是“js如何實現水平和豎直滑動條”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。