Thay đổi background (fadeIn-fadeOut) với JQuery
JQuery:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript"> $(document).ready(function(){
/* phần tử cuối cùng của mảng hình background */
var lastEl = $("#body-bg div").length - 1;
/* add css */
$("#body-bg").css({"left":0,"top":0, "position":"fixed", "z-index":"-1"});
$("#body-bg div").css({"width":$(document).width(), "height":$(document).height(), "min-height":"600px", "min-width":"800px","position":"absolute"});
$("#body-bg > div").slice(1,lastEl).css("display","none"); // ẩn phan tu thu 2 tro di
/* Cho phan tu dau tien cua mang la background mac dinh */
var bgId = 0;
var preId = lastEl;
// sau 8 giay goi lai ham thay doi background 1 lan
setInterval(function()
{
if (bgId == lastEl)
{
bgId = 0;
preId = lastEl;
}
else
{
bgId++;
preId = bgId - 1;
}
$("#body-bg > div").slice(preId).fadeOut(6000); // hình hiện tại mờ dần trong 6 giây
$( "#body-bg > div ").slice(bgId).fadeIn(6000); // hình tiếp theo hiện lên trong 6 giây
}, 8000 // 8 giây gọi lại hàm này
);
})
</script>
<script type="text/javascript"> $(document).ready(function(){
/* phần tử cuối cùng của mảng hình background */
var lastEl = $("#body-bg div").length - 1;
/* add css */
$("#body-bg").css({"left":0,"top":0, "position":"fixed", "z-index":"-1"});
$("#body-bg div").css({"width":$(document).width(), "height":$(document).height(), "min-height":"600px", "min-width":"800px","position":"absolute"});
$("#body-bg > div").slice(1,lastEl).css("display","none"); // ẩn phan tu thu 2 tro di
/* Cho phan tu dau tien cua mang la background mac dinh */
var bgId = 0;
var preId = lastEl;
// sau 8 giay goi lai ham thay doi background 1 lan
setInterval(function()
{
if (bgId == lastEl)
{
bgId = 0;
preId = lastEl;
}
else
{
bgId++;
preId = bgId - 1;
}
$("#body-bg > div").slice(preId).fadeOut(6000); // hình hiện tại mờ dần trong 6 giây
$( "#body-bg > div ").slice(bgId).fadeIn(6000); // hình tiếp theo hiện lên trong 6 giây
}, 8000 // 8 giây gọi lại hàm này
);
})
</script>
HTML:
<div>Hello JQuery
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
This is the first slideshow.
</div>
<!-- background -->
<div id="body-bg">
<div style="background:url(images/bg-1.jpg) center top no-repeat;"></div>
<div style="background:url(images/bg-2.jpg) center top no-repeat;"></div>
<div style="background:url(images/bg-3.jpg) center top no-repeat;"></div>
</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
This is the first slideshow.
</div>
<!-- background -->
<div id="body-bg">
<div style="background:url(images/bg-1.jpg) center top no-repeat;"></div>
<div style="background:url(images/bg-2.jpg) center top no-repeat;"></div>
<div style="background:url(images/bg-3.jpg) center top no-repeat;"></div>
</div>



.jpg)
