简体   繁体   English

如何以编程方式旋转使用background-size:cover与jQuery的div的背景图像?

[英]How to rotate programmatically the background image of a div that uses background-size:cover with Jquery?

I've tried this in order to change the background image for a website but unfortunately it isn't working. 我已经尝试过此操作,以便更改网站的背景图片,但是很遗憾,它无法正常工作。 Sadly, the console doesn't give me any clue as to what's wrong. 可悲的是,控制台并没有给我任何提示。 So I've ran out of ideas. 所以我已经没有想法了。 Wish someone can help. 希望有人可以帮忙。 What I'm trying to do is to rotate the images on the background of the website based on an array of images. 我正在尝试做的是基于图像数组旋转网站背景上的图像。 I don't have a clue as to why it isn't working (perhaps the cover property of the background is getting in the way?). 我对它为什么不起作用一无所知(也许背景的cover属性正在挡住?)。 I've been hours working on this and changing the code so that it might work. 我已经花了很多时间来处理这个问题并更改代码,以便它可能起作用。 First I did it without populating an array with new Image() constructor, then I thought that should fix it, because otherwise the browser may not have a dom node to work with. 首先,我没有使用新的Image()构造函数填充数组,然后执行了此操作,因此我认为应该对其进行修复,因为否则浏览器可能没有dom节点可以使用。 But I'm still stuck. 但是我仍然被困住。 (Most of the code is based on an answer in this very website, here Here's the code: (大多数代码都是基于这个网站上的答案, 这里是代码:

$(document).ready(function() {
fondos = new Array('/Foto_Pimend/activos/flickr_foto_fondo_1.jpg',     '/Foto_Pimend/activos/flickr_foto_fondo_2.jpg',     '/Foto_Pimend/activos/flickr_foto_fondo_3.jpg');
cargar_fondos=new Array();
fondo_num = 1;
var i;
for(i=0; i<fondos.length; i++) {
cargar_fondos[i]=new Image();
cargar_fondos[i].src=fondos[i]; 
}
function rotarFondos() {
$('.fondo_intro').css({'background':'url("'+cargar_fondos[fondo_num++     %cargar_fondos.length].src+'") no-repeat 50% 100% cover'},'slow');
}
intId=setTimeout(rotarFondos, 10000);
});

The div I'm trying to change programmatically has this style: 我尝试以编程方式更改的div具有以下样式:

.fondo_intro {background: url(/Foto_Pimend/activos/flickr_foto_fondo_1.jpg) no-repeat;     width:100%; background-size:cover; background-position:50% 100%;position:fixed;     height:100%;}

I also tried linking the function to a click event that I use to change the content, like so: 我还尝试将函数链接到用于更改内容的click事件,如下所示:

$('.link_manejador').click(function() {
var content = $(this).attr('name')+'_template';
llamada_contenido(content); rotarFondos();  
});

That way instead of a rotating background I'd have a changing background depending on content. 这样,我将根据内容而不是旋转的背景。 But it doesn't work either, what's more frustrating, the console doesn't show any error or hint as to what's happening. 但这也不起作用,更令人沮丧的是,控制台未显示任何错误或提示。 I can see the images being created and the counter (namely fondo_num) incrementing, but nothing happens. 我可以看到正在创建的图像,并且计数器(即fondo_num)正在增加,但是什么也没有发生。 Even more to it: when I issue on the console: 更重要的是:当我在控制台上发布时:

$('.fondo_intro').css({'background-  image':'url(/Foto_Pimend/activos/flickr_foto_fondo_2.jpg'},'slow')

Nothing happens. 什么都没发生。 So I'm not understanding something, very clearly. 所以我不太清楚。

Remove "cover" from this line of code: 从以下代码行中删除“ cover”:

$('.fondo_intro').css({'background':'url("'+cargar_fondos[fondo_num++ %cargar_fondos.length].src+'") no-repeat 50% 100% cover '},'slow'); $('。fondo_intro')。css({'background':'url(“'+ cargar_fondos [fondo_num ++%cargar_fondos.length] .src +'”)无重复50%100% 覆盖 '},'slow');

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

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