How to remove an event listener when e.target points to a hyperlink

element, I encountered an issue using a slideshow component sourced from our components library. This component receives swipe events from a utility function that is initialized upon mounting. The problem arose while testing on a mobile phone - tapping a link within a slide triggered both a swipe event and a click event. The utility function in question looks like this: function initSwipeEvents(el, deltaMin = 80) { const swipeData = { startX: 0, startY: 0, endX: 0, endY: 0, } let directionEvents = [] el.addEventListener("touchstart", (e) => { const t = e.touches[0] swipeData.startX = t.screenX swipeData.startY = t.screenY }) el.addEventListener("touchmove", (e) => { const t = e.touches[0] swipeData.endX = t.screenX swipeData.endY = t.screenY }) el.addEventListener("touchend", () => { const deltaX = swipeData.endX - swipeData.startX const deltaY = swipeData.endY - swipeData.startY if (Math.abs(deltaX) > deltaMin) { if (deltaX > 0) directionEvents.push("right") else directionEvents.push("left") } if (Math.abs(deltaY) > deltaMin) { if (deltaY > 0) directionEvents.push("down") else directionEvents.push("up") } directionEvents.forEach((direction) => el.dispatchEvent(new Event(`swipe-${direction}`)) ) directionEvents = [] }) } export default initSwipeEvents The slideshow component implementation is as follows:
Inside the slot of the slideshow component, there is a custom image component wrapped in a Nuxt Link. To address the issue of the unwanted swipe event triggering when clicking the link, modifications were made to the initSwipeEvents function by adding specific conditions to prevent the default action for touch events on the link element. Despite these adjustments, the problem persists, leading me to believe that the scope of the utility function may need refinement. Additionally, attempting to isolate the issue by logging click events while targeting the slideshow div yielded unexpected results compared to utilizing wheel events. I am unsure whether the necessary changes should be applied to the utility function or directly within the slideshow component itself. Your guidance on how best to approach this issue would be greatly appreciated.

Answer №1

After some investigation, I was able to figure out the answer independently. The issue stemmed from the utility function that was listening for touchStart, touchMove, and touchEnd events. In cases where only a single finger tap occurred, the touchMove event did not trigger. As a result, when the TouchEnd event calculated the difference between touchStart and touchMove data (screenX & screenY), it always exceeded the deltaMin threshold, causing the custom swipe event to be activated. To resolve this, I eliminated the touchMove event and integrated its functionality into the touchEnd 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

JQuery datepicker is functioning properly only after an alert when loaded with AJAX

Here's a puzzling question I have encountered. I implemented a field with a datepicker functionality. This field gets loaded into a div through an AJAX call. To sum it up, everything seems to be in working order.. But there's a strange issue. I ...

Emphasize table cells dynamically

Query How can I dynamically highlight a selected td? Codepen Example View Pen here Code Snippet The map consists of a randomly generated 2D array, like this: map = [[1,1,1,1,0], [1,0,0,0,0], [1,0,1,1,1], [1,0,0,0,1], [1,1, ...

What is the best way to implement spacing between the navigation bar logo, search bar, and links using flex in Bootstrap 5?

I want to customize the layout of my navigation bar using Bootstrap 5. Specifically, I would like the logo text to be on the left, the search bar in the middle, and the user links on the right. How can I achieve this using Bootstrap 5? <!-- NAVIGATION ...

V3: Ensuring Autocomplete Stays Focused and Clears Input on Keydown Enter

Is there a way to clear the v-autocomplete without exiting the autocomplete after the user presses enter? I came across a somewhat working solution for vuefiy 3 in an answer to a similar question on Stack Overflow. While it didn't completely solve my ...

Turn off hover opacity effect for a specific image

Currently, I've implemented a hover opacity effect on my images using the following CSS: img { opacity: 1.0; filter: alpha(opacity=1.0); } img:hover { opacity: 0.6; filter: alpha(opacity=0.6); } However, I now have a specific image for whi ...

Difficulty encountered in aligning items within neighboring table cells vertically

Currently, I am facing a styling issue with a few table rows. In this particular screenshot: https://i.sstatic.net/FcmJW.png The cells in the blue and red circles are part of a table row (with a height of 50px). Both are set to "vertical-align:top". The ...

Exploring the various possibilities of establishing multiple types of many-to-many relationships between two tables using Sequelize technology

Working with Sequelize in Node, I am dealing with Users and items. A user can interact with an item in various ways, such as voting or commending, which are distinct actions in this scenario. Initially, I considered having two separate many-to-many relati ...

A step-by-step guide to implementing Inline Linear Gradient in Nuxt/Vue

How can I create a linear gradient overlay on top of my inline background image? I attempted to add a CSS style but the class seems to be positioned beneath the image. <div class="header__bkgd" v-bind:style="{'background-image': 'url(&ap ...

What could be causing the axios response to come back empty in this scenario?

I keep getting this error on the console: "IntegrationError: stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId." I suspect that sessionId is empty because when I log response.data, it returns an "empty strin ...

What is the best way to store JSON data in the state of a ReactJS application?

I need assistance with a data format related issue. The current format of the data is being set by someone else in the backend of the application. When the data is empty, it looks like this: "opening_time":{"Mon":[["0"],["0"]], "Tue":[["0"],["0"]], "Wed" ...

Activate the button when the password is correct

Check out my code snippet: $("#reg_confirm_pass").blur(function(){ var user_pass= $("#reg_pass").val(); var user_pass2=$("#reg_confirm_pass").val(); var enter = $("#enter").val(); if(user_pass.length == 0){ alert("please fill password ...

Finding your site's First Contentful Paint (FCP) can be done by analyzing the

After doing some research on Google insights, I came across information about FCP. However, I'm still unsure about how to determine my site's FCP and other relevant metrics. If anyone could provide me with more information or share detailed link ...

Using Javascript/React to filter an array by a specific value

{ "team_group": ["Alex Smith", "Jake Brown", "Sarah King"], "group_data": { "Alex Smith": { "status": "member" }, "Jake Brown": { "status": &qu ...

When an answer is provided in Inquirer.js, pressing Enter is causing the process to terminate

Currently, I am working on creating a Command Line Interface (CLI) using the NPM package called Inquirer. It has been a useful tool so far, but I have encountered an issue. The interface functions correctly in terms of posing questions to the user, but onc ...

Nodes in force-directed graphs are magnetically attracted to the central point

After finding a solution to the issue in this question Insert text inside Circle in D3 chart I have encountered an unexpected behavior where my nodes are not positioned correctly. I am unsure about which property is controlling the x and y coordinates of ...

Function asynchronously returning Promise even after Await statement is executed

I've been attempting to develop a function that retrieves data from a document in the Firebase database using Nodejs: module.exports = async (collectionName, documentId, res) => { const collection = db.doc(`/${collectionName}/${documentId}`); t ...

Is there a method to give a webpage a subtle shimmering effect without utilizing CSS box-shadow?

Struggling to Develop a High-Performance Interface Feature I'm currently facing a challenge in coding an interface that requires a subtle and broad glow effect, similar to the example provided below: https://i.sstatic.net/E4ilD.jpg Exploration of ...

Dealing with multiple input fields that are generated using the map method in combination with react-redux

I am working on a project where I have a product list in JSON format stored in json-server. I am using React-Redux to fetch the data and render it in a table. Additionally, I have an input field where users can enter the quantity of each product. I need to ...

What is the process to subscribe and obtain data from a server-to-user channel using pusher-js?

I am currently hosting my application using next.js on Vercel. I want to integrate Pusher to provide real-time messages to users in a private and secure manner. Despite successful log entries, I am facing challenges in subscribing to the channel and retrie ...

Can you tell me the Bootstrap 4 equivalent class for '.well'?

While using bootstrap-3, the .well class can be utilized to create a rounded border around an element with a gray background color and padding. <div class="well">Basic Well</div> However, it seems that there is no .well class available in boo ...