2011-11-11 51 views
0

全部。 我有。mouseout set previus返回div的前一個bg圖像

<div id="imagecontainer" class="header-image-container">&nbsp;</div> 

BG圖像在css中根據父類指定。

.category-1 #imagecontainer { 
background: url(_/images/1.jpg); 
} 

而且我有菜單。我想要將BG圖像ommouse over,並按照父類在css中爲此頁指定返回圖像。我認爲這可能是真正的使用JQuery。例如,我們打開了3類頁面,並將鼠標移動到1類菜單項上,並在#imagecontainer中看到catefory-1 BG圖像,然後我們將鼠標移出,再次看到3類BG圖像。

回答

0

我認爲這會做你想要的:

$('#menu').mouseenter(function() { 
    $('#imagecontainer').css({'background':'blue'}); 
}).mouseleave(function() { 
    $('#imagecontainer').removeAttr('style'); 
}) 

這裏你可以看到它在行動:http://jsfiddle.net/Cdzk8/

如果您對您的imagecontainer其他內嵌樣式,它也將刪除這些在mouseleave上。在那種情況下,你會做更多的事情,比如mblase75推薦的東西。

+0

謝謝。它適用於我的項目=)。但我真的很傷心,因爲我無法工作mblase75提出的方法。 – redbass

0

將當前背景圖像存儲爲data,然後將其交換出去,並在需要將其交換回來時從其中檢索。

$('#imagecontainer').mouseover(function() { 
    $(this).data('bgimg') = $(this).css('background-image'); 
    $(this).css('background-image','url(my/new/url.jpg)'); 
}).mouseout(function() { 
    $(this).css('background-image', $(this).data('bgimg')); 
}); 
+0

謝謝,但它並沒有幫助我=(這是行不通的 – redbass

+0

你在多個項目上使用相同的#imagecontainer ID?這是不允許的,並可以解釋你的問題 – Blazemonger

+0

我嘗試你的解決方案http: //jsfiddle.net/S7xKn/3/和nothig作品...直到我刪除腳本的第二行 – redbass