我正在尝试将 Very Simple Slider 用于我未来的网站,但当我想在同一页面上放置几个滑块时,我遇到了问题。
当我复制HTML / CSS / JS中的所有属性并更改名称以创建新滑块时,它会创建一个新滑块,但控制箭头不再起作用。嗯,他们工作,但只有一个滑块。我知道它可能像JS问题,但由于我不是开发人员,我找不到问题。
如果你可以帮我找到一个解决方案 - 也是一种缩短CSS和JS的方法,这将是非常酷的,因为我将不得不通过css模态在同一页面上使用这个滑块十次。
哦,这是我的代码。
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();
});
var slideCount = $('#slider2 ul li').length;
var slideWidth = $('#slider2 ul li').width();
var slideHeight = $('#slider2 ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider2').css({
width: slideWidth,
height: slideHeight
});
$('#slider2 ul').css({
width: sliderUlWidth,
marginLeft: -slideWidth
});
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
function moveLeft() {
$('#slider2 ul').animate({
left: +slideWidth
}, 200, function() {
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
function moveRight() {
$('#slider2 ul').animate({
left: -slideWidth
}, 200, function() {
$('#slider2 ul li:first-child').appendTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
$('a.control_prev2').click(function() {
moveLeft();
});
$('a.control_next2').click(function() {
moveRight();
});
});
#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;
}
#slider2 {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}
#slider2 ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider2 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_prev2,
a.control_next2 {
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_prev2:hover,
a.control_next2:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
a.control_prev2 {
border-radius: 0 2px 2px 0;
}
a.control_next2 {
right: 0;
border-radius: 2px 0 0 2px;
}
.slider_option2 {
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 id="slider2">
<a href="#" class="control_next2">></a>
<a href="#" class="control_prev2">
<</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>
答案 0 :(得分:0)
您宣布moveLeft
和moveRight
两次只是重命名为slider2的那个,它会起作用
编辑:
不推荐使用您正在使用的方法,因为每次需要在同一页面上添加新滑块时都需要更改源代码(js和css),您应该了解如何创建插件,这将有助于您学习如何创建通用滑块,同时像slick这样的滑块的一个很好的例子将是一个很好的选择使用,但因为你正在尝试学习,并希望清除这些错误为什么要添加片段并提出答案。
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();
});
var slideCount = $('#slider2 ul li').length;
var slideWidth = $('#slider2 ul li').width();
var slideHeight = $('#slider2 ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('#slider2').css({
width: slideWidth,
height: slideHeight
});
$('#slider2 ul').css({
width: sliderUlWidth,
marginLeft: -slideWidth
});
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
function moveLeft2() {
$('#slider2 ul').animate({
left: +slideWidth
}, 200, function() {
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
function moveRight2() {
$('#slider2 ul').animate({
left: -slideWidth
}, 200, function() {
$('#slider2 ul li:first-child').appendTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
$('a.control_prev2').click(function() {
moveLeft2();
});
$('a.control_next2').click(function() {
moveRight2();
});
});
&#13;
#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;
}
#slider2 {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}
#slider2 ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider2 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_prev2,
a.control_next2 {
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_prev2:hover,
a.control_next2:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
a.control_prev2 {
border-radius: 0 2px 2px 0;
}
a.control_next2 {
right: 0;
border-radius: 2px 0 0 2px;
}
.slider_option2 {
position: relative;
margin: 10px auto;
width: 160px;
font-size: 18px;
}
&#13;
<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 id="slider2">
<a href="#." class="control_next2">></a>
<a href="#." class="control_prev2">
<</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>
&#13;
答案 1 :(得分:-1)
这是工作示例。但是你应该使它更通用,这样你就可以添加多个滑块。
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();
});
var slide2Count = $('#slider2 ul li').length;
var slide2Width = $('#slider2 ul li').width();
var slide2Height = $('#slider2 ul li').height();
var slider2UlWidth = slide2Count * slide2Width;
$('#slider2').css({ width: slide2Width, height: slide2Height });
$('#slider2 ul').css({ width: slider2UlWidth, marginLeft: - slide2Width });
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
function moveLeftS2() {
$('#slider2 ul').animate({
left: + slide2Width
}, 200, function () {
$('#slider2 ul li:last-child').prependTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
function moveRightS2() {
$('#slider2 ul').animate({
left: - slide2Width
}, 200, function () {
$('#slider2 ul li:first-child').appendTo('#slider2 ul');
$('#slider2 ul').css('left', '');
});
};
$('a.control_prev2').click(function () {
moveLeftS2();
});
$('a.control_next2').click(function () {
moveRightS2();
});
});
#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;
}
#slider2 {
position: relative;
overflow: hidden;
margin: 20px auto 0 auto;
border-radius: 4px;
}
#slider2 ul {
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider2 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_prev2,
a.control_next2 {
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_prev2:hover,
a.control_next2:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
a.control_prev2 {
border-radius: 0 2px 2px 0;
}
a.control_next2 {
right: 0;
border-radius: 2px 0 0 2px;
}
.slider_option2 {
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="javascript:;" class="control_next">></a>
<a href="javascript:;" 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 id="slider2">
<a href="javascript:;" class="control_next2">></a>
<a href="javascript:;" class="control_prev2">
< </a>
<ul>
<li>SLIDE 21</li>
<li style="background: #aaa;">SLIDE 22</li>
<li>SLIDE 23</li>
<li style="background: #aaa;">SLIDE 24</li>
</ul>
</div>