Exploring my initial encounter with JavaScript

I am currently reviewing the following code snippet:

if(text && spot && box) {
    document.getElementById('text-shadow-box').onmousemove = onMouseMove;

    document.getElementById('text-shadow-box').ontouchmove = function(e) {
        e.preventDefault();
        e.stopPropagation();

        onMouseMove({
            clientX: e.touches[0].clientX,
            clientY: e.touches[0].clientY
        });
    };
}

I am curious about the purpose of onmousemove = onMouseMove and why it is not assigned initially. Additionally, I have a question regarding the significance of 'e' in the context of the function(e), as well as e.touches[0].clientx.

Answer №1

Here is a breakdown of the code:

// Checking if variables ext, spot, and box are defined and not falsy values
if (ext && spot && box) {
/* Attaching onmousemove event handler to the function onMouseMove in JavaScript */
            document.getElementById('text-shadow-box').onmousemove = onMouseMove;
/* Another example where ontouchmove anonymous function gets attached */  
            document.getElementById('text-shadow-box').ontouchmove = function (e) {
 // The 'e' here is the event object passed as an argument to the handler function
                e.preventDefault(); e.stopPropagation(); 
// Calling function onMouseMove with certain properties from the touches
                onMouseMove({
                    clientX: e.touches[0].clientX,
                    clientY: e.touches[0].clientY
                });
            };


For more information on touchscreen events like ontouchmove, visit here. According to the specification, the touches object handles touches on the screen, providing different ways to access touched elements.
Explaining event.stopPropagation()

Description: Stops the event from bubbling up the DOM tree, preventing any parent handlers from receiving it.

Detailing event.preventDefault()

Description: Calling this method prevents the default action associated with the event.

Therefore, using event.stopPropagation() ensures that parent elements do not receive the event, while event.preventDefault() stops the default action, such as following a link when clicking.

Answer №2

When it comes to event registration in HTML DOM, onmousemove = onMouseMove plays a crucial role. It signifies that the method 'onMouseMove' will be triggered whenever there is a mouse movement event on the HTML element 'text-shadow-box'.

On the other hand, ontouchmove is an event dedicated to detecting touch movements, typically seen on mobile devices like Android clients. In its event handler, it utilizes the onMouseMove function to manage this event by converting the touch point into client coordinates.

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

Can you explain the function of v-bind within Vue?

I recently started learning Vue and decided to follow a video tutorial by Traversy Media on Youtube In the tutorial, the instructor used v-bind but I struggled to understand its usage. Despite my efforts, I still find the documentation a bit challenging ...

Using Socket.emit in Ionic may not function as expected

Currently, I'm in the process of establishing a socket connection between my Ionic application and a socket server that I've constructed. The socket connection seems to encounter issues specifically when running the app on an iOS simulator u ...

Choosing the right framework for implementing push notifications can be a critical decision. Should

I am currently working on a Java application that requires the server to send push notifications to the client every one second. To achieve this, I am using HTML5 server-sent events for one-way communication from the server to the client. However, my conce ...

Example of fetching Pubnub history using AngularJS

I am not a paid PubNub user. I am utilizing the example code for an Angular JS basic chat application from PubNub, and I want to access the chat history. This specific example can be found on the PubNub website. git clone https://github.com/stephenlb/an ...

Automatically populate input fields with text based on the option chosen from a dropdown menu

Having some issues here... I want to automatically populate the text input boxes in my form when a username is selected from the dropdown. The user data array is retrieved from a MySQL database. Not sure if my JavaScript code is correct. Here's my arr ...

The remove() function is failing to eliminate the element from all parent elements with the same class

I have a bunch of Wordpress posts. When the browser size is reduced to a certain point, I must relocate one specific element from each post to another element as per the design requirements. Here is the code I am using to iterate through each post and mov ...

Conceal and reveal text using JavaScript

I am attempting to create a function where text appears and disappears when clicking on the '+' sign, but I am facing difficulties in getting it to work. Additionally, when I apply this function to other sections, the original one stops working a ...

Guide on downloading a file from Firebase storage within a Vue-based web application

Scenario: You are in a situation where you need to download a PDF file (or any file type) from Firebase storage to your computer using a browser that has a Vue.js web app with Vuetify components. Despite having looked at the Firebase storage documentatio ...

Issue with VideoJS: MP4 file does not play after dynamically updating video URL

I have been using videoJs to display videos on my website. Below is the HTML code: <video id="player-vjs_html5_api" class="vjs-tech" crossorigin="anonymous" preload="auto" src="http://path-to-video/small.mp4"> <p class="vjs-no-vjs">Your bro ...

Breaking down arrays in Typescript

Let's say I have an array like this: public taskListCustom: any=[ {title: 'Task 1', status: 'done'}, {title: 'Task 2', status: 'done'}, {title: 'Task 3', status: 'done'}, {title: 'Task ...

What is the best way to pass a variable between different routing functions?

I am currently developing a server-side parser for an API. Each GET request made to my website must first initiate a request to the API, and since this request is always the same, I would like to encapsulate it within its own function. What is the best wa ...

Error message indicating that property 'cause' is not found on type 'Error' is displayed in a new Vue 3 project

After creating a new Vue app using npm init vue@latest, I set it up as follows: https://i.sstatic.net/cbedP.png Next, I replaced the content of the App.vue file with: <script setup lang="ts"> const e = new Error("something failed&quo ...

The issue with the Z-index being ineffective is causing the button to appear behind a container in the HTML-CSS

I am currently using Metro CSS (Windows 8 style) and running into an issue. Within my container, there are alerts displayed in blue, and above that is 'IT-CENTER'. When I click on 'IT-CENTER', a button should appear, but it seems to be ...

Stream data's modification to numerous controllers post ajax loading

One issue I encountered in my Angular app is related to the asynchronous loading of data from a server. Some controllers depend on this data, which is retrieved through an AJAX request. The problem arises because controllers end up pointing to an empty arr ...

Disabling images in Selenium Webdriver using Chromedriver - A step-by-step guide

The script provided above fails to prevent images from loading in Selenium's ChromeDriver. const { Builder, By, Key, until } = require('selenium-webdriver'); require('chromedriver'); (async function example() { const chro ...

Performing conditional aggregation in MongoDB through a collection of data

I attempted to update a MongoDB record conditionally using the code snippet below within a transaction. db.collection(SESSIONS_COLLECTION) .updateOne({_id: ObjectId(id)}, { $set: { end: { $cond: { if: { $gt: ["$en ...

Error encountered while custom building AngularJS/ngCordova in Ionic with injection module

Today I found myself on the edge of madness. All I wanted to do was add a plugin called ngCordova (specifically cordova-plugin-device) to my project. It seemed simple enough. I started with an Ionic tabs project, nothing more. Following some advice, I v ...

Which Angular2 npm packages should I be installing?

When I'm trying to create an empty app without using angular-cli, it's really difficult for me to figure out which packages or libraries to include. Searching for angular2 on npmjs yields unwanted results, forcing me to click through multiple li ...

Unable to pass parameter in ngRoute

When I pass /?preview=true in my function and catch it in app.js with $route.current.params.preview, it appears to be undefined when outputting to the console log. Can someone please assist me in resolving this error? Below is blah.js which will navigate ...

ts-jest should replace the character '@' with the '/src' folder in the map configuration

I have set up a node project using TypeScript and configured various paths in my tsconfig.json file, such as: "paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl' ...