简体   繁体   中英

how to change the background images with fade

I have the code below,and I want to images to be change with a fade,the images now are been replaced by the function 'changeBg',but they are just been replaced without fade.

how can I "merge" between the function that's changing the images and the function that in charge of the fade.

thanks.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7"> 
<title>none</title>
<link rel="stylesheet" type="text/css" href="Css/design.css" >
 <script src="query-1.4.4.js" type="text/javascript"></script> 
<script type="text/javascript">
$(document).ready(function() 
{
    $(document).ready(function() ({$('').fadeIn(1000);  
});
</script>
<script language="JavaScript">
function changeBg (color) {
  document.getElementById("wrapper").style.background="url(Images/"+color+".jpg) no-repeat";}
</script>
</head>
<body>
<div id="wrapper" class="wrapper">
     <div class="logo"><a href="http://mazonit.co.il/"><img border="0" src="Images/logo.png" ></a>
     </div>
        <div class="menu">
            <a href="#" id="arrowleft"><img border="0" src="Images/arrowleft.png" ></a>
            <img border="0" src="Images/black.png" onclick="changeBg(this.name);" name="black">
            <img border="0" src="Images/blue.png" onclick="changeBg(this.name);" name="blue">
            <img border="0" src="Images/fuksia.png" onclick="changeBg(this.name);" name="fuksia">
            <img border="0" src="Images/brown.png" onclick="changeBg(this.name);" name="brown">
            <img border="0" src="Images/orange.png" onclick="changeBg(this.name);" name="orange">
            <img border="0" src="Images/red.png" onclick="changeBg(this.name);" name="red">
            <img border="0" src="Images/grey.png" onclick="changeBg(this.name);" name="grey">
            <img border="0" src="Images/white.png" onclick="changeBg(this.name);" name="white">
            <a href="#" id="arrowright"><img border="0" src="Images/arrowright.png" ></a>
</div>
</body>
</html>

Thanks!

If I understand you correctly, you might be able to fade the background out, change it, then fade it back in again? Like this:

function changeBg (color) {
   $('#wrapper').fadeOut(1000,function(){
      $(this).css('background','url(Images/'+color+'.jpg) no-repeat').fadeIn(1000);
});
}

The cross-fading (fade out while fading in) is achieved in jQuery by having the statements straigh in line and not inside functions from previos animation.

Also, I understand that you want JUST THE BACKGROUND IMAGE to fadeout and fadein; not the actual contents of the page.

To achieve that, make your background image a separate item altogether but inside of the main div you have backgrounded:

<div id='wrapper' style='position:relative' > <!-- must be relative -->

 <img id='bg1' src='initial.image' style='position:absolute; top:0; left:0; 
  width:100%; height:100%; opacity:0.2; display:none; ' />

 <img id='bg2' src='initial.imageTWO' style='position:absolute; top:0; left:0; 
  width:100%; height:100%; opacity:0.2; display:none; ' />

</div>

function changeBackground(which) {
    $('#bg'+which).attr('src','current-image.xxx').fadeOut('slow');    
    which = (which = 1) ? 2 : 1;
    $('#bg'+which).attr('src','next-image.xxx').fadeIn('slow');
    setTimeout('changeBackground('+which+')',2000);
}    

$(document).ready( function () {
    $('#bg1').attr('src','image-name.xxx').fadeIn('slow');
    setTimeout('changeBackground(1)',2000); //every 2 seconds?
    . ......
});

In case you have various images for the "slide show", you might want to add a random
number generation for which picture to show next.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM