我正在尝试用jquery创建简单的滑块。但是当我骑自行车时,我有点卡住了。
当我点击下一个时,第一个项目应该替换为最后一个,反之亦然。 是否有任何建议来解决这个问题
$(document).ready(function(){
var slideItem = $('.slider-item');
var slideContainer = $('.slide-container');
var sliderWrap = ((slideItem.width() * slideItem.length)+ 100);
$(slideContainer).css('width', sliderWrap+'px');
function next() {
$(slideContainer).animate({'margin-left':'-=515px'},300,function(){
$(slideItem).first().remove().appendTo(slideContainer);
$(slideContainer).css('margin-left', '');
});
}
function prev() {
$(slideContainer).animate({'margin-left':'+=515px'},300,function(){
$(slideItem).last().remove().prependTo(slideContainer);
$(slideContainer).css('margin-left', '');
});
}
$('.next').on('click', function(){
next();
});
$('.prev').on('click', function(){
prev();
});
});

.thumbnail-gallery {
margin-top: 160px;
position: relative;
overflow: hidden;
height: 100%;
background-color: green;
}
.slide-container {
white-space: nowrap;
background-color: red;
}
.slider-item {
width: 500px;
height: 100px;
background-color: yellow;
float: left;
margin-right: 15px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thumbnail-gallery">
<div class="slide-container">
<div class="slider-item">1</div>
<div class="slider-item">2</div>
<div class="slider-item">3</div>
</div>
</div>
<button class="prev">prev</button>
<button class="next">next</button>
&#13;
答案 0 :(得分:1)
.remove()
无需致电。 .appendTo()
和.prependTo()
都会从DOM
中删除元素,并将元素追加或预先添加到选定的父元素。
您可以使用jQuery对象变量slideContainer
作为jQuery()
的第二个参数,将上下文设置为父元素,直接后代选择器>
,jQuery :eq()
,参数为{{1}在0
函数和参数next
的{{1}}函数中选择-1
父元素中的预期元素。
prev
&#13;
slideContainer
&#13;
$(document).ready(function() {
var slideItem = $('.slider-item');
var slideContainer = $('.slide-container');
var sliderWrap = ((slideItem.width() * slideItem.length) + 100);
$(slideContainer).css('width', sliderWrap + 'px');
function next() {
slideContainer.animate({
'margin-left': '-=515px'
}, 300, function() {
$("> :eq(0)", slideContainer).appendTo(slideContainer);
slideContainer.css('margin-left', '');
});
}
function prev() {
$(slideContainer).animate({
'margin-left': '+=515px'
}, 300, function() {
$("> :eq(-1)", slideContainer)
.prependTo(slideContainer);
slideContainer.css('margin-left', '');
});
}
$('.next').on('click', function() {
next();
});
$('.prev').on('click', function() {
prev();
});
});
&#13;
或者,您可以对.thumbnail-gallery {
margin-top: 160px;
position: relative;
overflow: hidden;
height: 100%;
background-color: green;
}
.slide-container {
white-space: nowrap;
background-color: red;
}
.slider-item {
width: 500px;
height: 100px;
background-color: yellow;
float: left;
margin-right: 15px;
}
,<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thumbnail-gallery">
<div class="slide-container">
<div class="slider-item">1</div>
<div class="slider-item">2</div>
<div class="slider-item">3</div>
</div>
</div>
<button class="prev">prev</button>
<button class="next">next</button>
函数和next
事件处理程序使用单个函数,方法是将传递给函数的参数存储为与元素关联的数组,例如, prev
和click
元素的.data()
.next
&#13;
.prev
&#13;
$(document).ready(function() {
var slideItem = $('.slider-item');
var slideContainer = $('.slide-container');
var sliderWrap = ((slideItem.width() * slideItem.length) + 100);
$(".next").data("args", ["-", "append", 0]);
$(".prev").data("args", ["+", "prepend", -1]);
$(slideContainer).css('width', sliderWrap + 'px');
function nextPrev(sign, dir, index) {
slideContainer.animate({
'margin-left': sign + '=515px'
}, 300, function() {
$("> :eq("+index+")", slideContainer)[dir + "To"](slideContainer);
slideContainer.css('margin-left', '');
});
}
$(".next, .prev").on("click", function() {
nextPrev.apply(this, $(this).data("args"));
});
});
&#13;
答案 1 :(得分:0)
jQuery(document).ready(function ($) {
$('#checkbox').change(function(){
setInterval(function () {
moveRight();
}, 3000);
});
var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
$('#slider ul li:last-child').prependTo('#slider ul');
function moveLeft() {
$('#slider ul').animate({
left: + slideWidth
}, 200, function () {
$('#slider ul li:last-child').prependTo('#slider ul');
$('#slider ul').css('left', '');
});
};
function moveRight() {
$('#slider ul').animate({
left: - slideWidth
}, 200, function () {
$('#slider ul li:first-child').appendTo('#slider ul');
$('#slider ul').css('left', '');
});
};
$('a.control_prev').click(function () {
moveLeft();
});
$('a.control_next').click(function () {
moveRight();
});
});
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
html {
border-top: 5px solid #fff;
color: #2a2a2a;
}
html, body {
margin: 0;
padding: 0;
font-family: 'Open Sans';
}
h1 {
color: #fff;
text-align: center;
font-weight: 300;
}
#slider {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}
#slider ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider ul li {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
width: 500px;
height: 300px;
background: #ccc;
text-align: center;
line-height: 300px;
}
a.control_prev, a.control_next {
position: absolute;
top: 40%;
z-index: 999;
display: block;
padding: 4% 3%;
width: auto;
height: auto;
background: #2a2a2a;
color: #fff;
text-decoration: none;
font-weight: 600;
font-size: 18px;
opacity: 0.8;
cursor: pointer;
}
a.control_prev:hover, a.control_next:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
a.control_prev {
border-radius: 0 2px 2px 0;
}
a.control_next {
right: 0;
border-radius: 2px 0 0 2px;
}
.slider_option {
position: relative;
margin: 10px auto;
width: 160px;
font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider">
<a href="#" class="control_next">>></a>
<a href="#" class="control_prev"><</a>
<ul>
<li>SLIDE 1</li>
<li style="background: #aaa;">SLIDE 2</li>
<li>SLIDE 3</li>
<li style="background: #aaa;">SLIDE 4</li>
</ul>
</div>
<div class="slider_option">
<input type="checkbox" id="checkbox">
<label for="checkbox">Autoplay Slider</label>
</div>