How to suppress scrolling by mouse wheel under certain conditions?

ichiro kanazawa 80 Reputation points
2023-07-13T05:39:49.54+00:00

Thank you for reading my message.

I have a question about the .net maui blazor program. When the image is enlarged or reduced by operating the mouse wheel while the mouse cursor is on the image, the screen scrolling, which is the default behavior, moves at the same time. How can I suppress this screen scrolling when image scaling is working? Below is a sample program (added to Index.razor).


@page "/"

<h1>Hello world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

scale : @scale<br>
<div>
     <img src="/dotnet_bot.svg" @onwheel="OnMouseWheel" style="transform: scale(@scale, @scale);">
</div>


@code {
     private double scale = 1.0;
     protected void OnMouseWheel(WheelEventArgs e)
     {
         double delta = e.DeltaY * 0.0001;
         double newScale = scale - delta;
         scale = newScale;
     }
}
Developer technologies | .NET | Blazor
Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-07-13T19:44:57.65+00:00

    you need to cancel the default event (event.preventDefault()) as this needs to be done by Blazor's js event handler, not Blazor code, they give you a way to specify the option. try:

    <img src="/dotnet_bot.svg" @onwheel="OnMouseWheel:preventDefault" style="transform: scale(@scale, @scale);">
    

    note: blazor code can not directly access the dom or receive events. javascript is used for this purpose and sends async messages to the blazor code. with WASM the browser supplies a messaging api to WASM apps, and with blazer server signal/r is used.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.