[英]I need help switching background images with jQuery
I have an unordered list of img url's that are generated with from a custom post type in wordpress. 我有一个从Wordpress中的自定义帖子类型生成的img URL的无序列表。
I have a slider and am using the number of slides to determine which Image url I want as the background for my element. 我有一个滑块,并且正在使用幻灯片的数量来确定要将哪个图像URL作为元素的背景。
Sample of generated list: 生成列表的样本:
<ul class="tes-image-links">
<li>http://img-url1</li>
<li>http://img-url2</li>
<li>http://img-url3</li>
</ul>
Sample of my jQuery 我的jQuery示例
$('.cycle-slideshow').find('.cycle-slide').each(function(i){
if ( $(this).hasClass('cycle-slide-active') ){
var apimglink = $('.tes-image-links').children('li').eq(i).text();
$('.ap-testimonial.img-back').css("background" , "'url('"+apimglink+"') !important'" );
}
});
When I use console.log()
it spits out the right text that is in the <li>
tag, but I can't seem to get this to work. 当我使用console.log()
它会吐出<li>
标记中的正确文本,但是我似乎无法使其正常工作。
Remove single quotes and !imprtant from background properties. 从背景属性中删除单引号和!imprtant。 Also dont forget to wrap your function in a DOM ready function 也不要忘记将函数包装在DOM ready函数中
$(document).ready(function(){
$('.cycle-slideshow').find('.cycle-slide').each(function(i){
if ( $(this).hasClass('cycle-slide-active') ){
var apimglink = $('.tes-image-links').children('li').eq(i).text();
$('.ap-testimonial .img-back').css("background" , "url('"+apimglink+"')" );
}
});
})
The previous code is giving me issues and I could not get it to show the image with .each()
. 先前的代码给了我一些问题,但是我无法使用.each()
来显示图像。
Using a for-loop I was able to get the Section to use the URL as a background based on the slide number: 使用for循环,我可以根据幻灯片编号获取Section以将URL用作背景:
var cycleSlide = $('.cycle-slideshow').find('.cycle-slide');
for (i = 0; i < cycleSlide.length; i++) {
if (cycleSlide.eq(i).hasClass('cycle-slide-active')){
var apimglink = $('.tes-image-links').children('li').eq(i).text();
apimglink = apimglink.replace("http://localhost", "");
$('.ap-testimonial').css('background-image', 'url(' + apimglink + ')');
console.log(apimglink);
} //End If
}; //End For
Thank you all for your help. 谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.