我在这里找到了我想要完成的工作的 JSfiddle:http: //jsfiddle.net/gdoron/YwKVt/3/
我有一个 foreach 循环:
<div class="box-product">
<?php
$counter = 0;
?>
<?php foreach ($products as $product) { ?>
<?php $counter++; ?>
<div class="feature prod-<?php echo $counter ?>">
<?php if ($product['thumb']) { ?>
<div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
<?php } ?>
<div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
<?php if ($product['price']) { ?>
<div class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
<?php } ?>
</div>
<?php } ?>
<?php if ($product['rating']) { ?>
<div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
<?php } ?>
<div class="cart"><input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" /></div>
</div>
<?php } ?>
</div>
输出是这样的:
<div class="box-product">
<div class="feature prod-1">
<div class="feature prod-2">
<div class="feature prod-3">
<div class="feature prod-4">
</div>
我试图通过添加到我的标题来实现该 js fiddle 的解决方案:
<script type="text/javascript">
$('.feature').hover(function(){
$(this).siblings().addClass('fade');
}, function(){
$(this).siblings().removeClass('fade');
});
</script>
并添加了课程:
.fade{opacity:.5;}
没有运气。有没有更好的方法来实现这一点?