Andrew Fielden
Andrew Fielden

Reputation: 3899

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. Unfortunately 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.

Upvotes: 2

Views: 889

Answers (2)

Matthew
Matthew

Reputation: 13332

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...

Upvotes: 4

Elliott
Elliott

Reputation: 5609

Have you tried attaching a MouseListener to the container that displays the map and then have your container requestFocus()?

Upvotes: 0

Related Questions