【问题标题】:Jquery .nextAll() is not working on hoverJquery .nextAll() 在悬停时不起作用
【发布时间】:2016-09-04 19:29:55
【问题描述】:

如果第一个 div 悬停,我会尝试操作堆叠在多个父 div 中的某个类。

我吓坏了,因为我无法让它工作

我创造了一个小提琴 https://jsfiddle.net/h0153wja/9/

但这里是代码:

HTML

<div class="testkrams wpb_column vc_column_container vc_col-sm-4">
  <div class="inner">
    <div class="wrapper">
      <div class="cat-title">
        <h2> TestText TextTest with break</h2>
      </div>
      <div class="box">
      </div>
    </div>
  </div>
</div>

JQUERY

$('.testkrams').hover(
  function() {
    $(this).nextAll(".cat-title").animate({
      marginLeft: '100px'
    }, 250);
  },
  function() {
    $(this).nextAll(".cat-title").animate({
      marginLeft: '0'
    }, 250);
  }
);

有什么想法吗?

我是不是太累了?

谢谢!

【问题讨论】:

    标签: jquery html jquery-animate jquery-hover


    【解决方案1】:

    nextAll() 寻找以下兄弟姐妹。 .cat-title 元素是 .testkrams 的子元素。你应该改用find()

    $('.testkrams').hover(function() {
        $(this).find(".cat-title").animate({
            marginLeft: '100px'
        }, 250);
    }, function() {
        $(this).find(".cat-title").animate({
            marginLeft: '0'
        }, 250);
    });
    

    Updated fiddle

    另请注意,您可以使用 transition 规则单独在 CSS 中实现此目的:

    .cat-title {
        position: absolute;
        bottom: 0;
        margin-left: 10px;
        max-width: 80%;
        z-index: 3;
        transition: margin-left .25s;
    }
    .testkrams:hover .cat-title {
        margin-left: 100px;
    }
    

    Working example

    【讨论】:

    • 那行得通。我到底为什么要使用兄弟选择器而不是子选择器...好吧我想我太累了...非常感谢您
    • 没问题,很高兴为您提供帮助。我还添加了一个单独使用 CSS 的示例,因为不一定需要 JS 代码来满足您的需求。
    • 是的,但这实际上不是我需要的,我只是出于解释的原因分解了我的代码;)
    猜你喜欢
    • 2012-08-28
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2013-06-16
    • 2014-01-02
    • 2015-06-03
    相关资源
    最近更新 更多