我使用此幻灯片http://jsfiddle.net/rCd26/2/并进行了一些更改 - > http://jsfiddle.net/rCd26/386/
但我需要两件事:
1)分页 - 我需要从分页链接到适当的图像。
2)如果你从最后一张图像到第一张图像,从头到尾,你会看到只有空格。
旧版代码
HTML:
<div class="slideshow">
<img src="http://placehold.it/500x100/0000CD&text=1" width="500" height="100" alt="first image">
<img src="http://placehold.it/500x100/008000&text=2" width="500" height="100" alt="second image">
<img src="http://placehold.it/500x100/B22222&text=3" width="500" height="100" alt="third image">
<img src="http://placehold.it/500x100/F4A460&text=4" width="500" height="100" alt="fourth image">
</div>
<button id="prev">Prev</button>
<button id="next">Next</button>
CSS:
.slideshow {
position: relative;
/* necessary to absolutely position the images inside */
width: 500px;
/* same as the images inside */
height: 100px;
}
.slideshow img {
position: absolute;
display: none;
}
.slideshow img:first-child {
display: block;
/* overrides the previous style */
}
JS:
var interval = undefined;
$(document).ready(function () {
interval = setInterval(getNext, 2000); // milliseconds
$('#next').on('click', getNext);
$('#prev').on('click', getPrev);
});
function getNext() {
var $curr = $('.slideshow img:visible'),
$next = ($curr.next().length) ? $curr.next() : $('.slideshow img').first();
transition($curr, $next);
}
function getPrev() {
var $curr = $('.slideshow img:visible'),
$next = ($curr.prev().length) ? $curr.prev() : $('.slideshow img').last();
transition($curr, $next);
}
function transition($curr, $next) {
clearInterval(interval);
$next.css('z-index', 2).fadeIn('slow', function () {
$curr.hide().css('z-index', 0);
$next.css('z-index', 1);
});
}
新版本
HTML:
<div class="slideshow">
<div class="arrow-left" id="prev"></div>
<img src="http://www.theextramilewithcharlie.com/wp-content/uploads/2014/02/MovingTargetMobile_f2.jpg" width="360" height="222" alt="Yellow" class="slide">
<img src="https://www.digitec.ch/img/t-1-64-E8454026A2BFADCFE29E820F854838F4/disk2go-usb-stick-nano-16gb-usb-20-red-usb-flash-drive.png" width="360" height="222" alt="Red" class="slide">
<img src="https://www.digitec.ch/img/t-1-64-605CE6EB9472BEB7A267A29F5F052D6B/kef-c6lcr-black-center-speaker-hifi-homecinema-speaker.png" width="360" height="222" alt="Black" class="slide">
<img src="http://www.qishop.cyklomax.cz/RM-14-Cross-100-18-black-blue-white-_a45681105_10639.aspx?fm=0&width=360" width="360" height="222" alt="Blue" class="slide">
<div class="arrow-right" id="next"></div>
<div class="pagination" style="position: relative; top: 280px;">
<div class="yellow"></div>
<div class="red"></div>
<div class="black"></div>
<div class="blue"></div>
</div>
</div>
CSS:
.slideshow {
position: relative;
/* necessary to absolutely position the images inside */
width: 500px;
/* same as the images inside */
height: 100px;
}
.slideshow img {
position: absolute;
display: none;
}
.slideshow img:first-child {
display: block;
/* overrides the previous style */
}
.arrow-left{
float:left;
width:15px;
height:25px;
background:url(http://eshop.lukasholeczy.eu/images/arrowhover.png) 0 0 no-repeat;
cursor: pointer;
transition: background 0.3s;
position:absolute;
left:0px;
top:0px;
bottom:0px;
margin:auto;
}
.arrow-left:hover{
background:url(http://eshop.lukasholeczy.eu/images/arrow.png) 0 0 no-repeat;
}
.arrow-right{
float:right;
width:15px;
height:25px;
background:url(http://eshop.lukasholeczy.eu/images/arrowhover.png) 0 0 no-repeat;
cursor: pointer;
transition: background 0.3s;
position:absolute;
right:0px;
top:0px;
bottom:0px;
margin:auto;
-ms-transform: rotate(180deg); /* IE 9 */
-webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
transform: rotate(180deg);
}
.arrow-right:hover{
background:url(http://eshop.lukasholeczy.eu/images/arrow.png) 0 0 no-repeat;
}
.pagination{
width:100%;
float:left;
position:absolute;
bottom:0px;
height:3px;
}
.yellow{
display:inline-block;
width:54px;
height:3px;
cursor:pointer;
background:#ffd631;
}
.yellow:hover{
height:7px !important;
}
.red{
display:inline-block;
width:54px;
height:3px;
cursor:pointer;
background:#ff5e5e;
}
.red:hover{
height:7px !important;
}
.black{
display:inline-block;
width:54px;
height:3px;
cursor:pointer;
background:#3b3b3b;
}
.black:hover{
height:7px !important;
}
.blue{
display:inline-block;
width:54px;
height:3px;
cursor:pointer;
background:#52708a;
}
.blue:hover{
height:7px !important;
}
JS是一样的。
非常感谢大家。
答案 0 :(得分:0)
有一些事情:
alt
属性,因为这是为了可访问性。相反,我使用了data-linked-class属性。我还在CSS中添加了一个indicatePage
类来打开或关闭高度。如果您希望页面链接也淡入/淡出,您可以使用动画淡化addClass / removeClass过渡。
根据评论进行更新
var interval = undefined;
$(document).ready(function() {
interval = setInterval(function() {
getNext(null, true);
}, 2000); // milliseconds
$('#next').on('click', getNext);
$('#prev').on('click', getPrev);
$('.nav').click(function() {
var $next = $('img[data-linked-id=' + this.id + ']');
var $curr = $('.slideshow img:visible');
transition($curr, $next, false);
});
});
function getNext(event, keepInterval) {
var $curr = $('.slideshow img:visible'),
$next = ($curr.next().length) ? $curr.next() : $('.slideshow img').first();
transition($curr, $next, keepInterval);
}
function getPrev() {
var $curr = $('.slideshow img:visible'),
$next = ($curr.prev().length) ? $curr.prev() : $('.slideshow img').last();
transition($curr, $next);
}
function transition($curr, $next, keepInterval) {
if (!keepInterval) {
clearInterval(interval);
}
$next.css('z-index', 2).fadeIn('slow', function() {
$curr.hide().css('z-index', 0);
$next.css('z-index', 1);
$('#' + $curr.attr('data-linked-id')).removeClass('indicatePage');
$('#' + $next.attr('data-linked-id')).addClass('indicatePage');
});
}
.slideshow {
position: relative;
/* necessary to absolutely position the images inside */
width: 500px;
/* same as the images inside */
height: 100px;
left: 20px;
}
.slideshow img {
position: absolute;
display: none;
}
.slideshow img:first-child {
display: block;
/* overrides the previous style */
}
.arrow-left {
float: left;
width: 15px;
height: 25px;
background: url(http://eshop.lukasholeczy.eu/images/arrowhover.png) 0 0 no-repeat;
cursor: pointer;
transition: background 0.3s;
position: absolute;
left: 0px;
top: 0px;
bottom: 0px;
margin: auto;
}
.arrow-left:hover {
background: url(http://eshop.lukasholeczy.eu/images/arrow.png) 0 0 no-repeat;
}
.arrow-right {
float: right;
width: 15px;
height: 25px;
background: url(http://eshop.lukasholeczy.eu/images/arrowhover.png) 0 0 no-repeat;
cursor: pointer;
transition: background 0.3s;
position: absolute;
right: 0px;
top: 0px;
bottom: 0px;
margin: auto;
-ms-transform: rotate(180deg);
/* IE 9 */
-webkit-transform: rotate(180deg);
/* Chrome, Safari, Opera */
transform: rotate(180deg);
}
.arrow-right:hover {
background: url(http://eshop.lukasholeczy.eu/images/arrow.png) 0 0 no-repeat;
}
.pagination {
width: 100%;
float: left;
position: absolute;
bottom: 0px;
height: 3px;
}
#yellow {
display: inline-block;
width: 54px;
height: 3px;
cursor: pointer;
background: #ffd631;
}
#yellow:hover {
height: 7px !important;
}
#red {
display: inline-block;
width: 54px;
height: 3px;
cursor: pointer;
background: #ff5e5e;
}
#red:hover {
height: 7px !important;
}
#black {
display: inline-block;
width: 54px;
height: 3px;
cursor: pointer;
background: #3b3b3b;
}
#black:hover {
height: 7px !important;
}
#blue {
display: inline-block;
width: 54px;
height: 3px;
cursor: pointer;
background: #52708a;
}
#blue:hover {
height: 7px !important;
}
.indicatePage {
height: 7px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="arrow-left" id="prev"></div>
<div class="slideshow">
<img src="http://www.theextramilewithcharlie.com/wp-content/uploads/2014/02/MovingTargetMobile_f2.jpg" width="360" height="222" data-linked-id='yellow' alt="Yellow" class="slide" />
<img src="https://www.digitec.ch/img/t-1-64-E8454026A2BFADCFE29E820F854838F4/disk2go-usb-stick-nano-16gb-usb-20-red-usb-flash-drive.png" width="360" height="222" alt="Red" data-linked-id='red' class="slide" />
<img src="https://www.digitec.ch/img/t-1-64-605CE6EB9472BEB7A267A29F5F052D6B/kef-c6lcr-black-center-speaker-hifi-homecinema-speaker.png" width="360" height="222" alt="Black" data-linked-id='black' class="slide" />
<img src="http://www.qishop.cyklomax.cz/RM-14-Cross-100-18-black-blue-white-_a45681105_10639.aspx?fm=0&width=360" width="360" height="222" alt="Blue" data-linked-id='blue' class="slide" />
</div>
<div class="arrow-right" id="next"></div>
<div class="pagination" style="position: relative; top: 280px;">
<div id="yellow" class="nav"></div>
<div id="red" class="nav"></div>
<div id="black" class="nav"></div>
<div id="blue" class="nav"></div>
</div>