如何使用背景图像COVER在图像之间进行转换
How to make a transition between images using background-image COVER?
我想从我的网站上的背景图像进行转换。我用了JQuery。
但是,图像必须具有COVER效果。但我不能那样做。
我用这个例子来制作图片封面:如何使背景覆盖成为一个单独的div?
之后,我插入Jquery进行转换,但我不能使所有图像都达到覆盖效果。
如何制作?
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>
默认情况下,<img>
标记是不透明的。除非img
是透明的,否则您无法看到<img>
标记的background-image
,但即便如此,这也是非常罕见的。
通常的做法是在block
或inline-block
元素上设置background-image
属性,通常包含正常页面内容,这些内容将在背景图像上方呈现。
以下是一个简化脚本的尝试,假设这是期望的结果:
注意:您可以向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>
您不应该将背景图像放置在实际的图像元素上。我很难想象它应该是什么样子,但我认为将图像元素更改为div将是一个好的开始。
$('.conteudo').html('<img src='""+imgs[id]+"'" width='"50%'" height='"100%'" class='"bg'"/>');
$('.conteudo').fadeIn(500);",250);
类似
$('.conteudo').html('<div style='"background-image:' + imgs[id] + ';'" width='"50%'" height='"100%'" class='"bg'"></div>');
$('.conteudo').fadeIn(500);",250);
相关文章:
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- 无法在Cordova 4.0 ios中将jpg/jpeg图像转换为Base64
- ng文件上传获胜't将jpg图像转换为png
- 画架JS透视图像转换
- 将普通的 PNG 图像转换为 JSON 数据,然后存储到对象
- 如何使用javascript将图像转换为字符串
- 将SVG过滤器应用的图像转换为二进制或base64以保存在后端
- FS.Store 不可靠的GM图像转换
- javascript 将 IMG 标签中的图像转换为 标签中的实际链接图像
- 在JavaScript IE8中将图像转换为字节代码
- 如何在 JSP/JavaScript 中将图像转换为字节数组
- 如何在 JavaScript 中将图像转换为 base64
- 使用GraphicsMagick和Node.js将多个图像转换为多页pdf文件
- 图像转换的 URL
- 在javascript中将base64图像转换为文件,或者如何使用jquery ajax传递一个大的base64字符串
- 图像转换错误裁剪
- 在 JQuery 中将图像转换为 base64string
- 在 Javascript 中将图像转换为 RGB 数组
- 将相机捕获图像转换为 base64 - Ionic