# Prevent scrollViewer onWheel event from "propagating" to camera zoom

**URL:** <https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969>\
**Category:** Questions\
**Tags:** gui, camera, gui-scrollviewer\
**Created:** [June 1, 2022, 7:13pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969 "2022-06-01T19:13:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ajp/32/9504_2.png) [@ajp](https://forum.babylonjs.com/u/ajp)\
**Post date:** [June 1, 2022, 7:13pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/1 "2022-06-01T19:13:37Z")

</div>

I would like to stop the camera from zooming out/in when the UI scrollViewer is scrolling down/up. Is this possible? I could not see anything in the [docs for scrollViewer](https://doc.babylonjs.com/divingDeeper/gui/scrollViewer). Thanks!

> **[Babylon.js Playground](https://playground.babylonjs.com/#TF5SQ3)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 1, 2022, 8:32pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/2 "2022-06-01T20:32:44Z")

</div>

cc @DarraghBurke

---

<div class="post-metadata">

**Author:** ![DRLeria](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/drleria/32/50928_2.png) [@DRLeria](https://forum.babylonjs.com/u/DRLeria)\
**Post date:** [June 1, 2022, 10:42pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/3 "2022-06-01T22:42:44Z")

</div>

You could try to the scroll element using html / css on top of the canvas.  
Also You access the scroll element in your Main JS using: document.getElementByID(“scroller”);

A simple Demo:

> **[demo.rar](https://drive.google.com/file/d/15fJpmEqJtFisNdB2y_ANxm7woltEtL2Z/view?usp=drive_fs)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![PolygonalSun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/polygonalsun/32/2704_2.png) [@PolygonalSun](https://forum.babylonjs.com/u/PolygonalSun)\
**Post date:** [June 1, 2022, 11:21pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/4 "2022-06-01T23:21:07Z")

</div>

If you don’t need to have mouse wheel scrolling, you could just remove it:

```auto
camera.inputs.removeByType("ArcRotateCameraMouseWheelInput");

```

If you want to keep it when not hovering over the scrollViewer, you could try one of these two methods:

```auto
// Remove the mouse wheel input when inside and re-add it when outside
scroll_viewer.onPointerEnterObservable.add((ev) => {
    camera.inputs.removeByType("ArcRotateCameraMouseWheelInput");
});
scroll_viewer.onPointerOutObservable.add((ev) => {
    camera.inputs.addMouseWheel();
});

```

If you want to avoid creating objects, you could also try this approach:

```auto
// When inside of the scroll viewer object, just max out the precision to make the movement practically zero
scroll_viewer.onPointerEnterObservable.add((ev) => {
   camera.inputs.attached.mousewheel.wheelPrecision = Number.MAX_VALUE;
});
scroll_viewer.onPointerOutObservable.add((ev) => {
    // Return to default value, you may want to store the value and restore it if you change the value
    camera.inputs.attached.mousewheel.wheelPrecision = 3;
});

```

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ajp/32/9504_2.png) [@ajp](https://forum.babylonjs.com/u/ajp)\
**Post date:** [June 2, 2022, 9:58am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/5 "2022-06-02T09:58:30Z")

</div>

This is useful. Thanks @PolygonalSun. As I want to stop the camera from moving when the user is interacting with the UI modal, I have instead put this code into the render and dispose functions of the UI modal component. This is instead of listening for the onPointerEnter/Out . It also has the added advantage of working on mobile where there are no onPointerEnter/Out events triggered.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 2, 2022, 10:21am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/6 "2022-06-02T10:21:37Z")

</div>

> [@ajp](#):
>
> dispose functions of the UI modal component

Yes.

> [@ajp](#):
>
> It also has the added advantage of working on mobile where there are no onPointerEnter/Out events

So did you actually fix it with this? Else, I would have one more solution (very similar I believe) that has proved to work both on desktop and mobile.

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ajp/32/9504_2.png) [@ajp](https://forum.babylonjs.com/u/ajp)\
**Post date:** [June 2, 2022, 11:06am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/7 "2022-06-02T11:06:30Z")

</div>

> [@mawa](#):
>
> So did you actually fix it with this? Else, I would have one more solution (very similar I believe) that has proved to work both on desktop and mobile.

Thanks @mawa actually it’s not fixed as on mobile:

a) the [scroll of the scrollViewer does not work](https://forum.babylonjs.com/t/scrolling-scrollviewer-seems-to-not-work-on-mobile-difference-between-mobile-and-desktop-gestures/30972/4) so  
b) once that’s working then the panning (moving in Y axis?) will need to be avoided (that’s triggered for the camera when you use two fingers to move up/down) and  
c) if the pinch zoom was somehow to be used to do something to control the scrollViewer on mobile (though I don’t know what that would be) then these events would also need to be disabled somehow.

Sorry this is a bit messier than I’d like it to be so I’m happy to provide any clarifications and I welcome any suggestions! 🙂

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 2, 2022, 11:10am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/8 "2022-06-02T11:10:12Z")

</div>

Yes, that’s what I thought. Give a minute to find this recent PG that has proven to work. It has just one little downside to it. You have to first tap a control or the scrollbar to unlock the gesture on the scrollViewer. May be it can be improved. Else, it works both on desktop and mobile and prevents from loosing controls or zooming the scene when interacting with the GUI… Shouldn’t be all too long…

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 2, 2022, 11:36am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/9 "2022-06-02T11:36:15Z")

</div>

Ok so there goes. Sry for the somewhat rushed PG. I quickly extracted the code from another PG you don’t want to see 😉 It’s a huge mess. Anyways, the point you are interested in is not and I implemented this part in [your revised PG](https://playground.babylonjs.com/#TF5SQ3#1)  
Note that you can still combine this with the enter and out observable for desktop, which is more enjoyable on desktop because you don’t have to click. It won’t create any issue to have both (as far as I know). As I said, not 100% perfect but 'Hope this will help,

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ajp/32/9504_2.png) [@ajp](https://forum.babylonjs.com/u/ajp)\
**Post date:** [June 3, 2022, 11:25pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/10 "2022-06-03T23:25:38Z")

</div>

That’s great. Thanks @mawa. I disabled the `uicamera` as I don’t think it’s needed? [https://playground.babylonjs.com/#TF5SQ3#2](https://playground.babylonjs.com/#TF5SQ3#2) This solution definitely gets it a lot closer to an easier user experience so thank you very much for that! 🙂

And for my use case it’s for a slightly transparent modal UI that sits in front of the view so when the modal is first rendered I will call `camera.detachControl()` and then when it’s disposed of I will call the `camera.attachControl()`

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ajp/32/9504_2.png) [@ajp](https://forum.babylonjs.com/u/ajp)\
**Post date:** [June 5, 2022, 9:26pm UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/11 "2022-06-05T21:26:05Z")

</div>

I have [some demo code](https://playground.babylonjs.com/#TF5SQ3#4) that I think solves this and solves the other problem: [Scrolling scrollViewer seems to not work on Mobile?](https://forum.babylonjs.com/t/scrolling-scrollviewer-seems-to-not-work-on-mobile-difference-between-mobile-and-desktop-gestures/30972/6)

I wanted to point out that I did end up using the `onPointerEnterObservable` / Out as suggested by @PolygonalSun 😃 So thanks for everyone’s help. Particularly @mawa ! 🌟

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 6, 2022, 7:46am UTC](https://forum.babylonjs.com/t/prevent-scrollviewer-onwheel-event-from-propagating-to-camera-zoom/30969/12 "2022-06-06T07:46:17Z")

</div>

Thanks a lot for your kind words 😊 and MANY THANKS for taking the time and effort to share the results and complete thread with the community. I already bookmarked it.  
I am sure it will be VERY HELPFUL for many users 🧑‍🏫  
Have an awesome day 🏖 🌞
