我正在使用Cycle2插件(http://jquery.malsup.com/cycle2/)来访问网站上的某些滑块,并且需要从当前幻灯片中包含的每个图像中检测内联样式,然后传递到父div,以便用每张幻灯片更改滑块周围区域的背景颜色。
滑块的输出如下:
<div class="cycle-slideshow" data-cycle-fx="fade" data-cycle-timeout="0" data-cycle-slides="div" data-cycle-prev="#previous" data-cycle-next="#next" data-cycle-swipe="true" data-cycle-pager="#no-template-pager" data-cycle-pager-template="" style="position: relative;">
<div class="cycle-sentinel cycle-slide" style="position: static; top: 0px; left: 0px; z-index: 100; opacity: 1; visibility: hidden; display: block;">
<a href="#"><img style="background-color: #ff0000;" src="http://cambelt.co/1070x400/Rotator1?color=cccccc,ffffff" alt="fsadfsdfas"></a>
</div>
<div class="slide cycle-slide cycle-slide-active" style="position: absolute; top: 0px; left: 0px; z-index: 100; opacity: 1;">
<a href="#"><img style="background-color: #ff0000;" src="http://cambelt.co/1070x400/Rotator1?color=cccccc,ffffff" alt="fsadfsdfas"></a>
</div>
<div class="slide cycle-slide" style="position: absolute; top: 0px; left: 0px; z-index: 99; display: none;">
<a href="#"><img src="http://cambelt.co/1070x400/Rotator2?color=cccccc,ffffff" alt=""></a>
</div>
<div class="slide cycle-slide" style="position: absolute; top: 0px; left: 0px; z-index: 97; display: none;">
<a href="#"><img src="http://cambelt.co/1070x400/Rotator3?color=cccccc,ffffff" alt=""></a></div>
<div class="slide cycle-slide" style="position: absolute; top: 0px; left: 0px; z-index: 96; display: none;"><a href="#"><img src="http://cambelt.co/1070x400/Rotator4?color=cccccc,ffffff" alt=""></a>
</div>
</div>
我的父div位于滑块上方(.home-rotator-wrap)并跨越窗口的宽度。到目前为止,我有这个,但我相当肯定我离开了。
$('.home-rotator-wrap > .cycle-slide-active > img').cycle(function() {
var slideBG = $(this).attr('style');
$('.home-rotator-wrap').attr('style', slideBG);
});
答案 0 :(得分:2)
首先,您需要将'data-cycle-timeout'设置为0以外的值,图像根本不显示。这是代码:
$('.cycle-slideshow').on('cycle-before', function (event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) {
var bg = $(incomingSlideEl).find('img').css('backgroundColor'); // get background style from each image
$('body').css('background', bg); // set background color from var
});