简体   繁体   English

jQuery隐藏和显示不正确(多次)

[英]jQuery hide and show not properly (multiple times)

I'm having a problem with my project, on combination with jQuery/Coffeescript. 与jQuery / Coffeescript结合使用时,我的项目出现了问题。

On my homepage I have a block with text, with an arrow underneath it. 在我的主页上,我有一个带有文本的块,下面有一个箭头。 Under there, there are buttons, and every time I have my mouse over one of those buttons, I want the text block + arrow to move. 在下面,有一些按钮,每当我将鼠标悬停在这些按钮之一上时,我都希望文本块+箭头移动。 I do this with the jQuery UI library, with the method hide and show . 我使用jQuery UI库(使用hideshow方法)来完成此操作。

In my CSS code I made several classes: .position1 , .position2 and .position1 . 在我的CSS代码中,我做了几个类: .position1.position2.position1 Every time over hover with my mouse over one of the buttons, I want the text block to move to a specific position, so I change it's class (if someone has a better solution, I would gladly like to hear it). 每次将鼠标悬停在一个按钮上时,我都希望文本块移动到特定位置,因此我更改了它的类(如果有人有更好的解决方案,我很高兴听到它)。

Now the problem I'm having, is that sometimes the arrow hides and appears multiple times after each other (especially when I move my mouse very fast between the buttons) 现在我遇到的问题是,有时箭头会相互隐藏并多次出现(特别是当我在两个按钮之间快速移动鼠标时)

A simple (partial) version of my jQuery is as follows: 我的jQuery的简单(部分)版本如下:

var appear_arrow = function(to_position, show_delay) {
  removeClasses($('.arrow'));
  $('.arrow').addClass(to_class);
  $('.arrow').delay(show_delay).show('slide', 'slow');
};

var to_position1 = function() {
  $('.arrow').hide(0);
  $('.text_block').hide(appear);
  switchClass($('.text_block'));
  $('.text_block').show(appear);
  appear_arrow('position1', delay);
};

$('.button1').hover(
  function() {
    to_position1();
  },
  function() {}
);

My question, does anybody know why sometimes the arrow is appearing multiple times. 我的问题是,有人知道为什么有时箭头多次出现吗? Or does someone has a suggestion how to better do this? 还是有人建议如何更好地做到这一点?

jQuery queues up all its animations on a given Element rather than resetting it first. jQuery将所有动画排在给定的元素上,而不是先将其重置。

$("selector").stop(); // stops animations on matched elements and resets queue 

A stop().fadeIn('slow') however will start from the opacity the element had when you called stop, which could be solved by for example hiding/showing it immediately, then doing a full fade at the new location - whatever looks best for you. 但是stop()。fadeIn('slow')将从您调用stop时元素具有的不透明度开始,这可以通过例如立即隐藏/显示它,然后在新位置完全淡入淡出的方式来解决-无论看起来如何最适合你

On a side note: 附带说明:

Seems you wrote wrapper functions for jQuery's removeClass and toggleClass methods. 似乎您为jQuery的removeClass和toggleClass方法编写了包装函数。 removeClass() if called without arguments removes all classes, so you could possibly just use those if in a jQuery context anyway (assuming your methods have no extra functionality). removeClass()如果不带参数调用将删除所有类,因此您仍然可以在jQuery上下文中使用它们(假设您的方法没有额外的功能)。

Seeing how often you call $('.arrow') you may want to cache your selectors, at least per iteration, then pass it to appear_arrow(). 查看调用$('。arrow')的频率,您可能希望至少在每次迭代时缓存选择器,然后将其传递给appear_arrow()。

var $arrow = $('.arrow')

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

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