JQuery触发器悬停不适用于:not(:hover)

时间:2016-07-11 15:48:01

标签: javascript jquery css jquery-hover mouseenter

我的案例与此案非常相似:https://codepen.io/ianfarb/pen/EJunm

我正在尝试使用jquery mouseenter来触发悬停在第一张有Id的图像上。

window.setTimeout(function () {
    $('#one').trigger('mouseenter');
}, 2500)

然而,这似乎不起作用,无论是在我的代码中还是在上面的链接中都没有,因为:not(:hover)样式似乎总是被应用。我也试过用$()。offset()来触发重绘,但这也行不通。

1 个答案:

答案 0 :(得分:1)

您可以使用类来应用悬停样式(例如,使用opacity

.image {
  opacity: .5;
}

.image:hover,
.image.is-hover {
  opacity: 1;
}

然后在你的超时中添加它(并确保在真正的悬停时清理该类)

jQuery(function($) {
  function enter() {
    $(this).addClass('is-hover').siblings().removeClass('is-hover');
  }

  function leave() {
    $(this).removeClass('is-hover');
  }

  $('.image').hover(enter, leave);

  setTimeout(function() {
    enter.call($('.image:first-child'));
  }, 2500);
});
相关问题