[javascript] event target, scroll event question

Asked 1 years ago, Updated 1 years ago, 96 views

Hi, everyone.

I wrote a code to check if the img tag was scrolling, but the img tag did not detect the scrolling event.

const imgs = document.querySelectorAll('img');
    imgs.forEach(img => img.addEventListener('scroll',isclicked));
    function isclicked(){
      console.log('image is clikced!');
    }

I think the scroll event will only detect the window screen... I wonder if other objects can detect scroll events.

Question 1 How do I know the target of the event? Is the target of the event called target? Is it HTML element?

So I looked for mdn. https://developer.mozilla.org/en-US/docs/Web/Events/scroll

Target : DefaultView, Document, Element

I looked up the mdn, and I think the target refers to the object of the event. If the element is a target, I think the img tag can be a target of scrolling...

Question 2img Does the tag detect a scroll event?

Why doesn't the code above work?

Thank you for reading my question!

javascript event

2022-09-22 18:02

1 Answers

Hello!

If you use the wheel, the img tag can also be the target of the event .

//HTML
<img id="myItem" src="..." />

//JS
let myitem = document.getElementById("myItem"); 
myitem.addEventListener("wheel", Handler);

function Handler(e)
{
    console.log('Hello')
}

Have a nice day.


2022-09-22 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.