【发布时间】:2017-11-23 23:46:13
【问题描述】:
我正在尝试将 Very Simple Slider 用于我未来的网站,但是当我想在同一页面上有多个滑块时,我遇到了问题。
当我复制 HTML/CSS/JS 中的所有属性并更改名称以创建新滑块时,它确实会创建一个新滑块,但控制箭头不再起作用。好吧,它们有效,但仅适用于一个滑块。我知道这可能是 JS 问题,但由于我不是开发人员,所以我找不到问题所在。
如果您能帮我找到解决方案 - 以及缩短 CSS 和 JS 的方法,那将非常酷,因为我将不得不通过 css modals 在同一页面上使用此滑块十次。
哦,这是我的代码。
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>
【问题讨论】:
-
宁可使用其他幻灯片/插件...这个有严重的错误,当您尝试使用“自动播放”选项时,打开和关闭它,然后看看...有更好的,当然……
标签: javascript jquery html css slider