What prevents a click event from reaching the <body> element?

This slider runs in an animation loop until one of the dots at the top is clicked. Once clicked, the animated elements (the <span> dots and the <figure>s) receive a style that halts the animation.

<section id="slider">
    <span class="control" tabindex="1"></span>
    <span class="control" tabindex="2"></span>
    <span class="control" tabindex="3"></span>
    <figure class="slide">
        <!--Image 1--->
    </figure>
    <figure class="slide">
        <!--Image 2--->
    </figure>
    <figure class="slide">
        <!--Image 3--->
  </section>

Script:

window.onload = function () {
    var slider = document.querySelector('#slider');
    var animated = slider.querySelectorAll('.control, .slide');

    function switchAll (focus) {
        animated.forEach(function (el) {
            el.style['animation-name'] = focus ? 'none' : null;
        });
    }

    document.querySelector('body').addEventListener('click', function (e) {
        switchAll(slider.querySelector('.control:focus'));
    });
};

Once the animation stops, it should restart if something other than the dots is clicked (losing focus). However, on Firefox, this only works if the click occurs outside of the #slider.

If you click inside the currently displayed <figure>, it disappears - which is the expected CSS behavior since no dot has focus. But the animation does not restart; the first click event does not reach the <body>. Only a second click triggers the event listener.

I've tried attaching the event to the #slider, but the result remains the same. It seems like the event does not bubble past the <figure> elements. Attaching it to those elements works, but is not the optimal solution.

In any case, I would like to understand what happens to the event.

Answer №1

Recently, I found myself caught in a common pitfall: Why does the blur event prevent the click event from working?

As soon as the control loses focus, the slide is shifted out of sight, making it impossible to click on it. Strangely, even clicking in the background doesn't seem to work; almost as if the click action is being deliberately blocked.

The trick here is to synchronize the animation with the focus and blur events instead of relying solely on the click event.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring Angular 2 Routing across multiple components

I am facing a situation where I have an app component with defined routes and a <router-outlet></router-outlet> set. Additionally, I also have a menu component where I want to set the [routerLink] using the same routes as the app component. How ...

What is the best way to create custom middleware in Express to promptly respond with a 413 status code if the incoming request exceeds a

It seems that the current setup is not returning any response. What's strange is that the debug logger indicates POST /ServiceName 413 2ms var maxSize = 1000*1000; module.exports = function (req, res, next) { var size = req.headers['content-l ...

What is the best way to make the child Div float to the bottom of the parent Div?

Screenshot I attempted various CSS techniques in an effort to make the .booknetic_appointment_container_footer float to the bottom of #booknetic_theme_5, but unfortunately, I was unsuccessful. The closest solution I found involved hardcoding padding, but ...

verifying the date in a specific moment

Is there a way to validate the date accurately? to determine whether she cleared it or not. I've exhausted all my options. Despite reading through the documentation, I am unable to get it right. Here is what I attempted: if ('2023-03-03 ...

Could leaving the src attribute empty trigger a request on all web browsers?

I'm curious about the safety of using an html image tag like this <img src width="30" height="20"/> When the ImageUrl is not set in ASP.NET, the Image control renders like this. I will populate the src attribute later on the client side I ju ...

Using JavaScript to cheat on a typing test by automating the typing of letters

I am currently working on a solution to simulate key presses for a typing test. My idea is to extract individual letters from the text, store them in an array, and then simulate pressing each key with a delay between each press. Below is the HTML layout o ...

Using Javascript to retrieve the childNode

I have encountered a challenge that I am struggling to overcome. My issue involves utilizing a dynamically generated table with 4 columns: a checkbox, text values, and a hidden input. echo "<tr><td><div class='input-containerh'> ...

What steps should I take to create a slider using my image gallery?

I have a unique photo gallery setup that I'm struggling with: As I keep adding photos, the cards in my gallery get smaller and smaller! What I really want is to show only 4 photos at a time and hide the rest, creating a sliding effect. I attempted ad ...

How can I use Jquery to animate an element to the right edge of the window?

Exploring the realm of jQuery animations, I am currently experimenting with animating a div to the right side of the window while changing its color using jQuery UI. This is just a fun project without any specific purpose in mind. Below is the code snippet ...

Can an App be duplicated from Chrome sources even if it displays all files except for the package.json?

I recently came across a React platform that I would like to dissect in order to gain a deeper understanding of it and potentially redesign it for a future project. Though the platform seems to have all the necessary files, I am unsure if I may be missing ...

Django plugin designed for showing a real-time feed of messages - powered by Dajax or Jquery?

Currently, I am attempting to set up a section in my Django application where updates or messages from the server can be displayed once specific tasks are done. I had initially looked into using a plugin that utilizes Dajax / Jquery for this feature, but ...

What steps do I need to take to successfully implement a $.fn. function that runs automatically when it is called?

I'm struggling with the following piece of code: function init() { var $contentButtonPanel: JQuery = $('#content-button-panel') $contentButtonPanel .find('.arbo .toggle, .collapsible-list li:has(ul) > ...

ReactJS creating trouble with incorporation of CSS in Table

I've noticed some strange behavior with the alignment of my table data. It seems to be all over the place. How can I fix this issue? Is there a way to specify column widths and ensure the text starts from the same position? Take a look at the table b ...

The value of a Highcharts series does not match that of a Data Source

Encountering an issue with Highcharts where the value of this.y does not reflect the data source. The discrepancy is apparent in the image below. Uncertain if this is a bug or user error. Screenshot illustrating the problem You can view my Demo here: htt ...

React - Issue with Input event handling when utilizing both onChange and onKeyDown functions

I was attempting to create a feature similar to a multi-select, where users could either choose a value from a list or enter a new value. The selected value should be added to an array when the user presses the enter key. To monitor changes in the input ...

Instructions on utilizing Tesseract.recognize in Node.js

I am working on developing an OCR program but encountered some issues while declaring the 'Tesseract.recognize' method. Here is the code snippet: const express = require('express'); const fs= require('fs'); const multer = r ...

The scroll animation shows unpredictable behavior when moving in both directions, and there may be a delay in response when using the

I'm attempting to create a smooth scroll effect to move to an element at the bottom of the page when scrolling down, and another element at the top of the page when scrolling up. Both elements have a height of 100vh. However, once I start scrolling d ...

List-style-type outside of a table's boundaries

I recently experimented with using <ol> as list elements within a table in order to dynamically insert new table rows. <table> <thead> <tr> <th>head</th> <th>head</th> <th>h ...

Using jQuery to select ASP control based on its dynamically generated Name attribute

When attempting to reference an ASP control in JavaScript, I initially used the generated ID but encountered issues. Surprisingly, referencing the control by its generated "Name" proved to be successful. ASP <asp:CheckBox ID="chkMyself" runat="server" ...

What is preventing my boolean from being altered?

Creating a basic calculator that can handle single-digit arithmetic. The current code is incomplete and redundant, so please avoid commenting on that. <!doctype html> <html> <head> <title>JavaScript Learning Zone</title> ...