Ask Your Question
3

How can I prevent the LeafletJS page from jumping when a layer is clicked?

asked 2022-02-25 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-11-02 19:00:00 +0000

pufferfish gravatar image

One solution to prevent the LeafletJS page from jumping when a layer is clicked is to use event.preventDefault() in the event listener for the click event. This will prevent the default behavior of the click event, which is to follow any links or jump to anchor points on the page. Instead, the map will respond to the click event without altering the page's scroll position.

For example:

// Add a click event listener to a layer
layer.on('click', function(event) {
  // Prevent the default behavior of the click event
  event.preventDefault();
  // Do something else with the click event, such as opening a modal window
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-25 11:00:00 +0000

Seen: 12 times

Last updated: Nov 02 '22