简体   繁体   English

如何使用背景图像COVER在图像之间进行转换?

[英]How to make a transition between images using background-image COVER?

I want to make a transition from background images on my site. 我想从我网站上的背景图片过渡。 I used JQuery for it. 我使用了JQuery。

However, the images have to be with the COVER effect. 但是,图像必须具有覆盖效果。 But I can not do that. 但是我做不到。

I used this example for make the image cover: How to make a background cover a separate div? 我使用此示例制作图像封面: 如何使背景封面成为单独的div?

After, I insert the Jquery to make a transition, but I can't make all images are to cover effect. 之后,我插入Jquery进行过渡,但不能使所有图像都覆盖效果。

How to make this? 怎么做?

  var id = 0; var imgs = new Array(); imgs[0] = "http://www.imagenstop.blog.br/wp-content/gallery/imagens-de-fundo/Imagem-de-Plano-de-Fundo.jpg"; imgs[1] = "http://wallpaper.ultradownloads.com.br/56263_Papel-de-Parede-Fundo-de-Mosaico-Azul_1920x1200.jpg"; imgs[2] = "http://www.wallpapersxl.com/wallpapers/2560x1600/fundo-de-tela-azul/450289/fundo-de-tela-azul-neon-papel-parede-450289.jpg"; //ADD IMAGES function troca() { if (id < imgs.length - 1) { id++; } else { id = 0; } $(".conteudo").fadeOut(250); setTimeout("$('.conteudo').html('<img src="\\" + imgs[id] + "\\" width=\\"50%\\" height=\\"100%\\" class=\\"bg\\"/>');$('.conteudo').fadeIn(500);", 250); } var segundos = 3; setInterval("troca();", segundos * 2000); 
 .conteudo { width: 100%; height: 100%; background: url('http://www.imagenstop.blog.br/wp-content/gallery/imagens-de-fundo/Imagem-de-Plano-de-Fundo.jpg') no-repeat; } img.bg { background-image: url('http://www.imagenstop.blog.br/wp-content/gallery/imagens-de-fundo/Imagem-de-Plano-de-Fundo.jpg'); */ background-size: cover; position: fixed; top: 0; right: 0; bottom: 0; width: 90%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="visible-sm visible-md visible-lg conteudo" style="width:100%; height:100%;"></div> 

By default, <img> tags are opaque. 默认情况下, <img>标签是不透明的。 You cannot see the background-image of an <img> tag unless the img is transparent, but even so, it is very uncommon. 除非img是透明的,否则您将看不到<img>标记的background-image ,但是即使如此,它也是非常不常见的。

The common practice is to set a background-image property on a block or inline-block element, usually holding the normal page content, content that will be rendered above the background image. 通常的做法是在block或行inline-block元素上设置background-image属性,通常包含常规页面内容,这些内容将在背景图像上方呈现。

Here is an attempt to streamline your script, assuming this is the desired outcome: 假设这是期望的结果,这是尝试简化脚本的尝试:

Note: you can add as many images to the imgs array. 注意:您可以向imgs数组添加尽可能多的图像。

 var the_target = '.conteudo', imgs = [ "http://www.imagenstop.blog.br/wp-content/gallery/imagens-de-fundo/Imagem-de-Plano-de-Fundo.jpg", "http://www.pageresource.com/wallpapers/wallpaper/vintage-blue-rishi-agarwal_2141757.jpg", "http://www.wallpapersxl.com/wallpapers/2560x1600/fundo-de-tela-azul/450289/fundo-de-tela-azul-neon-papel-parede-450289.jpg" ], addBackground = function(i) { if (i == imgs.length) i = 0; var bgImage = $(the_target + ' .bgImage'); bgImage.eq(0).fadeOut('slow', function() { $(this).remove(); }); $(the_target).append($('<div />').css({ 'background-image': 'url("' + imgs[i] + '")' }).addClass('bgImage')); setTimeout(function() { addBackground(i + 1); }, 2000); }; addBackground(0); 
 body { margin: 0; } .conteudo { background-color: rgba(0, 0, 0, .35); color: white; padding: 3px 20px; position: relative; width: 90%; margin: 0 auto; box-sizing: border-box; } .conteudo>.bgImage { position: absolute; min-width: 100%; min-height: 100%; background-size: cover; background-position: center center; background-attachment: fixed; z-index: -1; top: 0; left: 0; } .conteudo>.bgImage:last-child { z-index: -2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="conteudo"> <p>On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain.</p> <p>These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted.</p> <p>On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain.</p> <p>These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted.</p> </div> 

You shouldn't be placing background images on actual image elements. 您不应该在实际的图像元素上放置背景图像。 I'm having a hard time picturing what this is supposed to look like, but I think changing image element to a div would be a good start. 我很难想象这应该是什么样子,但是我认为将图像元素更改为div是一个好的开始。

$('.conteudo').html('<img src=\""+imgs[id]+"\" width=\"50%\" height=\"100%\" class=\"bg\"/>');
$('.conteudo').fadeIn(500);",250);

to something like

$('.conteudo').html('<div style=\"background-image:' + imgs[id] + ';\" width=\"50%\" height=\"100%\" class=\"bg\"></div>');
$('.conteudo').fadeIn(500);",250);

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

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