同步 flex 滑块 - flex-direction-nav(Arrows)
Sync flex Sliders - flex-direction-nav(Arrows)
我正在使用 flex-slider 开发 Slider。我想到了将两个 flexsliders 同步为一个的想法,就像我们对 Owl-carousel 所做的那样。当 flex 字幕与图像一起动画时,我必须使用两个 flexsliders,因为我希望 flex-caption 动画不同于图像动画。我已经在这个 Answer 的帮助下让它们同步了
但现在我被困在两点上:
1) 我也希望箭头同步。正如您在我的代码中看到的那样,我已经对 flex-direction-navs 进行了分组。现在我想通过单击按钮更改两张幻灯片,但这不起作用,因为它上面有分层。
2) 我希望自定义定位按钮根据活动幻灯片处于活动状态。例如,我在第三张幻灯片上,我希望第三个定位锚标记处于活动状态,例如颜色变化等等。
希望我清楚地解释了我的问题。如果您需要任何进一步的信息,请在下面发表评论。
这里是Codepenlink。后面是代码。
jQuery(document).ready(function($) {
$('#main-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
}
});
$('#secondary-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
}
});
});
.slider-wrap {
position: relative;
width: 700px;
margin: 0 auto
}
#secondary-slider {
position: absolute;
top: 0;
right: 0;
width: 50%;
}
.flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-next,
.flex-direction-nav .flex-next {
top: unset;
left: unset;
right: unset;
bottom: unset;
}
.flexslider:hover .flex-direction-nav .flex-prev:hover,
.flexslider:hover .flex-direction-nav .flex-prev:hover {
opacity: 1;
}
#main-slider .flex-direction-nav .flex-prev,
#secondary-slider .flex-direction-nav .flex-prev {
position: absolute;
top: 20px !important;
right: 0 !important;
opacity: 1;
}
#main-slider .flex-direction-nav .flex-next,
#secondary-slider .flex-direction-nav .flex-next {
position: absolute;
top: 120px !important;
right: 0;
opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/jquery.flexslider.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/flexslider.css" rel="stylesheet" />
<div class="slider-wrap">
<div id="main-slider" class="flexslider">
<ul class="slides">
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
</ul>
</div>
<div id="secondary-slider" class="flexslider">
<ul class="slides">
<li>
<p>Text 1</p>
</li>
<li>
<p>Text 2</p>
</li>
<li>
<p>Text 3</p>
</li>
<li>
<p>Text 4</p>
</li>
</ul>
</div>
<a rel="0" class="slide_thumb" href="#">slide link 1</a>
<a rel="1" class="slide_thumb" href="#">slide link 2</a>
<a rel="2" class="slide_thumb" href="#">slide link 3</a>
<a rel="3" class="slide_thumb" href="#">slide link 3</a>
您可以通过创建自己的箭头来应用为 link 所做的相同逻辑。您删除默认的,然后添加您自己的侦听器来移动滑块。
对于活动 link 只需添加更多 JS 来切换 class。
这是一个示例,您可以轻松地使用 CSS 随意调整位置:
jQuery(document).ready(function($) {
$('#main-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
$('.flex-prev').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=0) {
c--;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(3);
$('.slide_thumb[rel=3]').addClass('active');
}
})
$('.flex-next').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=3) {
c++;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(0);
$('.slide_thumb[rel=0]').addClass('active');
}
})
}
});
$('#secondary-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
$('.flex-prev').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=0) {
c--;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(3);
$('.slide_thumb[rel=3]').addClass('active');
}
})
$('.flex-next').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=3) {
c++;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(0);
$('.slide_thumb[rel=0]').addClass('active');
}
})
}
});
});
.slider-wrap {
position: relative;
width: 700px;
margin: 0 auto
}
#secondary-slider {
position: absolute;
top: 0;
right: 0;
width: 50%;
}
.flexslider .flex-direction-nav {
display:none;
}
.active {
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/jquery.flexslider.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/flexslider.css" rel="stylesheet" />
<div class="slider-wrap">
<div id="main-slider" class="flexslider">
<ul class="slides">
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
</ul>
</div>
<div id="secondary-slider" class="flexslider">
<ul class="slides">
<li>
<p>Text 1</p>
</li>
<li>
<p>Text 2</p>
</li>
<li>
<p>Text 3</p>
</li>
<li>
<p>Text 4</p>
</li>
</ul>
</div>
<ul class="flex-direction-nav"><li class="flex-nav-prev"><a class="flex-prev" href="#">Previous</a></li><li class="flex-nav-next"><a class="flex-next" href="#">Next</a></li></ul>
<a rel="0" class="slide_thumb" href="#">slide link 1</a>
<a rel="1" class="slide_thumb" href="#">slide link 2</a>
<a rel="2" class="slide_thumb" href="#">slide link 3</a>
<a rel="3" class="slide_thumb" href="#">slide link 3</a>
我正在使用 flex-slider 开发 Slider。我想到了将两个 flexsliders 同步为一个的想法,就像我们对 Owl-carousel 所做的那样。当 flex 字幕与图像一起动画时,我必须使用两个 flexsliders,因为我希望 flex-caption 动画不同于图像动画。我已经在这个 Answer 的帮助下让它们同步了 但现在我被困在两点上:
1) 我也希望箭头同步。正如您在我的代码中看到的那样,我已经对 flex-direction-navs 进行了分组。现在我想通过单击按钮更改两张幻灯片,但这不起作用,因为它上面有分层。
2) 我希望自定义定位按钮根据活动幻灯片处于活动状态。例如,我在第三张幻灯片上,我希望第三个定位锚标记处于活动状态,例如颜色变化等等。
希望我清楚地解释了我的问题。如果您需要任何进一步的信息,请在下面发表评论。
这里是Codepenlink。后面是代码。
jQuery(document).ready(function($) {
$('#main-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
}
});
$('#secondary-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
}
});
});
.slider-wrap {
position: relative;
width: 700px;
margin: 0 auto
}
#secondary-slider {
position: absolute;
top: 0;
right: 0;
width: 50%;
}
.flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-next,
.flex-direction-nav .flex-next {
top: unset;
left: unset;
right: unset;
bottom: unset;
}
.flexslider:hover .flex-direction-nav .flex-prev:hover,
.flexslider:hover .flex-direction-nav .flex-prev:hover {
opacity: 1;
}
#main-slider .flex-direction-nav .flex-prev,
#secondary-slider .flex-direction-nav .flex-prev {
position: absolute;
top: 20px !important;
right: 0 !important;
opacity: 1;
}
#main-slider .flex-direction-nav .flex-next,
#secondary-slider .flex-direction-nav .flex-next {
position: absolute;
top: 120px !important;
right: 0;
opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/jquery.flexslider.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/flexslider.css" rel="stylesheet" />
<div class="slider-wrap">
<div id="main-slider" class="flexslider">
<ul class="slides">
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
</ul>
</div>
<div id="secondary-slider" class="flexslider">
<ul class="slides">
<li>
<p>Text 1</p>
</li>
<li>
<p>Text 2</p>
</li>
<li>
<p>Text 3</p>
</li>
<li>
<p>Text 4</p>
</li>
</ul>
</div>
<a rel="0" class="slide_thumb" href="#">slide link 1</a>
<a rel="1" class="slide_thumb" href="#">slide link 2</a>
<a rel="2" class="slide_thumb" href="#">slide link 3</a>
<a rel="3" class="slide_thumb" href="#">slide link 3</a>
您可以通过创建自己的箭头来应用为 link 所做的相同逻辑。您删除默认的,然后添加您自己的侦听器来移动滑块。
对于活动 link 只需添加更多 JS 来切换 class。
这是一个示例,您可以轻松地使用 CSS 随意调整位置:
jQuery(document).ready(function($) {
$('#main-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
$('.flex-prev').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=0) {
c--;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(3);
$('.slide_thumb[rel=3]').addClass('active');
}
})
$('.flex-next').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=3) {
c++;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(0);
$('.slide_thumb[rel=0]').addClass('active');
}
})
}
});
$('#secondary-slider').flexslider({
animation: "slide",
slideToStart: 0,
start: function(slider) {
$('a.slide_thumb').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('.flexslider').show();
var slideTo = $(this).attr("rel") //Grab rel value from link;
var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
if (slider.currentSlide != slideToInt) {
slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
}
});
$('.flex-prev').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=0) {
c--;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(3);
$('.slide_thumb[rel=3]').addClass('active');
}
})
$('.flex-next').click(function(e) {
e.preventDefault();
$('.active').removeClass('active');
$('.flexslider').show();
var c = slider.currentSlide;
if(c!=3) {
c++;
slider.flexAnimate(c);
$('.slide_thumb[rel='+c+']').addClass('active');
} else {
slider.flexAnimate(0);
$('.slide_thumb[rel=0]').addClass('active');
}
})
}
});
});
.slider-wrap {
position: relative;
width: 700px;
margin: 0 auto
}
#secondary-slider {
position: absolute;
top: 0;
right: 0;
width: 50%;
}
.flexslider .flex-direction-nav {
display:none;
}
.active {
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/jquery.flexslider.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/flexslider.css" rel="stylesheet" />
<div class="slider-wrap">
<div id="main-slider" class="flexslider">
<ul class="slides">
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
<li>
<img src="https://via.placeholder.com/350x150" />
</li>
</ul>
</div>
<div id="secondary-slider" class="flexslider">
<ul class="slides">
<li>
<p>Text 1</p>
</li>
<li>
<p>Text 2</p>
</li>
<li>
<p>Text 3</p>
</li>
<li>
<p>Text 4</p>
</li>
</ul>
</div>
<ul class="flex-direction-nav"><li class="flex-nav-prev"><a class="flex-prev" href="#">Previous</a></li><li class="flex-nav-next"><a class="flex-next" href="#">Next</a></li></ul>
<a rel="0" class="slide_thumb" href="#">slide link 1</a>
<a rel="1" class="slide_thumb" href="#">slide link 2</a>
<a rel="2" class="slide_thumb" href="#">slide link 3</a>
<a rel="3" class="slide_thumb" href="#">slide link 3</a>