【问题标题】:Height of the individual elements using jQuery height();使用 jQuery height() 的单个元素的高度;
【发布时间】:2023-03-18 19:17:01
【问题描述】:

我有一个无序列表。在每个列表项中,我都有包含文本内容的 div。因此,第一个列表项可能只有一行文本,而下一个甚至可能超过三行。

当我将鼠标悬停在每个列表项上时,我正在尝试使用 (jQuery API - height();) 作为 & 来获取这些 div 的高度。

问题在于,无论我将鼠标悬停在哪个列表项上,每次我都会得到第一个列表项的高度。

.quickHover 是我的 div 的类名。

这是我的代码:

var divHeight = $('.quickHover').height();

$('.catProductlist li').hover(

function () {
    $(this).find('.quickHover').animate({
        bottom: '0'
    }, 'fast');
}, function () {
    $(this).find('.quickHover').animate({
        bottom: '-' + divHeight + 'px'
    }, 'fast');
});

【问题讨论】:

    标签: jquery height


    【解决方案1】:

    http://api.jquery.com/height/

    获取匹配元素集中第一个元素的当前计算高度。

    为什么不在悬停函数中计算div的高度?

    【讨论】:

      【解决方案2】:

      +1 给 MatTheCat:

      $('.catProductlist li').hover(
      
      var divHeight = $(this).find('.quickHover').height();
      
      function () {
          $(this).find('.quickHover').animate({
              bottom: '0'
      }, 'fast');
      }, function () {
          $(this).find('.quickHover').animate({
          bottom: '-' + divHeight + 'px'
      }, 'fast');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多