【问题标题】:jQuery hover method fires on multiple elementsjQuery悬停方法在多个元素上触发
【发布时间】:2016-12-08 22:54:33
【问题描述】:

我有一个针对多个图像的悬停方法,当您悬停图像时,它应该只在该悬停图像上添加一个预览图标,但它发生在每个元素上,我是 Javascript/jQuery 的新手,不能找个方法,谁能帮帮我?

这是我的实际 jQuery 代码:

// Make images smaller
$('.card').find('p > img').css({
  'width': '160px',
  'height': '118px'
}).parent().css('text-align', 'center');

// Add preview icon into DOM
$('.card').find('p:last-child').prepend("<img class='preview-icon' src='http://www.iconsfind.com/wp-content/uploads/2015/11/20151116_564931d273df6.png' width='80' height='80' />");

// Add style to our preview image
$('.preview-icon').css({
  'position' : 'absolute',
  'z-index' : '99999',
  'left' : '50%',
  'top' : '50%',
  'bottom' : 'auto',
  'right' : 'auto',
  'transform' : 'translateX(-50%) translateY(-50%)'
}).hide();

// On hover add/remove style
$('.card').find('p > img').hover(function() {
  $('.card').find('p > img').css({
    'opacity' : .2,
    'transition' : 'all 0.2s',
    '-webkit-transition' : 'all 0.2s',
    'cursor' : 'pointer'
  });

  $('.preview-icon').show();
}, function() {
  $('.card').find('p > img').css({
    'opacity' : 1,
  });

  $('.preview-icon').hide();
});

更新 v1

所以现在它正在工作,但是如果您将鼠标悬停在图像上,然后将鼠标悬停在出现的预览图标上,它不会绑定图像,它们就像两个不同的图像一样工作(尝试使用上面网站内的 Chrome 开发工具的代码):

// Make images smaller
$('.card').find('p > img').css({
  'width': '160px',
  'height': '118px'
}).parent().css('text-align', 'center');

// Add preview icon into DOM
$('.card').find('p:last-child').prepend("<img class='preview-icon' src='http://www.iconsfind.com/wp-content/uploads/2015/11/20151116_564931d273df6.png' width='80' height='80' />");

// Add style to our preview image
$('.preview-icon').css({
  'position' : 'absolute',
  'z-index' : '99999',
  'left' : '50%',
  'top' : '50%',
  'bottom' : 'auto',
  'right' : 'auto',
  'transform' : 'translateX(-50%) translateY(-50%)'
}).hide();

// On hover add/remove style
$('.card').find('p > img').hover(function() {
  $(this).css({
    'opacity' : .2,
    'transition' : 'all 0.2s',
    '-webkit-transition' : 'all 0.2s',
    'cursor' : 'pointer'
  });

  $(this).closest('.card').find('.preview-icon').show();
}, function() {
  $(this).css({
    'opacity' : 1
  });

  $(this).closest('.card').find('.preview-icon').hide();
}); 

谢谢!

【问题讨论】:

    标签: jquery jquery-hover


    【解决方案1】:

    您正在悬停处理程序中执行以下操作

    $('.card').find('p > img')
    

    这是重新查找所有图像,而不是使用悬停或关闭的图像。不要重复这个选择器,而是使用$(this)

    看起来您正在将预览图标附加到卡片上。所以要访问相关的预览图标。

    $(this).closest('.card').find('.preview-icon').hide();
    

    【讨论】:

    • 嗨@Taplar,感谢您的评论,我尝试使用 $(this) 并且效果很好,只是它破坏了我的预览图标图像。
    • 当您将图像悬停时,它会出现一个我通过 jQuery 添加的预览图标,使用 $(this) 时,它会正确应用于每个元素,但该预览图标无法正常工作,我需要添加一个 $ (this).parent().parent().show();但以一种奇怪的方式工作。感谢您的帮助!
    • 您可以尝试在主题网站中添加此代码,然后将鼠标悬停在图像上,当出现预览图标时,它会开始闪烁(我正在主题中添加新代码)。
    • hover(),如果你读过 API,绑定方式和 mouseenter 和 mouseleave 一样 api.jquery.com/hover
    • 使用 "$(this).css({" 和 "$(this).closest('.card').find('.preview-icon').show();" 目标正确地,闪烁消失了,但它没有“绑定”,就像您可以将两者都悬停而不是像一个图像一样工作。
    【解决方案2】:

    您正在定位每个 .card 的后代 p &gt; img 元素,而不仅仅是悬停的那个。

    $('.card').find('p &gt; img').css

    应该是

    $(this).css

    您可以阅读更多信息here

    【讨论】:

    • 嗨@Ryan Gee,感谢您的评论,我尝试使用 $(this) 并且效果很好,只是它破坏了我的预览图标图像。
    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2012-10-30
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多