处理多个 setInterval 调用

发布于 2024-12-10 22:48:24 字数 2467 浏览 0 评论 0原文

我无法弄清楚如何为一类 div 创建简单的弹出和放置效果。由于此类中每个项目的 onmouseover 事件当前都调用相同的函数,因此在 div 之间快速移动鼠标会使前一个 div 卡在中间(如果您将鼠标从 div 底部打开和关闭,您将看到效果我正在努力制作)。

我当前的方法还要求我复制相当多的代码来从每个单独的 div 的事件处理程序中调用函数,因此如果有一种方法将它们包装到一个函数中,我真的很想知道如何实现。我确信有一种简单而有效的方法可以完成这一切,只是似乎无法解决。

jsFiddle 示例: http://jsfiddle.net/DvXDb/< /a>

这是我的代码(脚本在底部):


div.bouncer{
       position:relative;
        top:50px;
    }




    function next(elem) {
        do {
            elem = elem.nextSibling;
        } while (elem && elem.nodeType != 1);
        return elem;
    }
    var thisDiv; var nextDiv; var selDiv;
    var containerDivList = document.getElementsByClassName("container");
    var containerArray = Array.prototype.slice.call(containerDivList);
    var container1 = containerArray[0];
    var container2 = containerArray[1];
    var container3 = containerArray[2];
    var container4 = containerArray[3];
    container1.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container1.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }
    container2.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container2.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }
    container3.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container3.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }


    var relativeHeights = ['0px', '5px', '10px', '15px', '20px', '25px', '30px', '35px', '40px', '45px', '50px'];
    var index = 10;
    var intervalHandle1;
    function callPop(thisDiv) {
        clearInterval(intervalHandle1);
        selDiv = thisDiv;
        intervalHandle1 = setInterval(popImage, 5);
    }
    function callDrop(thisDiv) {
        clearInterval(intervalHandle1);
        selDiv = thisDiv;
        intervalHandle1 = setInterval(dropImage, 5);
    }
    function popImage() {
        selDiv.style.top = relativeHeights[index];
        index--;
        if (selDiv.style.top === '0px') {
            index = 0;
        }
    }
    function dropImage() {
        index++;
        selDiv.style.top = relativeHeights[index];
        if (selDiv.style.top === '50px') {
            index = 10;
        }
    }

I am having trouble working out how to create a simple pop-and-drop effect for a class of divs. Since the onmouseover events of each of the items in this class are currently calling the same function, rapidly mousing between divs leaves the previous div stuck halfway through (if you mouse on and off from the bottom of the div, you'll see the effect I'm trying to produce).

My current approach also calls for me to replicate quite a bit of code to call the function from each individual div's event handlers, so if there's a way to wrap those up into one function I'd really like to know how. I'm sure there's a simple and efficient way to do all this, just can't seem to work it out.

jsFiddle example: http://jsfiddle.net/DvXDb/

Here's my code (script at the bottom):


div.bouncer{
       position:relative;
        top:50px;
    }




    function next(elem) {
        do {
            elem = elem.nextSibling;
        } while (elem && elem.nodeType != 1);
        return elem;
    }
    var thisDiv; var nextDiv; var selDiv;
    var containerDivList = document.getElementsByClassName("container");
    var containerArray = Array.prototype.slice.call(containerDivList);
    var container1 = containerArray[0];
    var container2 = containerArray[1];
    var container3 = containerArray[2];
    var container4 = containerArray[3];
    container1.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container1.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }
    container2.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container2.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }
    container3.onmouseover = function () {
        callPop(this.getElementsByTagName('div')[0]);
    }
    container3.onmouseout = function () {
        callDrop(this.getElementsByTagName('div')[0]);
    }


    var relativeHeights = ['0px', '5px', '10px', '15px', '20px', '25px', '30px', '35px', '40px', '45px', '50px'];
    var index = 10;
    var intervalHandle1;
    function callPop(thisDiv) {
        clearInterval(intervalHandle1);
        selDiv = thisDiv;
        intervalHandle1 = setInterval(popImage, 5);
    }
    function callDrop(thisDiv) {
        clearInterval(intervalHandle1);
        selDiv = thisDiv;
        intervalHandle1 = setInterval(dropImage, 5);
    }
    function popImage() {
        selDiv.style.top = relativeHeights[index];
        index--;
        if (selDiv.style.top === '0px') {
            index = 0;
        }
    }
    function dropImage() {
        index++;
        selDiv.style.top = relativeHeights[index];
        if (selDiv.style.top === '50px') {
            index = 10;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-12-17 22:48:24

这是一个使用 jQuery 的示例,您确实应该使用框架来完成这样的事情。他们简化了一切。

http://jsfiddle.net/AEJY9/

$(function () {

$('.bouncer').hover(
  function () {
    $(this).stop().animate({
      top: 10
      }, 500);
  },
  function () {
    $(this).stop().animate({
      top: 50
      }, 500);
  }
);


});

Here's an example using jQuery, you really should be using a Framework for things like this. They simplify everything.

http://jsfiddle.net/AEJY9/

$(function () {

$('.bouncer').hover(
  function () {
    $(this).stop().animate({
      top: 10
      }, 500);
  },
  function () {
    $(this).stop().animate({
      top: 50
      }, 500);
  }
);


});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文