【问题标题】:CSS and Jquery HoverCSS 和 Jquery 悬停
【发布时间】:2012-10-08 22:13:16
【问题描述】:

编辑:在此编辑之前,我要求提供涉及 CSS 的解决方案。我被告知我需要 Javascript 来完成以下任务

说明:我有一个显示成员完整个人资料的类(profile-inner)。它的背景颜色是白色。我已经悬停在 profile-inner 工作。它的背景颜色是灰色。 “profile-footer”类中有“评论”链接。当它被点击时,它会切换类“inline-profile-comment”(展开/折叠)。

问题:“profile-inner”悬停选择整个容器,包括切换类“inline-profile-comment”。我不想要这个。我只希望在未显示“inline-profile-comment”时悬停。

HTML

<div class="profile-inner">
  an entire profile

    <div class="profile-footer">
      <a data-toggle="collapse" data-target="#<%= profile_item.id %>_comment">
       Comment</a> 
       <ul><li>lots of list items etc</li></ul>

       <div class="inline-profile-comment">
      <div id="<%= profile_item.id %>_comment" class="show collapse">
     The comment form
   </div></div>

 </div></div>

CSS

.profile-inner {
  background-color: rgb(255, 255, 255);
}

.profile-inner:hover {
  background-color: rgb(243, 243, 243);
  cursor: pointer;
}

我希望我已经解释得足够好。感谢您的帮助。

【问题讨论】:

  • CSS 中没有隐藏元素选择器。如果您希望仅在隐藏inline-profile-comment 时对.profile-inner 产生悬停效果,则需要javascript。
  • 我以为会是这样,我应该注意到我意识到了这种可能性。我将为未来的读者编辑这个问题。

标签: jquery html jquery-selectors hover show-hide


【解决方案1】:

这是一个概念证明。我非常喜欢在对象的主要元素上切换状态,在这种情况下是你的 profile-inner。本质上,我们所做的是使用“隐藏评论”状态来定义我们的 CSS 规则,切换评论表单和悬停。

http://jsfiddle.net/amustill/NgzU6/

【讨论】:

  • 我现在正在使用所有这些答案的解决方案,我会告诉你进展如何。谢谢!
  • 感谢您的意见。我在我的应用程序的另一部分中使用了此代码:)
【解决方案2】:

如果你在切换发生后add/remove 一个类并将悬停的css 选择器更改为class,我认为你可以这样做,就像

JS

$('.profile-inner').toggleClass('hover');
$('.profile-footer a').on('click', function(e){
    e.preventDefault();
    $('.inline-profile-comment').toggle('fast',function(){
        $('.profile-inner').toggleClass('hover');
    });
});

CSS

profile-inner {
    background-color: rgb(255, 255, 255);
}
.profile-inner.hover:hover {
    background-color: rgb(243, 243, 243);
    cursor: pointer;
}
.inline-profile-comment{ display:none; }

Working Example.

【讨论】:

  • 我现在将在我的代码中实现这一点,并让您知道它是如何进行的。谢谢!
  • 谢谢兄弟,效果很好。但是有一个干扰。该应用程序是一个 Ruby 应用程序,我正在使用 twitter bootstrap gem。它内置了 Javascript 来触发显示折叠功能。如何逃避内置功能并使用这个功能?因为我认为这两者会相互干扰,因为切换不顺畅。
  • 不客气,我认为如果你从 div 中删除 class="show collapse",那么它可能会停止冲突。
  • 它在 Fiddle 中效果很好。但是js教会了我,我的html乱七八糟,哈哈。我得打扫一下。我是 Ruby 新手,我正在渲染这些表单,所以如果我不注意自己在做什么,html 会变得非常混乱(就像现在一样)。
  • 很抱歉再次打扰您,但自从收到您的回复以来,我一直在努力解决这个问题,并且有一个小问题我似乎无法解决。切换时一切顺利:它失去了悬停并更改了光标。问题是当我单击评论框中的文本字段时,它会切换悬停。奇怪的是,我可以点击评论框附近的任何地方,这不会发生,但是一旦点击了文本字段,它就会切换悬停类。有什么建议么?谢谢。
【解决方案3】:

你可以做你有的,加上这个:

.profile-inner:hover .inline-profile-comment {
    background-color: Whatever it should be; }

【讨论】:

  • 是的,但我想在切换所述类时一起失去悬停。不过感谢您的帮助。 :)
  • 抱歉,误读了您最初的问题……amustill 的 js 解决方案就是我当时的做法。
猜你喜欢
  • 2010-12-11
  • 2010-12-23
  • 2021-04-02
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 2010-11-06
  • 2023-03-10
相关资源
最近更新 更多