0

I have this SVG element with a scroll bar. I want to make it horizontally scrollable on mouse wheel. scrollable svg

I use the (wheel) event to trigger the method move()

<div id="tab-primary-wrapper" class="inner-wrapper" (wheel)="move()" #scrollable style="overflow: auto" >
   <div>

which does this:

 move(){
    var el = document.querySelector('#tab-primary-wrapper .inner-wrapper')
    if(el){
     el.addEventListener('mousewheel', function(e: WheelEvent) {
       this.scrollLeft += e.deltaX;
       e.preventDefault();
     });
    }
  } 

I'm not getting any movement of the scrollbar!

Is there any method which I can use that triggers the movement automatically?

Or, should I implement the scrolling using css?

I would very much appreciate any suggestion, of how would the best way be to proceed further.

Update:

In the .html I added an id="container" which I accessed in the .ts file:

<div id="container" #scrollable style="overflow-x: scroll" (wheel)="onWheel($event)">
    <div>

In the typescript I defined:

onWheel(event: WheelEvent): void {
    console.log("move...");
      document.getElementById('container').scrollLeft += 40;
  }

where I move from left to right but I cannot move then from right to left.

2 Answers 2

3

To scroll on both ways...

  onWheel(event: WheelEvent): void {
    if (event.deltaY > 0) document.getElementById('container')!.scrollLeft += 40;
    else document.getElementById('container')!.scrollLeft -= 40;
  }
Sign up to request clarification or add additional context in comments.

Comments

2

With (wheel) inside the div, you declare a wheel event handler, there's no need to explicitly add another listener using addEventListener.

<div ... (wheel)="onWheel($event)" ... ><div>

onWheel(event: WheelEvent): void {
   (<Element>event.target).parentElement.scrollLeft += event.deltaY;
   event.preventDefault();
} 

Please have a look at this StackBlitz

6 Comments

thank you for your input. Unfortunately the horizontal scrolling doesn't work yet and I get this strange error: _co.onWheel is not a function at Object.eval [as handleEvent]
Update: I got an error because I called the move() method instead of the onWheel() so that I just renamed. Now I don't get any error but just the scrollbar doesn't move on mouse event.
Try to use event.deltaY
It doesn't work with deltaY. I wonder if the .parentElement.scrollLeft since I use the (wheel)=onWheel($event) method on the parent, on the element where the scroll is defined. When we say parentElement.scrollLeft doesn't this scroll the parentElement of the current element where the method is defined?
I did this too, but It unfortunately doesn't move the scroll horizontally...I don't know if trying to do this with css would be a good idea!?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.