這並不明顯,你真的想要發生什麼,但如果期望的結果是,當你點擊任一鏈接它改變了背景圖像,淡入淡出過渡,我認爲它會更好通過更改背景的類來完成。我會做在這個叉形小提琴:
http://jsfiddle.net/4JGmK/1/
改變CSS:
#background {
opacity: 0;
margin: 0;
padding: 0;
z-index: -1;
position: absolute;
width: 100%;
height: 100%;
}
.background1 {
background-image: url("http://www.noupe.com/wp-content/themes/noupe/images/noupe-404.png");
}
.background2 {
background-image: url("http://upload.wikimedia.org/wikipedia/commons/7/7b/Sdf_losc-benf_22.11.05_-_4.JPG");
}
改變JS:
$(document).ready(function() {
$("#deneme").click(function(){deneme('background1', 'background2')});
$("#deneme2").click(function(){deneme('background2', 'background1')});
});
function deneme(newclass, oldclass) {
if ($('#background').hasClass(oldclass)) {
//fade out the other background first then fade the right one in
$('#background').animate({
opacity: 0
}, 3000, function() {
$('#background').removeClass(oldclass).addClass(newclass).animate({
opacity: 1
}, 3000);
});
} else if($('#background').hasClass(newclass)) {
//already got this background, don't do anything
return false;
} else {
//doesn't have either at the moment, fade in the right background
$('#background').addClass(newclass).animate({
opacity: 1
}, 3000);
}
}
的deneme功能我重構仍然感覺有點笨重,但它至少起作用。
問題的一部分可能是'#background2'不存在...... –
第一個鏈接在Chrome中運行,第二個鏈接未分配,您必須添加'$(「#deneme2」)。點擊(deneme2);'也改變「background2」爲只是「背景」建議 – Jacopofar
在CSS中已經有一個#background2 – aldimeola1122