Code
<!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>
Now there is a problem: when the scroll bar is clicked, the up and down keys of the keyboard are triggered to control the up and down scrolling. However, in general, keyboard events seem to only work for Form form objects and Winsow form objects (except IE), so how should we write when creating event handling functions?
oEventUtil.addEventHandler(category_hand,"keyup",test) seems not to work. After all, category_hand represents a DIV.
The
test() function is as follows:
function test(){
//alert("RUN");
var oEvent =
oEventUtil.getEvent();
if(oEvent.keyCode==38){
alert("UP");
clickNarrowup();
}
if(oEvent.keyCode==40){
clickNarrowdown();
alert("DOWN");
}
}
Please help me answer this question.
Question 2: In this code, when defining the variable, I inserted the code in the middle of the HTML to obtain the object. If this code is inserted into the window.onload function, then it is very likely that At this time, these objects have not been loaded and cannot be read and a program error will occur. The question is, if I want to completely:
What is the best way to deal with the introduction of
to
function $(id) { return document.getElementById(id); }