本文实例讲述了JS实现的页面自定义滚动条效果。分享给大家供大家参考,具体如下:
这里演示网页上用的滚动条效果,是一个自定义的滚动条代码,除了上下两个箭头以外,滚动条和一般的浏览器基本差不多,鼠标滚轮滚动,滚动条滚动。html结构很简单,mainBox是外层div,content是内容,滚动条div是js动态生成的。
运行效果截图如下:
在线演示地址如下:
http://demo.jb51.net/js/2015/js-web-zdy-scroll-style-codes/
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>滚动条</title> <style type="text/css"> *{ margin:0; padding:0;} p{ height:1000px;} #mainBox{ width:400px; height:500px; border:1px #bbb solid; position:relative; overflow:hidden; margin:50px auto;} #content{ height:2500px; position:absolute; left:0; top:0; background:url(http://files.jb51.net/file_images/article/201510/20151026113716032.jpg) } .scrollDiv{ width:18px; position:absolute; top:0; background:#666; border-radius:10px;} </style> </head> <body> <div id="mainBox"> <div id="content"></div> </div> <p></p> <script type="text/javascript"> var doc=document; var _wheelData=-1; var mainBox=doc.getElementById('mainBox'); function bind(obj,type,handler){ var node=typeof obj=="string"?$(obj):obj; if(node.addEventListener){ node.addEventListener(type,handler,false); }else if(node.attachEvent){ node.attachEvent('on'+type,handler); }else{ node['on'+type]=handler; } } function mouseWheel(obj,handler){ var node=typeof obj=="string"?$(obj):obj; bind(node,'mousewheel',function(event){ var data=-getWheelData(event); handler(data); if(document.all){ window.event.returnValue=false; }else{ event.preventDefault(); } }); //火狐 bind(node,'DOMMouseScroll',function(event){ var data=getWheelData(event); handler(data); event.preventDefault(); }); function getWheelData(event){ var e=event||window.event; return e.wheelDelta?e.wheelDelta:e.detail*40; } } function addScroll(){ this.init.apply(this,arguments); } addScroll.prototype={ init:function(mainBox,contentBox,className){ var mainBox=doc.getElementById(mainBox); var contentBox=doc.getElementById(contentBox); var scrollDiv=this._createScroll(mainBox,className); this._resizeScorll(scrollDiv,mainBox,contentBox); this._tragScroll(scrollDiv,mainBox,contentBox); this._wheelChange(scrollDiv,mainBox,contentBox); this._clickScroll(scrollDiv,mainBox,contentBox); }, //创建滚动条 _createScroll:function(mainBox,className){ var _scrollBox=doc.createElement('div') var _scroll=doc.createElement('div'); var span=doc.createElement('span'); _scrollBox.appendChild(_scroll); _scroll.appendChild(span); _scroll.className=className; mainBox.appendChild(_scrollBox); return _scroll; }, //调整滚动条 _resizeScorll:function(element,mainBox,contentBox){ var p=element.parentNode; var conHeight=contentBox.offsetHeight; var _width=mainBox.clientWidth; var _height=mainBox.clientHeight; var _scrollWidth=element.offsetWidth; var _left=_width-_scrollWidth; p.style.width=_scrollWidth+"px"; p.style.height=_height+"px"; p.style.left=_left+"px"; p.style.position="absolute"; p.style.background="#ccc"; contentBox.style.width=(mainBox.offsetWidth-_scrollWidth)+"px"; var _scrollHeight=parseInt(_height*(_height/conHeight)); if(_scrollHeight>=mainBox.clientHeight){ element.parentNode.style.display="none"; } element.style.height=_scrollHeight+"px"; }, //拖动滚动条 _tragScroll:function(element,mainBox,contentBox){ var mainHeight=mainBox.clientHeight; element.onmousedown=function(event){ var _this=this; var _scrollTop=element.offsetTop; var e=event||window.event; var top=e.clientY; //this.onmousemove=scrollGo; document.onmousemove=scrollGo; document.onmouseup=function(event){ this.onmousemove=null; } function scrollGo(event){ var e=event||window.event; var _top=e.clientY; var _t=_top-top+_scrollTop; if(_t>(mainHeight-element.offsetHeight)){ _t=mainHeight-element.offsetHeight; } if(_t<=0){ _t=0; } element.style.top=_t+"px"; contentBox.style.top=-_t*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"; _wheelData=_t; } } element.onmouseover=function(){ this.style.background="#444"; } element.onmouseout=function(){ this.style.background="#666"; } }, //鼠标滚轮滚动,滚动条滚动 _wheelChange:function(element,mainBox,contentBox){ var node=typeof mainBox=="string"?$(mainBox):mainBox; var flag=0,rate=0,wheelFlag=0; if(node){ mouseWheel(node,function(data){ wheelFlag+=data; if(_wheelData>=0){ flag=_wheelData; element.style.top=flag+"px"; wheelFlag=_wheelData*12; _wheelData=-1; }else{ flag=wheelFlag/12; } if(flag<=0){ flag=0; wheelFlag=0; } if(flag>=(mainBox.offsetHeight-element.offsetHeight)){ flag=(mainBox.clientHeight-element.offsetHeight); wheelFlag=(mainBox.clientHeight-element.offsetHeight)*12; } element.style.top=flag+"px"; contentBox.style.top=-flag*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"; }); } }, _clickScroll:function(element,mainBox,contentBox){ var p=element.parentNode; p.onclick=function(event){ var e=event||window.event; var t=e.target||e.srcElement; var sTop=document.documentElement.scrollTop>0?document.documentElement.scrollTop:document.body.scrollTop; var top=mainBox.offsetTop; var _top=e.clientY+sTop-top-element.offsetHeight/2; if(_top<=0){ _top=0; } if(_top>=(mainBox.clientHeight-element.offsetHeight)){ _top=mainBox.clientHeight-element.offsetHeight; } if(t!=element){ element.style.top=_top+"px"; contentBox.style.top=-_top*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"; _wheelData=_top; } } } } new addScroll('mainBox','content','scrollDiv'); </script> </body> </html>
希望本文所述对大家JavaScript程序设计有所帮助。

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中