简体   繁体   English

点击并不总是触发切换事件

[英]Click doesn't always trigger toggle-event

I have sort of an imagemap, which is basically a lot of absolutely positioned divs, which, when clicked, will show or hide a tooltip. 我有一个图像映射,基本上是很多绝对定位的div,单击它们可以显示或隐藏工具提示。 Looks pretty great, apart from the fact, that it doesn't always "work". 除了事实并非总是“有效”之外,它看起来还不错。 It sounds silly, but some times I will have to click a couple of times to trigger the event. 听起来很傻,但是有时候我不得不点击几次才能触发该事件。 Maybe I'm just not clicking hard enough? 也许我只是点击的力度不够? ;) ;)

Markup 标记

<div class="container">
  <img src="img.png" />
  <div class="trigger"
    <div class="tooltip">
      Awesome tooltip is awesome!
    </div>
  </div>
</div>

Style 样式

.container {
  width:100px;
  height:100px;
  position:relative; }

img {
    position:relative; }

.trigger {
  width:50px;
  height:50px;
  position:absolute;
  top:50px;
  left:50px; }

.tooltip {
  width:100px;
  height:20px;
  position:absolute;
  top:35px;
  left:35px;
  display:none; }

Javascript Java脚本

$(".trigger").toggle(function () {
      $(this).children(".tooltip").stop(true, true).fadeTo(200, 0.9);
      $(this).siblings(".trigger").children(".tooltip").stop(true, true).fadeOut(200);
   }, function () {
      $(this).children(".tooltip").fadeOut(200);
   });

The markup and CSS is simplified, but imagine I have several tooltips over the image. 标记和CSS得到了简化,但是想象一下我在图像上有几个工具提示。 When I open one tooltip, all others should be closed. 当我打开一个工具提示时,应关闭所有其他工具提示。 I'm guessing this is where things go wrong, but I can't see the error. 我猜这是哪里出了问题,但我看不到错误。

In a similar function on the same site, I've semi-dynamically added some IDs, and hide all that is :not(ID), but I just can't believe that should be necessary. 在同一站点上的类似功能中,我已半动态添加了一些ID,并隐藏了所有的:not(ID),但我简直不认为这是必要的。

EDIT: Behold, a Fiddle: http://jsfiddle.net/CfYRv/ 编辑:看哪,小提琴: http//jsfiddle.net/CfYRv/

change your javascript to something like 将您的JavaScript更改为类似

$(".trigger").click(function () {
      $(".tooltip").fadeOut();
      $(this).children(".tooltip").fadeIn();
   });

Gah! 加! Need to finish my homework, but long answer short: toggle doesn't work here because you toggle a submenu but then click another. 需要完成我的作业,但简短的回答很长:切换在这里不起作用,因为您切换了子菜单,然后单击了另一个。 this hides the first submenu, but it's still considered open (it was only hidden). 这将隐藏第一个子菜单,但仍被认为是打开的(仅被隐藏)。 Thus you need to click it twice to open it... I hacked together an alternative but it's not the best code. 因此,您需要单击两次才能将其打开...我找到了一个替代方法,但这并不是最好的代码。 It'll at least give you an idea what needs done: 至少可以让您知道需要做什么:

http://jsfiddle.net/uj2A4/ http://jsfiddle.net/uj2A4/

$(".trigger").click(function () {
      if($(this).hasClass("active"))
          $(".tooltip",this).fadeOut(200);
      else {
          $(this).children(".tooltip").stop(true, true).fadeTo(200, 0.9);
          $(this).siblings(".trigger").children(".tooltip").stop(true, true).fadeOut(200);
      }
      $(this).toggleClass("active");
      $(this).siblings(".trigger").removeClass("active");
   });

Rather than toggle, let's use click: http://jsfiddle.net/CfYRv/3/ 让我们使用单击来代替切换: http : //jsfiddle.net/CfYRv/3/

This assigns the "active" tooltip a css class "ttactive". 这为“活动”工具提示分配了一个css类“ ttactive”。 Clicking on "some trigger" will fade out every active tooltip, and activate the one you just clicked. 单击“某些触发器”将淡出每个活动的工具提示,并激活您刚刚单击的工具提示。 If the one you just clicked was the active one, all it does is fade that one out. 如果您刚刚单击的那个是活动的那个,那么它所做的就是淡化那个那个。

You could probably still use toggle this way: 您可能仍可以通过以下方式使用切换:

 $(".trigger").click(function () {
  $(this).children(".tooltip").stop(true, true).toggle();
  $(this).siblings(".trigger").children(".tooltip").stop(true, true).fadeOut(200);
 });

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

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