首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onmouseover和onmouseout setInterval

onmouseover和onmouseout setInterval
EN

Stack Overflow用户
提问于 2020-08-21 04:05:35
回答 1查看 385关注 0票数 0

这个问题一直困扰着我。我使用滑块作为我的应用程序。对于滑动应用程序,我正在使用夹板-js。我的图像滑块很好用。

我已经将滑块与我的li元素连接起来。我想要定制onmouseoveronmouseout事件。当用户在mouseover上触发li时,它将暂停,当mouseout将恢复幻灯片时。

我正在检查这个堆叠溢出柱,并试图通过使用setinterval来像人一样执行逻辑,但无法做到。

代码语言:javascript
复制
const autoPlay = false;
const pauseOnHover = document.getElementById('pause');

function nextSlide() { //I don't what kind logic I should use to pause the slide

}

let  timer = setInterval(function() {
  nextSlide();
}, 400);


pauseOnHover.onmouseover = () => {
  if( autoPlay) {
    timer
  }
}
pauseOnHover.onmouseout = () => {
  if (autoPlay) {
    clearInterval(timer)
  }
}
代码语言:javascript
复制
<div class='checklist' id="pause">
  <ol>
    <li id="link-1">Assign jobs in an instant.</li>
    <li id="link-2">Always see what's happening.</li>
    <li id="link-3">Easy to set-up & use.</li>
    <li id="link-4">Be more sustainable</li>
  </ol>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-21 04:15:57

请输入并恢复鼠标悬停时的间隔,如果存在间隔,请取消。

请试试下面的代码。

代码语言:javascript
复制
const autoPlay = false;
const pauseOnHover = document.getElementById('pause');

function nextSlide() {
  //For example I have put console.log, you mat put any code block here.
  console.log("hello");
}

let timer;


pauseOnHover.onmouseover = () => {

  autoplay = true;
  if (!autoPlay) {
    autoplay = true;
    timer = setInterval(function() {
      nextSlide();
    }, 400);
  }
}
pauseOnHover.onmouseout = () => {

  autoplay = false;
  timer && clearInterval(timer)

}
代码语言:javascript
复制
<div class='checklist' id="pause">
  <ol>
    <li id="link-1">Assign jobs in an instant.</li>
    <li id="link-2">Always see what's happening.</li>
    <li id="link-3">Easy to set-up & use.</li>
    <li id="link-4">Be more sustainable</li>
  </ol>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63516374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档