简体   繁体   English

鼠标悬停在元素上时重复滚动功能

[英]Repeat scroll function while mouse hovering over element

is there a way how to repeat a function as long as the cursor is hovering over a specific dom element? 只要光标悬停在特定的dom元素上,有没有办法重复功能? I want to perform a scroll operation as long as long as the user is hovering over an arrow with an optional timeout. 只要用户将鼠标悬停在带有可选超时的箭头上,我就想执行滚动操作。 This task should be fullfillled without using jquery! 无需使用jquery 即可完成此任务!

Here is my current code: 这是我当前的代码:

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

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

And the HTML 和HTML

<div class="list-arrow-up" (mouseover)="overUp()">▲</div>
  <ul #tabList>
    <li><!--several List items --></span></li>
  </ul>
  <div class="list-arrow-down" (mouseover)="overDown()">▼</div>

I would use a different aproach. 我会使用其他方式。 I would execute a function when hovering over this specific DOM element, that scrolls down to the bottom, lasting x seconds. 将鼠标悬停在此特定DOM元素上时,将执行一个函数,该函数向下滚动到底部,持续x秒。 And then I would execute another function that stops this scroll down when cursor goes away from this element. 然后我将执行另一个函数,当光标离开此元素时,该函数将停止向下滚动。 You can improve performance by checking if you are already at the bottom of the page befor executing scroll down. 您可以通过执行向下滚动检查是否已位于页面底部来提高性能。

I hope this will help you. 我希望这能帮到您。 Change moveDown and moveUp function based on your requirement. 根据需要更改moveDownmoveUp函数。 This is javascript code, change this code to angular TypeScript. 这是javascript代码,请将此代码更改为angular TypeScript。

HTML HTML

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

JavaScript JavaScript的

  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();
  }

Click here to view demo, open console window and you can see the move up/down in the console window when mouse over Down/Up links. 单击此处查看演示,打开控制台窗口,将鼠标悬停在Down / Up链接上时,您可以在控制台窗口中看到上/下移动。 https://liveweave.com/bFG3OP https://liveweave.com/bFG3OP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM