Javascript類比scroll滾動效果指令碼第1/2頁_javascript技巧

來源:互聯網
上載者:User
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> var oEventUtil = new Object(); oEventUtil.addEventHandler = function (oTarget,sType,fnDo){ if(oTarget.addEventListener){ oTarget.addEventListener(sType,fnDo,false) } if(oTarget.attachEvent){ oTarget.attachEvent("on"+sType,fnDo) } else{ oTarget["on"+sType] = fnDo; } } oEventUtil.removeEventHandler = function (oTarget,sType,fnDo){ if(oTarget.removeEventListener){ oTarget.removeEventListener(sType,fnDo,false) } if(oTarget.detachEvent){ oTarget.detachEvent("on"+sType,fnDo) } else{ oTarget["on"+sType] = null; } } oEventUtil.getEvent = function() { if (window.event) { return this.formatEvent(window.event); } else { return oEventUtil.getEvent.caller.arguments[0]; } } oEventUtil.formatEvent = function (oEvent) { oEvent.pageY = oEvent.clientY + document.body.scrollTop; return oEvent; } function areaScroll(){ var scroll_hand_top = parseInt(scrollup.offsetHeight); var scroll_hand_bottom = parseInt(category_list.offsetHeight) - parseInt(scrolldown.offsetHeight) - parseInt(scroll_hand.offsetHeight); category_list.scrollTop = (parseInt(category_list.scrollHeight)-parseInt(category_list.offsetHeight) + parseInt(scrollup.offsetHeight))*(parseInt(scroll_hand.style.top)-parseInt(scrollup.offsetHeight))/(parseInt(scroll_hand_bottom)-parseInt(scrollup.offsetHeight))-parseInt(scrollup.offsetHeight); } function clickNarrowup(){ var oEvent = oEventUtil.getEvent(); var scroll_hand_top = parseInt(scrollup.offsetHeight); var scroll_hand_bottom = parseInt(category_list.offsetHeight) - parseInt(scrolldown.offsetHeight) - parseInt(scroll_hand.offsetHeight); if(parseInt(scroll_hand.offsetTop) <= parseInt(scroll_hand_bottom) && parseInt(scroll_hand.offsetTop) >= parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(scroll_hand.offsetTop) - 10 +"px"; if(parseInt(scroll_hand.style.top) >= parseInt(scroll_hand_bottom)){ scroll_hand.style.top = parseInt(scroll_hand_bottom)+"px"; } if(parseInt(scroll_hand.style.top)<=parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(scroll_hand_top)+"px"; } areaScroll(); } } function clickNarrowdown(){ var oEvent = oEventUtil.getEvent(); var scroll_hand_top = parseInt(scrollup.offsetHeight); var scroll_hand_bottom = parseInt(category_list.offsetHeight) - parseInt(scrolldown.offsetHeight) - parseInt(scroll_hand.offsetHeight); if(parseInt(scroll_hand.offsetTop) <= parseInt(scroll_hand_bottom) && parseInt(scroll_hand.offsetTop) >= parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(scroll_hand.offsetTop) + 10 +"px"; if(parseInt(scroll_hand.style.top) > parseInt(scroll_hand_bottom)){ scroll_hand.style.top = parseInt(scroll_hand_bottom)+"px"; } if(parseInt(scroll_hand.style.top)<parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(scroll_hand_top)+"px"; } areaScroll(); } } function mousedown(e){ var oEvent = oEventUtil.getEvent(); y = parseInt(oEvent.pageY) - parseInt(scroll_hand.offsetTop); oEventUtil.addEventHandler(scroll_hand, "mousemove", mousemove); oEventUtil.addEventHandler(scroll_hand, "mouseup", mouseup); oEventUtil.addEventHandler(scroll_hand, "mouseout", mouseout); } function mousemove(e){ var oEvent = oEventUtil.getEvent(); var scroll_hand_top = parseInt(scrollup.offsetHeight); var scroll_hand_bottom = parseInt(category_list.offsetHeight) - parseInt(scrolldown.offsetHeight) - parseInt(scroll_hand.offsetHeight); if(parseInt(scroll_hand.offsetTop) <= parseInt(scroll_hand_bottom) && parseInt(scroll_hand.offsetTop) >= parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(oEvent.pageY) - y + "px"; if(parseInt(scroll_hand.style.top) > parseInt(scroll_hand_bottom)){ scroll_hand.style.top = parseInt(scroll_hand_bottom)+"px"; } if(parseInt(scroll_hand.style.top)<parseInt(scroll_hand_top)){ scroll_hand.style.top = parseInt(scroll_hand_top)+"px"; } areaScroll(); } } function mouseup(e){ oEventUtil.removeEventHandler(scroll_hand,"mousemove",mousemove); oEventUtil.removeEventHandler(scroll_hand,"mouseup",mouseup); } function mouseout(e){ oEventUtil.removeEventHandler(scroll_hand,"mousemove",mousemove); oEventUtil.removeEventHandler(scroll_hand,"mouseout",mouseout); } function displayScrollbar(){ if(category_list.scrollHeight > category_list.offsetHeight){ category_scroll.style.display="block"; } else{category_scroll.style.display="none";} } window.onload = function () { //var category_scroll = document.getElementById("category_scroll"); //var scroll_hand = document.getElementById("scroll_hand"); //var scrollup = document.getElementById("scrollup"); //var scrolldown = document.getElementById("scrolldown"); //var category_list = document.getElementById("category_list"); category_list.scrollTop = 0; displayScrollbar(); oEventUtil.addEventHandler(scroll_hand,"mousedown",mousedown); oEventUtil.addEventHandler(scrollup,"click",clickNarrowup); oEventUtil.addEventHandler(scrolldown,"click",clickNarrowdown); } </script> <style type="text/css"> .category_list{width:200px;border:solid 1px #666666;height:300px;position:relative;} #category_list{list-style:none;margin:0;padding:0;padding-left:20px;overflow:hidden;height:298px;} #category_scroll{height:298px;width:15px;border:green solid 1px;position:absolute;right:0;top:0;font-size:12px;cursor:hand;} #scrollup{height:15px;width:15px;border:green solid 1px;} #scroll_hand{height:100px;width:15px;border:green solid 1px;position:absolute;} #scrolldown{height:15px;width:15px;border:green solid 1px;position:absolute;bottom:0;} </style> </head> <body> <div class="category_list"> <ul id="category_list"> <li>1something</li> <li>2something</li> <li>3something</li> <li>4something</li> <li>5something</li> <li>6something</li> <li>7something</li> <li>8something</li> <li>9something</li> <li>10something</li> <li>11something</li> <li>12something</li> <li>13something</li> <li>14something</li> <li>15something</li> <li>16something</li> <li>17something</li> <li>18something</li> <li>19something</li> <li>20something</li> <li>21something</li> <li>22something</li> <li>23something</li> <li>24something</li> <li>25something</li> <li>26something</li> <li>27something</li> <li>28something</li> <li>29something</li> <li>30something</li> </ul> <div id="category_scroll"> <div id="scrollup">上</div> <div id="scroll_hand">滾動滾動滾動</div> <div id="scrolldown">下</div> <script type="text/javascript"> var category_scroll = document.getElementById("category_scroll"); var scroll_hand = document.getElementById("scroll_hand"); var scrollup = document.getElementById("scrollup"); var scrolldown = document.getElementById("scrolldown"); var category_list = document.getElementById("category_list"); </script> </div> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

現在有個問題就是當點中捲軸後,觸發鍵盤的上下鍵,來控制上下滾動。然而,一般情況下,鍵盤事件似乎只針對Form表單對象和Winsow表單對象起作用(除IE),那麼當建立事件處理函數時該怎們寫呢?
oEventUtil.addEventHandler(category_hand,"keyup",test),似乎是不行,畢竟category_hand代表的是一個DIV
test()函數如下:
function test(){
//alert("RUN");
var oEvent = oEventUtil.getEvent();
if(oEvent.keyCode==38){
alert("UP");
clickNarrowup();
}
if(oEvent.keyCode==40){
clickNarrowdown();
alert("DOWN");
}
}
請高手幫忙解答一下。

疑問二:這段代碼中,在定義變數的時候我是在HTML中間插入的代碼,用於擷取對象,如果這段代碼插入到window.onload函數中時,那麼很有可能此時這些對象尚未載入,無法讀取到而產生程式錯誤,那麼問題就是,如果我想完全將:
<script type="text/javascript">
var category_scroll = document.getElementById("category_scroll");
var scroll_hand = document.getElementById("scroll_hand");
var scrollup = document.getElementById("scrollup");
var scrolldown = document.getElementById("scrolldown");
var category_list = document.getElementById("category_list");
</script>
引入到<head>之間怎們處理比較好呢?
blueidea版主給出的解決方案
當前1/2頁 12下一頁閱讀全文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.