问题描述:

I have a changing background image that currently uses this script. I am not a javascript expert and would like to add a fade effect as it transitions from one slide to the next. What would I need to add?

function changeBgImage (image, id) {

var element = document.getElementById(id);

element.style.backgroundImage = "url("+image+")";

}

Thanks as always!

网友答案:

You'll have to include jquery.js and use element.fadeOut() before changing the background and element.fadeIn() after changing it, something like this:

var element = document.getElementById(id);
$('#' + id).fadeOut();
element.style.backgroundImage = "url("+image+")";
$('#' + id).fadeIn();

Include jquery in the head of your html document:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
相关阅读:
Top