当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Add fade to image transistion

问题描述:

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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: