How to prevent browser from responding to scroll events
I have a Java applet which displays an interactive map. By using the scroll wheel the user can zoom in/out of the map. Unfortunatel开发者_如何学Cy the web browser (Firefox) also responds to the scroll events, and the applet ends up being scrolled off the visible page.
My question is, how can I prevent the browser from responding when the mouse is within the boundaries of the applet?
So far I've tried modifying my HTML to make the applet request focus
applet.focus()
I also tried adding a statement to my applet's Java code like this
requestFocus();
But neither has resolved the problem.
I could add an specific zoom in/out control in the applet, or possibly restructure my web page to reduce the need for the web page to scroll, but it would be nice if the scroll wheel did the job of zooming the map only.
Use javascript and do something like this:
var applet = document.getElementById("appletID");
applet.mouseover = function(e) {
document.body.onscroll = function(e) {
e.preventDefault();
}
}
applet.mouseout = function(e) {
//replace with empty function
document.body.onscroll = function(e) { }
}
not this is untested code. might be a couple typos
EDIT like i said typos... I tested this and it works for sure:
window.onload = function() {
var applet = document.getElementById("header");
applet.onmouseover = function(e) {
window.onscroll = function(e) {
//scroll where you want to be (the top)
scroll(0,0) ;
}
}
applet.onmouseout = function(e) {
//replace with empty function
window.onscroll = function(e) {}
}
};
Using the scroll method is not as clean but preventDefault didn't seem to work...
Have you tried attaching a MouseListener to the container that displays the map and then have your container requestFocus()?
精彩评论