如何突出显示 Flexslider 轮播中的活动幻灯片?
How to highlight active slides in Flexslider carousel?
是否可以突出显示 Flexslider 轮播中的当前项,以便我可以向活动项添加 class?
我使用 Flexslider 轮播的基本设置。现在我想要一个脚本,可以将活动添加到可见视口内的每张幻灯片。
我用的这个例子:
http://flexslider.woothemes.com/basic-carousel.html
我现在如何在启动函数中获取 Flexslider 属性。当我为函数提供我可以请求的参数时 - ul 中的项目总数、它将移动多少项目以及将有多少分页。我不知道是否可以编写一个公式来计算给定变量中哪个是活动的,所以我可以突出显示它们。
$('.flexslider').flexslider({
animation: "slide",
animationLoop: false,
slideshow: false,
itemWidth: 460,
itemMargin: 30,
minItems: 2,
maxItems: 6,
controlNav: false,
customDirectionNav: ".custom-direction li a",
start: function(slider) {
var pagingCount = slider.pagingCount;
var move = slider.move;
var visibleSlides = slider.visible;
var totalSlides = slider.count;
console.log(slider);
console.log(slider.move);
console.log(slider.update);
if (totalSlides) {
$('.slides li').addClass('active-slides');
};
// console.log(slider.count);
// console.log(slider.currentSlide);
}
});
我已经开始了,但不知道 if 语句或 for 循环应该如何完成。
有什么想法吗?
提前致谢。
使用事件 start
和 'after' 来处理这个问题。看下面的例子。
$('.flexslider').flexslider({
animation: "slide",
animationLoop: false,
slideshow: false,
itemWidth: 460,
itemMargin: 30,
minItems: 2,
maxItems: 6,
controlNav: false,
customDirectionNav: ".custom-direction li a",
start: function (slider) {
window.addCurrentSlidesClass(slider);
},
after: function (slider) {
window.addCurrentSlidesClass(slider);
}
});
window.addCurrentSlidesClass = function (slider) {
//debugger;
$('.flexslider li').removeClass("active-slides");
var startli = (slider.move * (slider.currentSlide));
var endli = (slider.move * (slider.currentSlide + 1));
for (i = startli + 1; i <= endli; i++) {
$('.flexslider li:nth-child(' + i + ')').addClass('active-slides');
}
}
<div class="flexslider carousel">
<div class="flex-viewport" style="overflow: hidden; position: relative;">
<ul class="slides" style="width: 2400%; transition-duration: 0.6s; transform: translate3d(0px, 0px, 0px);">
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
</ul>
</div>
<ol class="flex-control-nav flex-control-paging">
<li><a class="flex-active">1</a>
</li>
<li><a class="">2</a>
</li>
<li><a class="">3</a>
</li>
</ol>
<ul class="flex-direction-nav">
<li class="flex-nav-prev"><a class="flex-prev flex-disabled" href="#" tabindex="-1">Previous</a>
</li>
<li class="flex-nav-next"><a class="flex-next" href="#" tabindex="-1">Next</a>
</li>
</ul>
<div class="flex-pauseplay"><a class="flex-pause">Pause</a>
</div>
</div>
使用了上面接受的答案,但需要进行一些修改才能使其适用于我,如下所示。
主要处理一些异常,处理显示一半的幻灯片以及 move/visible 不同的情况。
window.addCurrentSlidesClass = function (slider) {
jQuery('.flexslider li').removeClass("active-slide");
// get visible by math rather than slider variable due to half shown slides not counting
// var visible = slider.visible;
var visible = Math.ceil(slider.w / slider.itemT);
var startli = (slider.move * (slider.currentSlide));
// var endli = (slider.move * (slider.currentSlide + 1));
var endli = startli + visible;
// Don't push past when you reach end of list
if(endli > slider.count) {
startli = startli - (endli - slider.count);
endli = endli - (endli - slider.count);
}
for (i = startli + 1; i <= endli; i++) {
jQuery('.flexslider li:nth-child(' + i + ')').addClass('active-slide');
}
};
是否可以突出显示 Flexslider 轮播中的当前项,以便我可以向活动项添加 class?
我使用 Flexslider 轮播的基本设置。现在我想要一个脚本,可以将活动添加到可见视口内的每张幻灯片。
我用的这个例子: http://flexslider.woothemes.com/basic-carousel.html
我现在如何在启动函数中获取 Flexslider 属性。当我为函数提供我可以请求的参数时 - ul 中的项目总数、它将移动多少项目以及将有多少分页。我不知道是否可以编写一个公式来计算给定变量中哪个是活动的,所以我可以突出显示它们。
$('.flexslider').flexslider({
animation: "slide",
animationLoop: false,
slideshow: false,
itemWidth: 460,
itemMargin: 30,
minItems: 2,
maxItems: 6,
controlNav: false,
customDirectionNav: ".custom-direction li a",
start: function(slider) {
var pagingCount = slider.pagingCount;
var move = slider.move;
var visibleSlides = slider.visible;
var totalSlides = slider.count;
console.log(slider);
console.log(slider.move);
console.log(slider.update);
if (totalSlides) {
$('.slides li').addClass('active-slides');
};
// console.log(slider.count);
// console.log(slider.currentSlide);
}
});
我已经开始了,但不知道 if 语句或 for 循环应该如何完成。
有什么想法吗? 提前致谢。
使用事件 start
和 'after' 来处理这个问题。看下面的例子。
$('.flexslider').flexslider({
animation: "slide",
animationLoop: false,
slideshow: false,
itemWidth: 460,
itemMargin: 30,
minItems: 2,
maxItems: 6,
controlNav: false,
customDirectionNav: ".custom-direction li a",
start: function (slider) {
window.addCurrentSlidesClass(slider);
},
after: function (slider) {
window.addCurrentSlidesClass(slider);
}
});
window.addCurrentSlidesClass = function (slider) {
//debugger;
$('.flexslider li').removeClass("active-slides");
var startli = (slider.move * (slider.currentSlide));
var endli = (slider.move * (slider.currentSlide + 1));
for (i = startli + 1; i <= endli; i++) {
$('.flexslider li:nth-child(' + i + ')').addClass('active-slides');
}
}
<div class="flexslider carousel">
<div class="flex-viewport" style="overflow: hidden; position: relative;">
<ul class="slides" style="width: 2400%; transition-duration: 0.6s; transform: translate3d(0px, 0px, 0px);">
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_cheesecake_brownie.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_lemon.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_donut.jpg" draggable="false">
</li>
<li style="width: 210px; float: left; display: block;">
<img src="images/kitchen_adventurer_caramel.jpg" draggable="false">
</li>
</ul>
</div>
<ol class="flex-control-nav flex-control-paging">
<li><a class="flex-active">1</a>
</li>
<li><a class="">2</a>
</li>
<li><a class="">3</a>
</li>
</ol>
<ul class="flex-direction-nav">
<li class="flex-nav-prev"><a class="flex-prev flex-disabled" href="#" tabindex="-1">Previous</a>
</li>
<li class="flex-nav-next"><a class="flex-next" href="#" tabindex="-1">Next</a>
</li>
</ul>
<div class="flex-pauseplay"><a class="flex-pause">Pause</a>
</div>
</div>
使用了上面接受的答案,但需要进行一些修改才能使其适用于我,如下所示。
主要处理一些异常,处理显示一半的幻灯片以及 move/visible 不同的情况。
window.addCurrentSlidesClass = function (slider) {
jQuery('.flexslider li').removeClass("active-slide");
// get visible by math rather than slider variable due to half shown slides not counting
// var visible = slider.visible;
var visible = Math.ceil(slider.w / slider.itemT);
var startli = (slider.move * (slider.currentSlide));
// var endli = (slider.move * (slider.currentSlide + 1));
var endli = startli + visible;
// Don't push past when you reach end of list
if(endli > slider.count) {
startli = startli - (endli - slider.count);
endli = endli - (endli - slider.count);
}
for (i = startli + 1; i <= endli; i++) {
jQuery('.flexslider li:nth-child(' + i + ')').addClass('active-slide');
}
};