其實我們一樣可以很個性。。^_^ 效果實現:(附效果圖) 各位大神有什麼好的意見和指導敬請賜教~~ ...
其實我們一樣可以很個性。。^_^
效果實現:(附效果圖)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <style> body, html { margin: 0; width: 100%; height: 100%; } ul, li { margin: 0; padding: 0; list-style: none; } .scrollBox { width: 100%; height: 100%; overflow: hidden; padding-right: 15px; box-sizing: border-box; } .cBox { width: 100%; /*float: left;*/ } .sBox { width: 15px; height: 100%; /*float: right;*/ position: absolute; top: 0; right: 0; background-image: linear-gradient(rgba(188,57,75,0.8) 0%,rgba(38,70,173,0.5) 100%); box-shadow: 0 0 15px 3px rgba(0,0,0,0.5) inset; } li { width: 100%; } img { display: block; width: 100%; } .tool { width: 100%; border-radius:7px; position: relative; left: 0px; top: 0px; text-align: center; /*background-color: skyblue;*/ background-image: linear-gradient(skyblue 0%,pink 100%); box-shadow: 0 0 20px 2px #FFFF99 inset; cursor: pointer; } </style> <script type="text/javascript"> window.onload = function() { //獲取觸發滾動事件的元素 var scrollBox = document.querySelector('.scrollBox'); //創建滾動結構: //-------------- //獲取和子元素 var cBox = scrollBox.querySelector('.cBox'); var sBox = scrollBox.querySelector('.sBox'); var tool = scrollBox.querySelector('.tool'); initWheel(); //滾動條初始化 function initWheel() { //計算滾動條高度 var H1 = cBox.offsetHeight; var H2 = scrollBox.clientHeight; var h1 = sBox.offsetHeight; //求出h2的高度 var h2 = H2 / H1 * h1; // console.log(h2) tool.style.height = h2 + 'px'; tool.style.lineHeight = h2 + 'px'; } var site = tool.offsetTop; var speed = 10; wheel({ obj: scrollBox, up: function() { site -= speed; //邊界處理 if(site <= 0) { site = 0; } tool.style.top = site + 'px'; cBox.style.marginTop = -(cBox.offsetHeight * (site / sBox.clientHeight)) + 'px'; }, down: function() { site += speed; //邊界處理 if(site >= sBox.clientHeight - tool.offsetHeight) { site = sBox.clientHeight - tool.offsetHeight; } tool.style.top = site + 'px'; cBox.style.marginTop = -(cBox.offsetHeight * (site / sBox.clientHeight)) + 'px'; } }); //拖動滾動條 tool.onmousedown = function(ev){ var ev=ev||event; ev.preventDefault(); var x1 = ev.clientX; var y1 =ev.clientY; document.onmousemove = function(ev){ var ev=ev||event; ev.preventDefault(); var x2 = ev.clientX; var y2 =ev.clientY; var x = Math.abs(x2-x1); var y = y2-y1; //邊界處理 var m =site+y; if(m<=0){ m=0; }; if(m>=sBox.clientHeight-tool.offsetHeight){ m=sBox.clientHeight-tool.offsetHeight; }; tool.style.top=m+'px'; cBox.style.marginTop = -cBox.offsetHeight*(m/sBox.clientHeight)+'px'; if(x>200){//x超過滾動條200時取消事件 site = tool.offseetTop;//這裡記錄變化後的位置 document.onmousemove = null; } } document.onmouseup = function(){ site = tool.offsetTop;//記錄變化後的位置 document.onmousemove = null; } } /* * * //滾輪事件方法: * 功能:綁定滾輪事件 * 參數: * option { * * obj: 觸發事件的元素 * up: 滾輪向上滾動要執行的函數 * down: 滾輪向下滾動要執行的函數 * * } * * */ function wheel(option) { //自定義錯誤提示 if(!option) { console.log('錯誤碼10086:參數不正確'); return false; } var opt = { obj: option.obj || documen, up: option.up || null, down: option.down || null, }; opt.obj.onmousewheel = function(ev) { var ev = ev || event; if(ev.wheelDelta < 0) { console.log('向下') opt.down && opt.down(); } if(ev.wheelDelta > 0) { console.log('向上') opt.up && opt.up(); } } opt.obj.addEventListener('DOMMouseScroll', function(ev) { var ev = ev || event; // console.log(ev.detail); if(ev.detail > 0) { // console.log('向下'); opt.down && opt.down(); } if(ev.detail < 0) { // console.log('向上'); opt.up && opt.up(); } }); } } </script> </head> <body> <section class="scrollBox"> <div class="cBox"> <ul> <li> <img src="img/1.jpg" alt="" /> </li> <li> <img src="img/2.jpg" alt="" /> </li> <li> <img src="img/3.jpg" alt="" /> </li> <li> <img src="img/4.jpg" alt="" /> </li> <li> <img src="img/5.jpg" alt="" /> </li> <li> <img src="img/6.jpg" alt="" /> </li> <li> <img src="img/7.jpg" alt="" /> </li> <li> <img src="img/8.jpg" alt="" /> </li> <li> <img src="img/9.jpg" alt="" /> </li> </ul> </div> <div class="sBox"> <span class="tool glyphicon glyphicon-flash" href="javascript:;"></span> </div> </section> </body> </html>
各位大神有什麼好的意見和指導敬請賜教~~