2016-04-30 9 views
0

divの背景イメージをフェードインしようとしています。イメージがそのdiv内のコンテンツをアピールすると、コンテンツのフェード効果を止める方法も消えます。フェードバックグラウンドイメージはそのdiv内のコンテンツをフェードアウトします

JS Fiddle

HTML:

<div id="background">adsdsa</div> 

JS:

var bgArr = ["http://malsup.github.io/images/p1.jpg", "http://malsup.github.io/images/p1.jpg", "http://malsup.github.io/images/p2.jpg"]; 
var i = 0; 

// Start the slide show 
var interval = self.setInterval(swapBkgnd, 5000) 

function swapBkgnd() { 
    if (i > (bgArr.length - 1)) { 
    i = 0 
    $('#background') 
    .animate({opacity: 0}, 'slow', function() { 
     $(this) 
      .css({'background-image': "url(" + bgArr[i] + ")"}) 
      .animate({opacity: 1}); 
    }); 
    } else { 
    $('#background') 
    .animate({opacity: 0}, 'slow', function() { 
     $(this) 
      .css({'background-image': "url(" + bgArr[i] + ")"}) 
      .animate({opacity: 1}); 
    }); 
    } 
    i++; 
}; 

CSS:

#background { 
    position: absolute; 
    min-height: 100%; 
    width: 100%; 
    height: auto; 
    top: 0; 
    left: 0; 

    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    -o-background-size: cover; 
    background-size: cover; 
} 

答えて

0

あなたはその後、ヨーヨーをfaddingさdiv内部の内容を置く場合コンテンツがフェードアウトするのを止めることはできません。 divの外にコンテンツを入れてposition: absolute;を使用して整列すれば、あなたの望みの目標を達成することができます。デモを見て -

var bgArr = ["http://malsup.github.io/images/p1.jpg", "http://malsup.github.io/images/p1.jpg", "http://malsup.github.io/images/p2.jpg"]; 
 
var i = 0; 
 

 
// Start the slide show 
 
var interval = self.setInterval(swapBkgnd, 1000) 
 

 
function swapBkgnd() { 
 
    if (i > (bgArr.length - 1)) { 
 
    i = 0 
 
    $('#background') 
 
     .animate({ 
 
     opacity: 0 
 
     }, 'slow', function() { 
 
     $(this) 
 
      .css({ 
 
      'background-image': "url(" + bgArr[i] + ")" 
 
      }) 
 
      .animate({ 
 
      opacity: 1 
 
      }); 
 
     }); 
 
    } else { 
 
    $('#background') 
 
     .animate({ 
 
     opacity: 0 
 
     }, 'slow', function() { 
 
     $(this) 
 
      .css({ 
 
      'background-image': "url(" + bgArr[i] + ")" 
 
      }) 
 
      .animate({ 
 
      opacity: 1 
 
      }); 
 
     }); 
 
    } 
 
    i++; 
 
};
#background { 
 
    position: absolute; 
 
    min-height: 100%; 
 
    width: 100%; 
 
    height: auto; 
 
    top: 0; 
 
    left: 0; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 
span { 
 
    position: absolute; 
 
    top:0; 
 
    left:0; 
 
    z-index: 2; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="background"></div> 
 
<span>adsdsa</span>

関連する問題