简体   繁体   English

如何在js中为数组值设置动画?

[英]How do I animate an array value in jS?

I'm using this MagicMouse which works just fine.我正在使用这个很好用的 MagicMouse When I hover over an element I want to animate the width and height values and then return them to normal once the user unhovers.当我在一个元素上 hover 时,我想为宽度和高度值设置动画,然后在用户悬停后将它们恢复正常。

I have the following code which defines the values of the cursor in a jS array:我有以下代码,它定义了 jS 数组中 cursor 的值:

options = {
    "cursorOuter": "circle-basic",
    "hoverEffect": "pointer-overlay",
    "hoverItemMove": false,
    "defaultCursor": false,
    "outerWidth": 30,
    "outerHeight": 30
      };
    magicMouse(options);

Then I have this code to handle the hover:然后我有这段代码来处理 hover:

$(document).on("mouseenter mouseleave", ".fwds3dcov-thumbnail", function (e) {
  if (e.type == "mouseenter") {
    console.log('hovering');
  } else {
    console.log('unhovering');
  }
});

The mouse itself and the hover function work independently as expected, however I need to be able to animate the width and height values inside the if statement.鼠标本身和 hover function 按预期独立工作,但是我需要能够在 if 语句中为宽度和高度值设置动画。

Can I do something like MagicMouse.outerWidth = 70 ?我可以做类似MagicMouse.outerWidth = 70的事情吗?

I'm not familiar with updating values which originate from an array, if someone could point me in the right direction that would be greatly appreciated!我不熟悉更新源自数组的值,如果有人能指出我正确的方向,我将不胜感激!


EDIT:编辑:

I tried this and it 'works' but it causes a bug where the cursor regenerates in the corner of the screen as if it's reinitialising on every hover event.我试过这个并且它“有效”,但它会导致 cursor 在屏幕角落重新生成的错误,就好像它在每个 hover 事件上重新初始化一样。

$(document).on("mouseenter mouseleave", ".fwds3dcov-thumbnail", function (e) {
  if (e.type == "mouseenter") {
    magicMouse({
      "outerWidth": 60,
      "outerHeight": 60
    });
    console.log('hovering');
  } else {
    magicMouse({
      "outerWidth": 30,
      "outerHeight": 30
    });
    console.log('unhovering');
  }
});

I would suggest taking a look into magicMouse source code .我建议看看magicMouse 源代码 You will see that the "cursor" is just another DOM element and that its shape depends on applied CSS classes.您将看到“光标”只是另一个 DOM 元素,它的形状取决于应用的 CSS 类。

This is basic way to override the default library behavior:这是覆盖默认库行为的基本方法:

var magicMouseCursor = document.getElementById("magicMouseCursor");
var hoverEls = document.querySelectorAll(".magic-hover");
    hoverEls.forEach((item, i) => {
      item.addEventListener("mouseenter", event => {
          magicMouseCursor.classList.add('is-hover');
      });

      item.addEventListener("mouseleave", event => {
          magicMouseCursor.classList.remove('is-hover');
      });
    });
div#magicMouseCursor.is-hover {
    width: 60px !important;
    height: 60px !important;
}

A combination of the answer from Vladislav Leonov and my original code made this work. Vladislav Leonov 的答案和我的原始代码相结合,使这项工作有效。

Working code:工作代码:

$(document).on("mouseenter mouseleave", ".fwds3dcov-thumbnail", function (e) {
  var magicMouseCursor = document.getElementById("magicMouseCursor");
  if (e.type == "mouseenter") {
    magicMouseCursor.classList.add('is-hover');
  } else {
    magicMouseCursor.classList.remove('is-hover');
  }
});

div#magicMouseCursor.is-hover {
    transition: all 0.2s!important;
    width: 60px !important;
    height: 60px !important;
}

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

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