Continuously execute the scroll function as long as the cursor remains over the element

Is there a method to continuously execute a function while the cursor hovers over a specific DOM element? I need to trigger a scroll action whenever the user hovers over an arrow, with an optional timeout feature. This requirement must be met without utilizing jQuery!

Check out my current code snippet:

public onHoverDown() {
    this.tabList.nativeElement.scrollBy(0, 10);
    const element = this.tabList.nativeElement;
    if(element.offsetHeight + element.scrollTop >= element.scrollHeight) {
      console.log('Reached Bottom Limit');
    }
  }

  public onHoverUp() {
    this.tabList.nativeElement.scrollBy(0, -10);
    const element = this.tabList.nativeElement;
    if(element.scrollTop === 0) {
      console.log('Reached Top Limit');
    }
  }

Here's the corresponding HTML:

<div class="list-arrow-up" (mouseover)="onHoverUp()">▲</div>
  <ul #tabList>
    <li><!-- Multiple list items --></span></li>
  </ul>
  <div class="list-arrow-down" (mouseover)="onHoverDown()">▼</div>

Answer №1

For a unique solution, I propose trying a new method. Implementing a function triggered by hovering over a particular DOM element that smoothly scrolls to the bottom over a specified duration. Additionally, including a function to halt the scrolling when the cursor moves away from the element would enhance user experience. To optimize performance, consider checking if the page is already scrolled to the bottom before initiating the scroll action.

Answer №2

Feel free to customize the moveDown and moveUp functions according to your needs. This code snippet is in JavaScript, but you can easily convert it to Angular TypeScript.

Here's the HTML:

<a onmouseover="overDown()" onmouseleave="stopMoveDown = true">Down</a>
<a onmouseover="overUp()" onmouseleave="stopMoveUp = true">Up</a>

And the original JavaScript code:

  var stopMoveDown = false;
  var stopMoveUp = false;

  function moveDown() {
     console.log('Move Down');
     if(!stopMoveDown)
        setTimeout(moveDown, 100);
  }

  function moveUp() {
     console.log('Move Up');
     if(!stopMoveUp)
        setTimeout(moveUp, 100);
  }

  function overDown(){
    stopMoveDown = false;
    moveDown();
  }

  function overUp(){
    stopMoveUp = false;
    moveUp();
  }

To see a demonstration of these functions in action, click the following link: Live Demo. Make sure to open the console window to observe the moving up/down messages when hovering over the Down/Up links.

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

Tips for displaying the date of a JSON response in Angular HTML format?

When working with Angular HTML, I am looping through a JSON array using ngFor and accessing the birth date data like this: <td>{{item.customer_info.birth_date}}</td> The data is being displayed as ddMMyyyy format, but I would like to change it ...

`Zooming and scrolling feature within a masked image`

I'm struggling to achieve a scrolling zoom effect similar to the website mentioned below, but I can't seem to get it to fully zoom. Additionally, when I try to zoom in on a clipped shape or text while scrolling, the entire div ends up scrolling ...

Turn off the dynamically generated multiselect dropdown feature (using a plugin) in an angular application

I have included a multi-select dropdown in each row of a table, allowing the user to modify the selection and save it. The configuration for the multi-select dropdown is stored in a TypeScript file. Now, I am looking to disable the dropdown in certain ro ...

How about connecting functions in JavaScript?

I'm looking to create a custom function that will add an item to my localStorage object. For example: alert(localStorage.getItem('names').addItem('Bill').getItem('names')); The initial method is getItem, which retrieves ...

Tips on showcasing jQuery autocomplete suggestions on different fields?

Seeking assistance with jQuery and autocomplete as a newcomer to the technology. Despite extensive research, I am struggling to understand how to showcase query results on a website. The code below is nearly identical to the example on the jquery autocomp ...

"CSS styles applied to the body element do not automatically transfer to the h1

Explore the World of Coding: <body class="pageDesign"> <h1>CSS - A Creative Journey</h1> <p><strong>Discover the beauty of coding</strong> and unlock a world of possibilities with CSS. Let your imagination soar as you d ...

Content displayed above a slideshow of images in a stunning gallery on Squarespace platform

Currently working on a one-page landing page using Squarespace and running into some issues with creating a slideshow. You can check out my website here (the slideshow is located at the bottom of the page): Squarespace provides default options for text p ...

How do we handle parent elements of clicked elements using Javascript event delegation?

http://jsfiddle.net/walkerneo/QqkkA/ In the realm of Javascript event delegation, there are many discussions related to using it for elements that serve as targets for click events. However, a less explored topic is how to implement event delegation for e ...

What are the steps for implementing middleware that relies on a socket connection?

Using express.io, I am currently working on creating a middleware that necessitates a connection to a remote server through two sockets. However, I have encountered an issue. var net = require('net'); module.exports = function (host, port) { ...

I need help with my calendar that only displays months with 31 days! Can anyone assist me, please?

My calendar was functioning properly earlier. However, it has started to skip months and only display those with 31 days. For example: if the current month is May, the next month should be June. But instead, it repeats May and then jumps to July (which ha ...

Is it possible to combine multiple jQuery selectors for one command?

I need some help with jQuery commands. I have two similar commands, but not exactly the same. Here are the commands: $("#abc > .className1 > .className2 > .className3"); $("#pqr > .className1 > .className2 > .className3"); Is there a w ...

The sticky position is malfunctioning even when there is no parent element with the overflow hidden property

// observer for feature section let featuresSection = document.querySelector('#featuresSection'); let callbackFeature = (items) => { items.forEach((item) => { if (item.isIntersecting) { item.target.classList.add("in ...

Is there a way to open Internet Explorer using a specific URL from a different browser?

Imagine a scenario where a visitor lands on www.mysite.com using MS Edge. Instead of staying in Edge, I would like the page to recognize that Edge is being used and automatically launch Internet Explorer with the same URL. Is there a way to achieve this? ...

Struggling to accurately determine the intersection face vertex positions in Three.js

Hello there! I've been immersed in the world of three.js, dealing with loading models using THREE.JSONLoader. Currently, I'm faced with the task of selecting these objects and their faces, which I've managed to do successfully. Now, my goal ...

Customizing upload directories in elfinder for dynamic path generation

In my CodeIgniter project, I am trying to configure elfinder to have a dynamic upload path for each unique ID. The folder structure I am working with is as follows: Below are my elfinder settings: $idce = $_GET['id_c']; $client_code = $this- ...

How to retrieve errors from the backend when uploading an array using Angular?

I am facing an issue with my Angular application where I need to handle server-side failures when sending a list of strings to a backend server. Despite following tutorials, I am struggling to display error messages on the client side in case something goe ...

Oops! Looks like there was an issue with defining Angular in AngularJS

I am encountering issues while attempting to launch my Angular application. When using npm install, I encountered the following error: ReferenceError: angular is not defined at Object.<anonymous> (C:\Users\GrupoBECM18\Documents&bs ...

Navigating the browser view across an extensive HTML page without relying on scrollbars

I have a large HTML page (approximately 7000x5000) and I need to be able to move the user's view around with JavaScript. Disabling the ability for the user to scroll by hiding the browser scrollbars using overflow:hidden on the HTML element is easy. ...

Why are my AngularJs elements not appearing in the print dialog window?

Whenever I try to open the print Dialogue on a specific page, all I'm seeing is a blank screen. The majority of the content on this page consists of AngularJS elements. To trigger the print dialogue, I use the following code: <a href=""onclick="wi ...

Create a composition of several debounce promises in JavaScript

I am looking for a way to efficiently manage multiple costly server calls by continuously invoking a function that accepts a key and returns a promise containing an object. This object is guaranteed to have the requested key along with additional values, i ...