2011-06-09 48 views
0

当用户将鼠标悬停在上面时,我正在使用以下函数来缩放每个图像。如何使用此功能隐藏某些元素?

$(function() { 
    var imgScale = 4; 
    $("img").each(function() { 
     $.data(this, 'size', { 
      width: $(this).width(), 
      height: $(this).height() 
     }); 
    }).hover(function() { 
     $(this).stop().animate({ 
      height: $.data(this, 'size').height * imgScale, 
      width: $.data(this, 'size').width * imgScale 
    }); 
    }, function() { 
     $(this).stop().animate({ 
      height: $.data(this, 'size').height, 
      width: $.data(this, 'size').width 
     }); 
    }); 
}); 

页面的基本结构如下:

<div id="product-container" style="width: 100%; overflow: auto;"> 
    <div id="products" style="width: 50%; display: block; float: left;"> 
     <h2>Products</h2> 

     <div class="product"> 
      <div class="image"> 
       <a href="page.php?product=123" title="Product 123"> 
        <img width="80" height="60" src="product123.jpg" /> 
       </a> 
      </div> 

      <div class="link"> 
       <p><a id="link-123" href="page.php?product=123" title="Product 123">Product 123</a></p> 
      </div> 
     </div> 

     <div class="product"> 
      <div class="image"> 
       ... 
      </div> 

      <div class="link"> 
       ... 
      </div> 
     </div> 

     ... 

    </div> <!-- Close 'products' div --> 

使用jQuery,我想隐藏所有当前没有悬停在产品<p><a id="link-123"...> ... </a></p>标签。

除此之外,我想在展示产品名称的展开图片上创建文字叠加层(例如“Product 123”)。这可以从图像本身的title属性中提取。

这是一个非常早期的工作,因为你可能已经猜到我是jQuery的新手了。我只是想抓住jQuery的一些基本功能。

回答

0

拳你要隐藏的各个环节与

$('.link').hide(); 

在你的悬停功能,你必须找到你的形象正确<div class="link">。您可以通过树加紧你的<div class="product">和向下<div class="product">以做到这一点(也有其他可能的方式来实现这一目标:

$("img").parents('.product').find('.link'); 

现在隐藏或显示该元素

你完整的代码可以样子。

$(function() { 
    $(".link").hide(); 
    var imgScale = 4; 
    $("img").each(function() { 
     $.data(this, 'size', { 
      width: $(this).width(), 
      height: $(this).height() 
     }); 
    }).hover(function() { 
     $(this).stop().animate({ 
      height: $.data(this, 'size').height * imgScale, 
      width: $.data(this, 'size').width * imgScale 
     }).parents('.product').find('.link').show(); 

    }, function() { 
     $(this).stop().animate({ 
      height: $.data(this, 'size').height, 
      width: $.data(this, 'size').width 
     }).parents('.product').find('.link').hide(); 
    }); 
}); 
+0

谢谢你的背景资料,但并不完全是我所期待的。我想所有图像和所有链接是在页面加载可见的,那么当你将鼠标悬停在图片中的一个,所有_other_图片的标题都消失了ld然后在鼠标移出图像时重新出现。这个新代码会发生什么,所有的链接都会在页面加载时隐藏,然后当每个图像被悬停时,它们就会出现(并持续存在)。对不起,如果我没有足够清楚地解释原来的问题! – Chris 2011-06-09 15:01:51

+0

我认为我现在拥有它 - 而不是在直接父级中找到“.link”,我在“#products”div(“.product”div的父级)中找到了“.link”,并将“show “和”隐藏“在一起。 – Chris 2011-06-09 15:15:56

+0

对不起,如果我误解了你的问题,但罚款,如果我的答案帮助仍然。 – DanielB 2011-06-09 15:25:43