


Javascript simulates scroll scrolling effect script page 1/2_javascript skills
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
The solution given by the blueidea moderator
1. Keyboard events should be added to the window for unified deployment and processed according to the category_hand activation status.
2. Put it in the function and wait until the page is loaded before executing it.
1. "Keyboard events should be added to the window for unified deployment, and processed according to the category_hand activation status" is the strategy to realize your sentence:
"When the scroll bar is clicked, trigger the up and down keys of the keyboard to control Scroll up and down”
When category_hand is clicked, a variable is set to 1, indicating that it is activated. When another mouse button event occurs in the web page, it will be deactivated.
A function responds to the keyboard event of the window object. If the category_hand is activated when the keyboard event occurs, the relevant code will be executed.
2. Get the page element object. Now everyone generally uses the $() function:
function $(id) { return document.getElementById(id); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
