【问题标题】:jquery selectorjQuery 选择器
【发布时间】:2011-09-19 23:36:20
【问题描述】:

我无法在此处获取我的选择器。这个函数的作用是

  1. 选择图像并在悬停时改变不透明度
  2. 在悬停时选择定位标记并向上滑动

如果您可以帮助我选择特定于悬停在特定图像上的锚标记,那就太好了。我尝试了很多不同的方法,但我的语法不是很好。 谢谢!

$(document).ready(function() {
        $('.workitem_photo > img').each(function() {
            $(this).hover(function() {
                $(this).stop().animate({ opacity: 0.8 }, 500);
                $(this > '.workitem_projectdetails').show('500')
            },
           function() {
               $(this).stop().animate({ opacity: 1.0 }, 500);
               $(this > '.workitem_projectdetails').hide('500')
           });

        });
    });

这里是html:

<div class="workitem branding">
   <div class="workitem_photo"><img src="<?php echo home_url( '/wp-content/themes/po/' ); ?>images/workitem/branding-1000ikc.gif" alt="1000 Islands Kayaking" /></div>
   <div class="workitem_title">1000 Islands Kayaking Identity</div>
   <div class="workitem_description">Brand development for a Kayak tour operator.</div>
   <a class="workitem_projectdetails" href="#" style="display:none;">View Project Details</a>
</div>

这是一个工作版本my test site

【问题讨论】:

    标签: jquery


    【解决方案1】:

    我会使用这样的东西......

    $(document).ready(function() {
        $('.workitem').each(function() {
            var projectDetails = $(this).find('> .workitem_projectdetails');
    
            $(this).find('.workitem_photo > img').hover(function() {
                $(this).fadeTo(500, .8);
                projectDetails.show('500');
            }, function() {
                $(this).fadeTo(500, 1);
                projectDetails.hide('500');
            });
        });
    });
    

    jsFiddle.

    【讨论】:

    • 非常轻微,但我认为.childrenfind("&gt; 更容易理解。不确定是否有任何性能差异。
    • @kingjiv 是的,您可以轻松替换它。 children() 的性能可能更好,因为它不需要解析 &gt;
    【解决方案2】:

    当您处于悬停功能时,$(this) 指的是 img,而不是 div。试试这个:

    $(document).ready(function() {
        $('.workitem_photo > img').each(function() {
            $(this).hover(function() {
                $(this).stop().animate({ opacity: 0.8 }, 500);
                $(this).parent().parent().find('.workitem_projectdetails').show('500')
            },
           function() {
               $(this).stop().animate({ opacity: 1.0 }, 500);
               $(this).parent().parent().find('.workitem_projectdetails').hide('500')
           });
    
        });
    });
    

    这是jsfiddle

    【讨论】:

    • 我认为 DOM 的整体结构存在问题,因为其目的是能够单击锚标记,该标记应使用 z-index 覆盖图像。我遇到了一个问题,一旦您将鼠标悬停在锚标记上,它就会消失,因为您不再悬停图像。有没有办法设置它,以便我们可以为多个选择器发送悬停功能?
    • 从头开始,当我添加一个双选择器时,它只是不让另一个完成 .show 功能。 jsfiddle.net/fjy4J/14
    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多