我一直在尋找爲什麼我的代碼不會在懸停時將元素設置爲「不透明:1」,但會旋轉元素。下面是html和js。jQuery不動畫不透明但會旋轉元素懸停
這樣的想法,如果一個元素是未選中的(具有類rwunselected),那麼它應該執行懸停功能。
<table cellpadding="0" cellspacing="10">
<tr>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/cellists.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/elegance.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/musicrooms.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/absolution.png" border="0" /></td>
</tr>
<tr>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/helpmankind.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/liva.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/anthonybrown.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/blairjohnstone.png" border="0" /></td>
</tr>
<tr>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/questiventi.png" border="0" /></td>
<td width="135" align="center"><img class="recentworkitem rwunselected" src="images/recentwork/smallimages/surveycentre.png" border="0" /></td>
</tr>
</table>
的JavaScript
//set opacities
$('.recentworkitem').css('opacity','.15');
//hover effect on all items with class recentworkitem
$('.recentworkitem').hover(function(){
if($(this).hasClass('rwunselected')){
$(this).stop().animate({opacity:'1'},{duration:300})
$(this).stop().rotate({animateTo:-90, duration:300})
}
},
function(){
if($(this).hasClass('rwunselected')){
$(this).stop().animate({opacity:'.15'},{duration:300})
$(this).stop().rotate({animateTo:0, duration:300})
}
});
//click function
$('.rwunselected').click(function(){
//swap image opacities and classes
$(this).removeClass('rwunselected');
$(this).animate({opacity:'1'},{duration:300});
$('.rwselected').addClass('rwunselected');
$('.rwselected').animate({opacity:'.15'},{duration:300});
$('.rwselected').removeClass('rwselected');
$(this).addClass('rwselected');
//rotate the old selected item back to 0 degrees
$('.rwunselected').rotate({animateTo:0, duration:300})
});
首先檢查您的瀏覽器是否支持不透明度。這是一個CSS3功能。 – defau1t 2013-03-14 10:54:21
很多錯別字。不要忘記你的好友,分號';' – Bigood 2013-03-14 10:55:20