简体   繁体   English

如何通过setInterval设置div元素的图像背景?

[英]How to set images background of div element via setInterval?

I implemented an iterator for my array, but I need to put array[i] as the background image of a div element. 我为我的数组实现了一个迭代器,但我需要将array[i]作为div元素的背景图像。 My iteration process completes but the background image is not working. 我的迭代过程完成但背景图像不起作用。 Can any experts solve this? 有专家能解决这个问题吗?

 //var imgobj = ['https://www.w3.org/html/logo/downloads/HTML5_Logo_512.png','https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTWOZVcvYs5dunAY_vSMxvFNKOvLWbn3RkHUM8SEU1cci9kNJEG','http://www.adservio.fr/img/logos2/jquery.jpg','https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKaIDe-P2BzZgWQLEOBo-LOTqduyPmBaRKhh0XcNG64fOTsm9e9A','http://www.unixstickers.com/image/data/stickers/python/python.sh.png']; var imgobj1 = ['IMG1', 'IMG2', 'IMG3', 'IMG4', 'IMG5']; var i = 0; function iterate() { setInterval(function() { $('.frame-div').html(imgobj1[i]); //$('.frame-div').css({'background',imgobj[i]}); i++ }, 2000); } $(document).ready(function() { iterate(); setInterval(function() { i = 0; }, 10000); }); 
 .frame-div { width: 300px; height: 300px; border: 1px solid #c1dbff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="frame-div"></div> 

The issue with your code is that the syntax for setting the background image is incorrect. 您的代码的问题是设置背景图像的语法不正确。 You need to use : to separate the key/value pair in an object. 您需要使用:分隔对象中的键/值对。 Alternatively, you can remove the braces and provide the values as separate parameters. 或者,您可以删除大括号并将值作为单独的参数提供。 The image URL should also be wrapped with url() . 图片网址也应该用url()包装。

Also, you can simplify the logic by using the modulo operator to cycle through the array, instead of resetting i to 0 every 10 seconds, which is unreliable at best. 此外,您可以通过使用模运算符循环数组来简化逻辑,而不是每10秒将i重置为0 ,这最多是不可靠的。 Try this: 尝试这个:

 var imgobj = ['https://www.w3.org/html/logo/downloads/HTML5_Logo_512.png', 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTWOZVcvYs5dunAY_vSMxvFNKOvLWbn3RkHUM8SEU1cci9kNJEG', 'http://www.adservio.fr/img/logos2/jquery.jpg', 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKaIDe-P2BzZgWQLEOBo-LOTqduyPmBaRKhh0XcNG64fOTsm9e9A', 'http://www.unixstickers.com/image/data/stickers/python/python.sh.png']; var i = 0; function iterate() { $('.frame-div').css('background-image', 'url("' + imgobj[i % imgobj.length] + '")'); i++; } $(document).ready(function() { iterate(); setInterval(iterate, 2000); }); 
 .frame-div { width: 300px; height: 300px; border: 1px solid #c1dbff; background-size: contain; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="frame-div"></div> 

Finally, note that I added background-size: contain; 最后,请注意我添加了background-size: contain; to the CSS so that the background image is automatically resized to fit within the bounds of the div. 到CSS,以便自动调整背景图像的大小以适应div的范围。 In the original example you would not see the whole image at all. 在原始示例中,您根本看不到整个图像。

As Rory McCrossan said you have invalid syntax for background image css. 正如Rory McCrossan所说,你的background image css语法无效。 And you don't need to use two setInterval s and this must be controlled by assigning to a variable. 并且您不需要使用两个setInterval ,这必须通过分配给变量来控制。 which helps to stop iteration when required. 这有助于在需要时停止迭代。

 var imgobj = ['https://www.w3.org/html/logo/downloads/HTML5_Logo_512.png','https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTWOZVcvYs5dunAY_vSMxvFNKOvLWbn3RkHUM8SEU1cci9kNJEG','http://www.adservio.fr/img/logos2/jquery.jpg','https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSKaIDe-P2BzZgWQLEOBo-LOTqduyPmBaRKhh0XcNG64fOTsm9e9A','http://www.unixstickers.com/image/data/stickers/python/python.sh.png']; var imgobj1 = ['IMG1', 'IMG2', 'IMG3', 'IMG4', 'IMG5']; var i = 0; var interval; function iterate() { interval = setInterval(function() { //$('.frame-div').html(imgobj[i]); $('.frame-div').css({'background': 'url(' + imgobj[i] + ')'}); i = i >= imgobj.length ? 0 : i + 1; }, 2000); } $(document).ready(function() { iterate(); //to stop iteration //clearInterval(interval) }); 
 .frame-div { width: 300px; height: 300px; border: 1px solid #c1dbff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="frame-div"></div> 

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

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