简体   繁体   English

仅更改背景图像平滑且前面没有文本

[英]change only the background-image smooth without text in front of it

I have a website, where I want to change between images in the background very smoothly. 我有一个网站,我想在其中非常平滑地在背景图像之间进行切换。 This is my actual javaScript-code for it: 这是我实际的javaScript代码:

var bg=[
        'images/best.jpg',
        'images/61182.jpg',
        'images/bg.jpg'
       ];

   $('._container-1').css('background-image','url('+bg[2]+')');


window.setInterval(
    function(){
        img=bg.shift();bg.push(img);
        document.getElementsByClassName('_container-1')[0].style.backgroundImage='url('+img+')';
    },
    10000
);

Now, I want to change the images very slowly. 现在,我想非常缓慢地更改图像。 I have tried a lot with jQuery-fadeIn/fadeOut-methods like this: 我已经对jQuery-fadeIn / fadeOut-method进行了很多尝试,例如:

window.setInterval(
        function(){
            img=bg.shift();
            bg.push(img);

        $('._container-1').fadeOut(600, function() {
            $('._container-1').css('background-image','url('+img+')');
            $('._container-1').fadeIn(600);
        });

    },
    17000
);

The problem is, that there are buttons and text in the container and they changes with the images. 问题是,容器中有按钮和文本,它们随图像而变化。 I want that the text and buttons are in the front all the time, only the background should fadeIn/fadeOut. 我希望文本和按钮始终在最前面,只有背景应该淡入/淡出。 My english is not perfect, I hope you understand my problem. 我的英语不是很完美,希望您能理解我的问题。

Can somebody help me please? 有人可以帮我吗?

nina_berlini nina_berlini

I have uses 2 elements as background to achieve the effect. 我使用2个元素作为背景来达到效果。 Also check demo on https://jsfiddle.net/n380u3cy/1/ 还可以在https://jsfiddle.net/n380u3cy/1/上查看演示

HTML: HTML:

<div class="container">
  <div class="background"></div>
  <div class="background"></div>
  <button>
    Test button
  </button>
</div>

CSS: CSS:

.container { position: relative; line-height: 100px; }
  .container > .background,
  .container > .background { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-size: contain; z-index: 0; }

  .container > *:not(.background) { position: relative; z-index: 1; }

Javascript: 使用Javascript:

var bg=[
          'images/best.jpg',
          'images/61182.jpg',
          'images/bg.jpg'
       ];
var Transition = 1000;

$('.background').css('background-image','url('+bg[bg.length - 1]+')');
window.setInterval(
  function() {
    img=bg.shift();
    bg.push(img);

    var $Backgrounds = $('.background');
        $Backgrounds.eq(1).hide(0).css({
        'background-image': 'url('+img+')'
    }).fadeIn(Transition * .9);

    $Backgrounds.eq(0).show(0).fadeOut(Transition, function(){
        $(this).show(0).css({
        'background-image': 'url('+img+')'
      });
      $Backgrounds.eq(1).hide(0);
    });
  }, 2000
);

Make a wrapper and include both the background div and button div inside it with position absolute and the following CSS styles. 制作一个包装,并在其中包含背景div和按钮div以及绝对位置和以下CSS样式。 This way you can control and animate the background separately from the buttons. 这样,您可以独立于按钮控制和设置背景动画。

 var bg = [ 'https://placehold.it/1001x201', 'https://placehold.it/1002x202', 'https://placehold.it/1003x203' ]; $('._container-1').css('background-image', 'url(' + bg[2] + ')'); window.setInterval( function() { img = bg.shift(); bg.push(img); document.getElementsByClassName('_container-1')[0].style.backgroundImage = 'url(' + img + ')'; }, 10000 ); window.setInterval( function() { img = bg.shift(); bg.push(img); $('._container-1').fadeOut(600, function() { $('._container-1').css('background-image', 'url(' + img + ')'); $('._container-1').fadeIn(600); }); }, 17000 ); 
 .wrapper { position: relative; width: 100%; height: 200px; } ._container-1 { position: absolute; width: 100%; height: 100%; top: 0; left: 0; background-size: cover; background-position: top center; } .buttons { position: absolute; width: 100%; text-align: center; bottom: 0; left: 0; } button { background: red; padding: 5px 10px; border: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="_container-1"></div> <div class="buttons"> <button type="button"> Button 1 </button> <button type="button"> Button 2 </button> </div> </div> 

thank you for your great solution. 感谢您的出色解决方案。 I am not well familiar with jQuery and have a question about your code: 我对jQuery不太熟悉,对您的代码有疑问:

  $Backgrounds.eq(1).hide(0).css({
    'background-image': 'url('+img+')'
}).fadeIn(Transition * .9);

means it that the second "background-div" first hides, then get a new background-image and after that it ist fadeIn? 表示第二个“ background-div”先隐藏,然后获取新的背景图像,然后它会淡入淡出? And means hide(0) that it immediately hides? 并表示它立即隐藏的hide(0)?

nina_berlini nina_berlini

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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