资料
表格
制度
合同
管理
职场
经营
创业
范文
总结
计划
作文
文章
美文
词句
教程
模板
图片
字体
图标
办公
人事
财务
生产
管理资源吧首页
>>>
教程
>>>
编程
>>>
JavaScript教程
>>>
图片与JavaScript配合做出个性滚动条
<!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>图片与JavaScript配合做出个性滚动条</title> <style type="text/css"> * { margin:0; padding:0; } body { margin:20px 0 400px 20px; font:12px Arial; } h1 { font-size:14px; } ol { margin:20px; line-height:160%; } #out { position:relative; width:500px; height:300px; margin:100px 0 100px 80px; border:1px solid #369; overflow:hidden; } #content { width:520px; height:100%; overflow:auto; -moz-user-select:none; } #scrollBar,#scrollBarHandle,.scroll_bar_top,.scroll_bar_top_a,.scroll_bar_bottom,.scroll_bar_bottom_a { background-image:url(http://files.glzy8.com/demoimg/200910/un_bg_scrollBar.gif); } #scrollBar { position:absolute; top:0; right:0; z-index:9; width:14px; height:100%; background-repeat:repeat-y; background-position:-42px 0; float:left; } #scrollBarTop,#scrollBarHandle,#scrollBarBottom { position:absolute; left:0; cursor:default; } #scrollBarTop,#scrollBarBottom { width:100%; height:14px; color:#fff; text-align:center; } #scrollBarTop { top:0; } #scrollBarBottom { bottom:0; } #scrollBarHandle { width:100%; background-repeat:repeat-y; background-position:-28px 0; } .scroll_bar_top { background-position:0 0; } .scroll_bar_top_a { background-position:-14px 0; } .scroll_bar_bottom { background-position:-56px 0; } .scroll_bar_bottom_a { background-position:-70px 0; } </style> </head> <body> <div id="out"> fsdfs<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br>11111111<br>fsdfs<br> </div> <div id="scrollBar"> <div id="scrollBarTop" class="scroll_bar_top"></div> <div id="scrollBarHandle"></div> <div id="scrollBarBottom" class="scroll_bar_bottom"></div> </div> </div> <img src="/_New/_cj/myeditor/sysimage/space.gif" class="ewebeditor__script" _ewebeditor_fake_tag="script" _ewebeditor_fake_value="%3Cscript%3E%20%0D%0Avar%20ximen%20%3D%20%7B%20%0D%0A%24%3Afunction(o)%7B%20return%20document.getElementById(o)%3B%20%7D%2C%20%0D%0AgetStyle%3Afunction(o)%20%7B%20return%20o.currentStyle%7C%7Cdocument.defaultView.getComputedStyle(o%2Cnull)%3B%20%7D%2C%20%0D%0AgetOffset%3Afunction(o)%20%7B%20%0D%0Avar%20t%20%3D%20o.offsetTop%2Ch%20%3D%20o.offsetHeight%3B%20%0D%0Awhile(o%20%3D%20o.offsetParent)%20%7B%20t%20%2B%3D%20o.offsetTop%3B%20%7D%20%0D%0Areturn%20%7B%20top%3At%2C%20height%3Ah%20%7D%3B%20%0D%0A%7D%2C%20%0D%0Abind%3Afunction(o%2CeType%2Cfn)%20%7B%20%0D%0Aif(o.addEventListener)%20%7B%20o.addEventListener(eType%2Cfn%2Cfalse)%3B%20%7D%20%0D%0Aelse%20if(o.attachEvent)%20%7B%20o.attachEvent(%22on%22%20%2B%20eType%2Cfn)%3B%20%7D%20%0D%0Aelse%20%7B%20o%5B%22on%22%20%2B%20eType%5D%20%3D%20fn%3B%20%7D%20%0D%0A%7D%2C%20%0D%0Aunbind%3Afunction(o%2CeType%2Cfn)%20%7B%20%0D%0Aif(o.removeEventListener)%20%7B%20o.removeEventListener(eType%2Cfn%2Cfalse)%3B%20%7D%20%0D%0Aelse%20if(o.detachEvent)%20%7B%20o.detachEvent(%22on%22%20%2B%20eType%2Cfn)%3B%20%7D%20%0D%0Aelse%20%7B%20o%5B%22on%22%20%2B%20eType%5D%20%3D%20fn%3B%20%7D%20%0D%0A%7D%2C%20%0D%0AstopPropagate%3Afunction(e)%20%7B%20%0D%0Aif(e%20%26%26%20e.stopPropagation)%20%7B%20e.stopPropagation()%3B%20%7D%20%0D%0Aelse%20%7B%20window.event.cancelBubble%20%3D%20true%3B%20%7D%20%0D%0Areturn%20false%3B%20%0D%0A%7D%2C%20%0D%0AstopDefault%3Afunction(e)%20%7B%20%0D%0Ae%20%3D%20e%20%7C%7C%20window.event%3B%20%0D%0Ae.stopPropagation%20%26%26%20(e.preventDefault()%2Ce.stopPropagation())%20%7C%7C%20(e.cancelBubble%20%3D%20true%2Ce.returnValue%20%3D%20false)%3B%20%0D%0A%7D%20%0D%0A%7D%3B%20%0D%0A(function()%7B%20%0D%0Avar%20myScrollDown%2CmyScrollUp%2CscrollBarMouseDown%2CscrollBarMouseUp%2CrelY%2C%20%0D%0Aout%20%3D%20ximen.%24(%22out%22)%2C%20%0D%0Acontent%20%3D%20ximen.%24(%22content%22)%2C%20%0D%0AscrollBar%20%3D%20ximen.%24(%22scrollBar%22)%2C%20%0D%0AscrollBarTop%20%3D%20ximen.%24(%22scrollBarTop%22)%2C%20%0D%0AscrollBarHandle%20%3D%20ximen.%24(%22scrollBarHandle%22)%2C%20%0D%0AscrollBarBottom%20%3D%20ximen.%24(%22scrollBarBottom%22)%2C%20%0D%0AscrollBarUpHeight%20%3D%20parseInt(ximen.getStyle(scrollBarTop).height)%2C%20%0D%0AscrollBarBottomHeight%20%3D%20parseInt(ximen.getStyle(scrollBarBottom).height)%2C%20%0D%0AcontentScrollHeight%20%3D%20content.scrollHeight%2C%2F%2F%E5%B0%86content.scrollHeight%E8%B5%8B%E4%B8%80%E6%AC%A1%E5%80%BC%2C%E8%A7%A3%E5%86%B3IE6%E4%B8%8BscrollHeight%E9%9C%80%E8%B0%83%E7%94%A8%E4%B8%A4%E6%AC%A1%E7%9A%84bug%20%0D%0AscrollBarHandleHeight%20%3D%20parseInt(content.offsetHeight%2Fcontent.scrollHeight%20*%20(scrollBar.offsetHeight%20-%20scrollBarUpHeight%20-%20scrollBarBottomHeight))%2C%20%0D%0AsetScrollBarHandle%20%3D%20function()%20%7B%2F%2F%E5%BD%93%E5%86%85%E5%AE%B9%E8%B6%85%E5%A4%9A%E6%97%B6%E8%AE%BE%E7%BD%AE%E6%8B%96%E6%8B%BD%E6%9D%A1%E5%AD%90%E7%9A%84%E6%9C%80%E5%B0%8F%E9%AB%98%E5%BA%A6%20%0D%0AscrollBarHandle.style.top%20%3D%20scrollBarUpHeight%20%2B%20%22px%22%3B%20%0D%0Aif(scrollBarHandleHeight%20%3E%2015)%20%7B%20scrollBarHandle.style.height%20%3D%20scrollBarHandleHeight%20%2B%20%22px%22%3B%20%7D%20%0D%0Aelse%20%7B%20scrollBarHandleHeight%20%3D%2015%3B%20scrollBarHandle.style.height%20%3D%20%2215px%22%3B%20%7D%20%0D%0A%7D%2C%20%0D%0AclearAllInterval%20%3D%20function()%20%7B%20clearInterval(myScrollDown)%3B%20clearInterval(myScrollUp)%3B%20clearInterval(scrollBarMouseDown)%3B%20%7D%2C%20%0D%0AforMousemove%20%3D%20function(e)%20%7B%20%0D%0Avar%20e%20%3D%20e%20%7C%7C%20window.event%3B%20%0D%0Acontent.scrollTop%20%3D%20(e.clientY%20-%20relY%20-%20scrollBarUpHeight)%2F(scrollBar.offsetHeight%20-%20scrollBarHandleHeight%20-%20scrollBarUpHeight%20-%20scrollBarBottomHeight)*(content.scrollHeight%20-%20content.offsetHeight)%3B%20%0D%0A%7D%2C%20%0D%0AforMouseDown%20%3D%20function(event)%7B%20%0D%0Avar%20et%20%3D%20event.target%20%7C%7C%20event.srcElement%3B%20%0D%0ArelY%20%3D%20event.clientY%20-%20et.offsetTop%3B%20%0D%0Aximen.bind(document%2C%22mousemove%22%2CforMousemove)%3B%20%0D%0A%7D%2C%20%0D%0AscrollDir%20%3D%20function(e)%20%7B%20%0D%0Avar%20e%20%3D%20e%20%7C%7C%20window.event%2CeDir%3B%20%2F%2F%E8%AE%BE%E7%BD%AE%E6%BB%9A%E8%BD%AE%E4%BA%8B%E4%BB%B6%2Ce.wheelDelta%E4%B8%8Ee.detail%E5%88%86%E5%88%AB%E5%85%BC%E5%AE%B9IE%E3%80%81W3C%EF%BC%8C%E6%A0%B9%E6%8D%AE%E8%BF%94%E5%9B%9E%E5%80%BC%E7%9A%84%E6%AD%A3%E8%B4%9F%E6%9D%A5%E5%88%A4%E6%96%AD%E6%BB%9A%E5%8A%A8%E6%96%B9%E5%90%91%20%0D%0Aif(e.wheelDelta)%20%7B%20eDir%20%3D%20e.wheelDelta%2F120%3B%20%7D%20%0D%0Aelse%20if(e.detail)%20%7B%20eDir%20%3D%20-e.detail%2F3%3B%20%7D%20%0D%0Aif(eDir%20%3E%200)%20%7B%20content.scrollTop%20-%3D%2080%3B%20%7D%20%2F%2F%E6%AD%A5%E9%95%BF%E8%AE%BE80%E5%83%8F%E7%B4%A0a%E6%AF%94%E8%BE%83%E6%8E%A5%E8%BF%91window%E6%BB%9A%E5%8A%A8%E6%9D%A1%E7%9A%84%E6%BB%9A%E5%8A%A8%E9%80%9F%E5%BA%A6%20%0D%0Aelse%20%7B%20content.scrollTop%20%2B%3D%2080%3B%20%7D%20%0D%0Aximen.stopDefault(e)%3B%20%0D%0A%7D%2C%20%0D%0AscrollBarClick%20%3D%20function(e)%20%7B%20%0D%0Avar%20e%20%3D%20e%20%7C%7C%20window.event%2C%20%0D%0AmStep%20%3D%20scrollBar.offsetHeight%2C%20%0D%0AdocumentScrollTop%20%3D%20document.documentElement.scrollTop%2C%20%0D%0AhOffset%20%3D%20ximen.getOffset(scrollBarHandle)%3B%20%0D%0Aif(documentScrollTop%20%2B%20e.clientY%20%3C%20hOffset.top)%20%7B%20scrollBarMouseDown%20%3D%20setInterval(function()%7B%20content.scrollTop%20-%3D%2015%3B%20%7D%2C10)%3B%20%7D%20%0D%0Aelse%20if(documentScrollTop%20%2B%20e.clientY%20%3E%20hOffset.top%20%2B%20hOffset.height)%20%7B%20scrollBarMouseDown%20%3D%20setInterval(function()%7B%20content.scrollTop%20%2B%3D%2015%3B%20%7D%2C10)%3B%20%7D%20%0D%0A%7D%3B%20%0D%0AsetScrollBarHandle()%3B%20%0D%0Aximen.bind(content%2C%22scroll%22%2Cfunction()%7B%20%0D%0AscrollBarHandle.style.top%20%3D%20content.scrollTop%2F(content.scrollHeight%20-%20content.offsetHeight)%20*%20(scrollBar.offsetHeight%20-%20scrollBarHandleHeight%20-%20scrollBarUpHeight%20-%20scrollBarBottomHeight)%20%2B%20scrollBarUpHeight%20%2B%20%22px%22%3B%20%0D%0A%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mousedown%22%2Cfunction()%7B%20myScrollDown%20%3D%20setInterval(function()%7B%20content.scrollTop%20%2B%3D%2015%3B%20%7D%2C10)%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mousedown%22%2Cfunction()%7B%20myScrollUp%20%3D%20setInterval(function()%7B%20content.scrollTop%20-%3D%2015%3B%20%7D%2C10)%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mouseup%22%2CclearAllInterval)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mouseout%22%2CclearAllInterval)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mouseup%22%2CclearAllInterval)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mouseout%22%2CclearAllInterval)%3B%20%0D%0Aximen.bind(scrollBarHandle%2C%22mousedown%22%2CforMouseDown)%3B%20%0D%0Aximen.bind(document%2C%22mouseup%22%2Cfunction()%7B%20%0D%0Aximen.unbind(document%2C%22mousemove%22%2CforMousemove)%3B%20%0D%0Aximen.unbind(scrollBarHandle%2C%22mousemove%22%2CforMousemove)%3B%20%0D%0A%7D)%3B%20%0D%0Aximen.bind(out%2C%22selectstart%22%2Cfunction()%7B%20return%20false%3B%20%7D)%3B%20%0D%0Aximen.bind(out%2C%22select%22%2Cfunction()%7B%20document.selection.empty()%3B%20%7D)%3B%20%0D%0Aximen.bind(out%2C%22mousewheel%22%2CscrollDir)%3B%20%0D%0Aximen.bind(out%2C%22DOMMouseScroll%22%2CscrollDir)%3B%20%0D%0Aximen.bind(scrollBar%2C%22mousedown%22%2CscrollBarClick)%3B%20%0D%0Aximen.bind(scrollBar%2C%22mouseup%22%2CclearAllInterval)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22click%22%2Cfunction(event)%7B%20ximen.stopPropagate(event)%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mousedown%22%2Cfunction()%7B%20scrollBarTop.className%20%3D%20%22scroll_bar_top_a%22%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mouseup%22%2Cfunction()%7B%20scrollBarTop.className%20%3D%20%22scroll_bar_top%22%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarTop%2C%22mouseout%22%2Cfunction()%7B%20scrollBarTop.className%20%3D%20%22scroll_bar_top%22%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22click%22%2Cfunction(event)%7B%20ximen.stopPropagate(event)%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mousedown%22%2Cfunction()%7B%20scrollBarBottom.className%20%3D%20%22scroll_bar_bottom_a%22%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mouseup%22%2Cfunction()%7B%20scrollBarBottom.className%20%3D%20%22scroll_bar_bottom%22%3B%20%7D)%3B%20%0D%0Aximen.bind(scrollBarBottom%2C%22mouseout%22%2Cfunction()%7B%20scrollBarBottom.className%20%3D%20%22scroll_bar_bottom%22%3B%20%7D)%3B%20%0D%0A%7D)()%3B%20%0D%0A%3C%2Fscript%3E"> </body> </html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行
]
教程首页
更多教程