我已经addClass
和removeClass
制作了幻灯片,但滑块没有效果。所以我想在方法addClass
和removeClass
上添加一些效果。
$('#button-next').on('click', function () {
$('#big img').css({
'display': 'none'
});
var currentActiveImage = $('.image-shown');
var nextActiveImage = currentActiveImage.next();
if (nextActiveImage.length == 0) {
nextActiveImage = $('.carousel-inner img').first();
}
currentActiveImage.removeClass('image-shown').addClass('image-hidden').css({
'z-index': '-10'
});
nextActiveImage.addClass('image-shown').removeClass('image-hidden').css({
'z-index': '2000'
});
$('.carousel-inner img').not([currentActiveImage, nextActiveImage]).css('z-index', 1);
e.preventDefault()
});
$('#button-prev').on('click', function () {
var currentActiveImage = $('.image-shown');
var nextActiveImage = currentActiveImage.prev();
if (nextActiveImage.length == 0) {
nextActiveImage = $('.carousel-inner img').last();
}
currentActiveImage.removeClass('image-shown').addClass('image-hidden').css({
'z-index': '-10'
});
nextActiveImage.addClass('image-shown').removeClass('image-hidden').css({
'z-index': '2000'
});
$('.carousel-inner img').not([currentActiveImage, nextActiveImage]).css('z-index', 1);
e.preventDefault()
});
答案 0 :(得分:0)
您可以使用CSS3动画功能。
答案 1 :(得分:0)
我建议你使用CSS过渡。
将opacity: 0;
CSS属性添加到.image-hidden
课程,并确保.image-hidden
没有 display: none;
。
与.image-shown
类似。
然后将转场添加到.image-shown
和.image-hidden
.image-shown {
/* ... */
opacity: 1;
}
.image-hidden {
/* ... */
opacity: 0;
}
.image-shown, .image-hidden {
-webkit-transition: opacity .5s linear 0s; /* For Safari 3.1 to 6.0 */
transition: opacity .5s linear 0s;
}
答案 2 :(得分:0)
也许您正在寻找CSS动画功能。例如,将您的css添加/更改为:
.carousel-inner {
/* We keep it position relative */
position: relative;
/* Change or remove these if you want */
height: 30px;
padding: 20px;
width: 100px;
border: 1px dashed;
}
.carousel-inner img {
/* So that they go on top of each other */
position: absolute;
/* Transitions; we animate the opacity, but you can animate whatever you want */
-webkit-transition: opacity 5s ease; /* 5 seconds */
-moz-transition: opacity 5s ease;
-ms-transition: opacity 5s ease;
-o-transition: opacity 5s ease;
transition: opacity 5s ease;
}
/* This is just for you, to tell the difference between the images (not necessary) */
img:nth-child(1) {
background-color: white;
}
img:nth-child(3) {
background-color: green;
}
.image-shown {
/* Change this from
display: block;
to: */
opacity: 1;
}
.image-hidden {
/* Change this from
display: none;
to: */
opacity: 0;
}
注意图像如何淡入下一个图像。不需要对脚本进行任何更改,但如果您为position: relative
和carousel-inner
设置了position: absolute
图片,那么您可以使用z-index
删除内容,因为它不会更长的必要。无论如何,一次只能显示一个图像。
请注意,这仅适用于现代浏览器。如果您需要支持旧浏览器,请使用jQuery的fade
方法,如下所示:
$('#button-next').on('click', function(){
$('#big').css({'display':'none'});
var currentActiveImage = $('.image-shown');
var nextActiveImage = currentActiveImage.next();
if (nextActiveImage.length == 0) {
nextActiveImage = $('.carousel-inner img').first();
}
currentActiveImage.removeClass('image-shown').addClass('image-hidden').fadeOut();
nextActiveImage.addClass('image-shown').removeClass('image-hidden').fadeIn();
e.preventDefault()
});
如果您这样做,请删除image-shown
和image-hidden
的所有CSS;它们变成了简单的标记类。对prev
按钮执行类似操作。