Is it possible for JavaScript to capture scroll events while allowing clicks to pass through?

Currently, I have a setup where the user needs to interact with the layer behind the transparent scroll capture:

http://jsbin.com/huxasup/4/edit?html,css,js,console,output

scrollerCapture = document.querySelector('.scroller-capture');
scrollerCapture.addEventListener('scroll', e => {
    e.preventDefault();
    console.log(scrollerCapture.scrollTop);
});

Is there a conventional method to achieve this functionality? Are tools like iScroll or ScrollMagic recommended for implementing this on mobile devices?

Appreciate any insights.

Answer №1

To create a <div> tag with the onscroll event, you can simply add the following code:

<div id="myDiv" onscroll="doSomething();">

Next, in your HTML file, include a script section like this:

<script>
function doSomething() {
        //Code to execute when scrolling occurs
    };
</script>

I hope this explanation is useful for you. Thank you!

Answer №2

If you want to locate a specific element in the DOM based on mouse coordinates, you can use

elementFromPoint()

This method helps in identifying the topmost element at the given coordinates.

The elementFromPoint() is a useful method of the Document interface that returns the element positioned at the specified coordinates. Find out more here

Once you have found the desired DOM element, you can trigger a click action on it.

element.click();

Read about HTMLElement click event here

Here's a quick example demonstrating how this can be implemented:

document.addEventListener('click', e => {
  var targetElement = document.elementFromPoint(e.pageX, e.pageY);
  targetElement.click();
})

You may also find the CSS property called:

pointer-events: none;

Useful as it allows mouse events to pass through certain elements.

The CSS property pointer-events provides control over which graphic element can receive mouse events under certain conditions. The value 'none' not only indicates that the element cannot be targeted by mouse events, but it also lets the events pass through and interact with elements beneath it instead. Learn more about pointer-events here

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

Section of links spanning 4 columns in full-width

Planning to design a 4 column link section for an upcoming website, aiming to achieve the look in the provided screenshot. https://i.sstatic.net/pydBp.png Each box will serve as a clickable link, changing color on hover. Utilizing the Bootstrap framework ...

Utilizing a dropdown list in HTML to dynamically change images

On my HTML page, I have implemented a dropdown list box. My objective is to change an image and update a label based on the selection made from the dropdown list box. I already have an array called 'temp' which represents the number of items. The ...

Transferring intricate data gracefully through the URL

I'm considering using something along the lines of: <script src="http://www.example.com/?key={'param':'value'}"> As illustrated in the example above, a JSON formatted string is being passed through the src URL. However, thi ...

"Form submission fails to populate _POST when file and text fields are used simultaneously

Looking for a solution... <form action="upload.php" method="get" enctype="multipart/form-data"> Enter a 10 digit mobile number: &nbsp;<input id="imgphno" type="text" maxlength="10" /> <br/><br/> <input id="file" type="file" ...

"Implementing a responsive design with a background image and radial gradient - how to

The image currently in use: https://i.stack.imgur.com/piK6l.jpg Here is how I've implemented it using Vue.js, Bootstrap, and CSS: Code Snippet <div class="landing"> <div class="container text-white details h-100"> ...

One or multiple web browsers set in the Browserslist of the project

I have recently started working with the Ionic framework and encountered some warnings while setting up my application. Can anyone help me with a solution? [ng] When starting the Ionic application, I received the following warnings: [ng] One or more browse ...

Is it possible to achieve a capsule shape using border-radius without specifying a fixed width or height?

Can a capsule shape be created using border-radius without specifying a width or height? I am looking to have the left and right sides of the capsule fully rounded while keeping the horizontal length straight. Setting the radius to 50% does not achieve th ...

How can you implement WriteFile in a GET Request without causing the response to be blocked

During the initialization of the app, I require a specific request to be made. This request entails parsing a portion of the JSON response into JavaScript and then saving it to a file. Simultaneously, the rest of the response needs to be sent to the front ...

Navigating to new location following the final iteration in a loop of asynchronous updates in MongoDB

Currently, I am developing an application using Node/Express/Mongodb/Mongoskin. I am facing an issue with the code responsible for updating a collection of documents: db.collection('invoices').find().toArray(function(err, dbDocs) { if (err) t ...

Encountering difficulties in generating a binary from a nodejs application with pkg

I am having trouble generating a binary executable from my nodejs app using the pkg command. My nodejs app is simple and consists of only three .js files: index.js, xlsx_to_pdf.js, and xlsx_extractor.js. This is how my package.json file looks like: { & ...

Keep the HTML structure of an element in the DOM while excluding it from an ng-repeat loop

I am currently working with the array badge = [{'name':'abc', 'flag': true}, {'name':'cde', 'flag': false}, {'name':'def', 'flag': true} ] After using it with ng-rep ...

Utilizing dynamic routes and incorporating additional search parameters within the URL structure in NextJS has never

Is there a way to use router.push(url); to redirect a user with a URL structure like this: [:lang]/something/[...dynamicRouteParams]?searchParam=true. The problem I'm facing is that the user ends up being redirected to a page with a URL structure lik ...

How can we toggle a function to expand or collapse fields generated in an ngFor loop?

One of my challenges involves managing a div that is repeated using *ngFor in Angular. This results in multiple divs on the page, each containing collapsible fields that can toggle. Essentially, I have nested collapsible fields within other collapsible fie ...

What is the best way to attach labels to objects in three.js when clicking the mouse?

Is it possible to apply a label to an object in a 3D scene using the three.js library through an onMouseClick event? ...

The AngularJS $filter(date) function is causing incorrect format outputs

Hey there! I've come across an issue with my AngularJS filter, where it's supposed to return a date in a specific format. However, when I try the following code: var input = '2015-08-11T13:00:00'; var format = 'yyyy MMM dd - hh:mm ...

Sorting arrays in javascript

My JavaScript array is structured like this: var data_tab = [[1,id_1001],[4,id_1004],[3,id_1003],[2,id_1002],[5,id_1005]] I am looking to organize and sort them based on the first value in each pair: 1,id_1001 2,id_1002 3,id_1003 4,id_1004 5,id_1005 ...

Determine the possible width of a concealed element

I am currently customizing the lavalamp plugin to be compatible with dropdown menus, but I have come across a minor issue. I am trying to determine the offsetWidth of a hidden element. Obviously, this question is a bit nonsensical. What I actually need is ...

"Encountering an error stating "breakpoint set but not yet bound" while debugging a node.js application

After following the instructions in this link to configure Visual Studio code for debugging nodejs and chrome(vuejs) applications, I encountered an error message saying "Failed to exec debug script" when attempting to debug "Meteor All" in Visual Studio co ...

Preventing clashes in namespaces while incorporating Bootstrap into a plugin for WordPress

After creating a WordPress plugin, I am considering revamping its layout with the help of bootstrap CSS and js. However, I have encountered issues in the past due to conflicting CSS naming conventions. I want to ensure that there are no namespace conflicts ...

Performing real-time arithmetic calculations on dynamic text input fields using AngularJS

In the process of creating a dynamic form, the number of textboxes is determined by the situation at hand. Each textbox is assigned an arithmetic formula, which is obtained from a database table. The structure of my form will be as follows: <div ng-ap ...