The problem of the third parameter of addEventListener

I wrote a rotation graph to stop the rotation when hovering. I used the third parameter of the addEvnetListener implementation (onmouseenter/onmouseleave). I used true, that is, the event capture was executed, but this would go wrong. Every time I move the mouse in / out of the left and right arrows, the onmouseenter and onmouseleave events will be triggered again!

but as far as I understand it, the third parameter is true for event capture, and I am bound to the listening function in the outermost container. Why does my dom movement in the inner layer still trigger container listening events? Shouldn"t this be the result of bubbling?

the structure of dom is as follows

<body>
    <div id="container" class="container">
        <div class="pics">
            <img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3057381651,2463402979&fm=26&gp=0.jpg" alt="">
            <img src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4117493074,4130557145&fm=26&gp=0.jpg" alt="">
            <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=347260778,2859369145&fm=26&gp=0.jpg" alt="">
        </div>
        <a href="javascript:;" id="prev" class="arrow"><</a>
        <a href="javascript:;" id="next" class="arrow">></a>
    </div>
</body>

then use js to write two functions, one is when the mouse moves away, the picture automatically plays, the left and right arrows disappear; when the mouse is over the picture, the picture automatically stops, and the left and right arrows appear clickable to move the left and right pictures.

clipboard.png

the main js is as follows

    play();
    
    //
    prev.onclick = function() {
        animate(600);
    }
    //
    next.onclick = function() {
        animate(-600);
    }
    
    //stop
   container.addEventListener("mouseenter",function () {
        console.log("in");
        prev.style.display = "block";
        next.style.display = "block";
        stop();
    },true)

    //play
    container.addEventListener("mouseleave",function () {
        prev.style.display = "none";
        next.style.display = "none";
        play();
    },true)
    
    //setInterval
    function play() {
        timer = setInterval(function(){
            animate(-600);
        }, 2000);
    }
    //clearInterval
    function stop(){
        clearInterval(timer);
    }
Apr.15,2022

events propagate from document to specific elements, which is called event capture (downward propagation).
events propagate from collective elements to document, which is called event bubbling (uppropagation).

onmouseenter/onmouseleave these two events do not bubble, when you move in the child element, it does not trigger the parent element.

when the third parameter of addEvnetListener is true, it indicates that the event needs to be executed during the capture phase. However, all events that can be propagated down to the child element must pass through the parent element. So there is what you call a problem

therefore, you need to remove the third parameter of addEvnetListener (default is false).
this article hopes to be helpful to you

Menu