我想构建一个内容滑块,左侧和右侧有一个箭头可以导航。我想设置滑块的位置+ 100px(当你点击右边)和-100px(当你点击左边)。这个功能有效。
但是出了什么问题,我想禁用它在达到某个x位置值时移动。因此,当我的内容到达结束时,它必须停止,以便用户只能导航回来。
希望你能帮到我,因为我找不到它。
CSS
#container{
width: 500px;
height: 150px;
background:#CDFAA8;
overflow:hidden;
position:absolute;
left: 13px;
}
#slider{
width: 200px;
height: 150px;
background:#063;
position:absolute;
left: 0px;
}
#block1{
width: 100px;
height: 150px;
background:#067;
float: left;
}
#block2{
width: 100px;
height: 150px;
background:#079;
float: left;
}
#move_right{
height: 150px;
width: 20px;
background: #3f3f3f;
position: absolute;
right:0px;
z-index: 200;
opacity: 0.2;
}
#move_left{
height: 150px;
width: 20px;
background: #3f3f3f;
position: absolute;
left:0px;
z-index: 200;
opacity: 0.2;
}
HTML
<div id="container">
<div id="move_left"><button id="right">«</button></div><div id="move_right"><br><br><button id="left">»</button></div>
<div id="slider">
<div id="block1"></div>
<div id="block2"></div>
</div>
</div>
的java
$("#right").click(function() {
$("#slider").animate({
"left": "+=100px"
}, "slow");
});
$("#left").click(function() {
$("#slider").animate({
"left": "-=100px"
}, "slow");
});
答案 0 :(得分:0)
var L = parseInt($("#slider").css('left'));
$("#right").click(function() {
if (L<400) {
$("#slider").animate({
"left": "+=100px"
}, "slow");
});
}
$("#left").click(function() {
if (L>0) {
$("#slider").animate({
"left": "-=100px"
}, "slow");
});
}
答案 1 :(得分:0)
此滑块的最终解决方案是:
<强> HTML 强>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
#temp{
height: 300px;
}
#container{
width: 500px;
height: 150px;
background:#CDFAA8;
overflow:hidden;
position:absolute;
left: 13px;
}
#slider{
width: 1300px;
height: 150px;
background:#063;
position:absolute;
left: 0px;
}
#block1{
width: 100px;
height: 150px;
background:#067;
float: left;
}
#block2{
width: 100px;
height: 150px;
background:#079;
float: left;
}
#move_right{
height: 150px;
width: 20px;
background: #3f3f3f;
position: absolute;
right:0px;
z-index: 200;
opacity: 0.2;
}
#move_left{
height: 150px;
width: 20px;
background: #3f3f3f;
position: absolute;
left:0px;
z-index: 200;
opacity: 0.2;
}
</style>
</head>
<body>
<div id="temp">
<div id="container">
<div id="move_left"><button id="right">«</button></div><div id="move_right"><br><br><button id="left">»</button></div>
<div id="slider">
<div id="block1">1</div>
<div id="block2">2</div>
<div id="block1">3</div>
<div id="block2">4</div>
<div id="block1">5</div>
<div id="block2">6</div>
<div id="block1">7</div>
<div id="block2">8</div>
<div id="block1">9</div>
<div id="block2">10</div>
<div id="block1">11</div>
<div id="block2">12</div>
<div id="block1">13</div>
</div>
</div>
</div>
<强> JAVA 强>
(function($) {
var slider = $('#slider'),
step = 500,
left = parseInt(slider.css('left'), 10),
max = $('#container').width() - slider.width(),
min = 0;
$("#left").click(function() {
if (left > max) {
var newLeft = left - step;
left = (newLeft>max) ? newLeft : max;
$("#slider").animate({
"left": left + 'px'
}, "slow");
}
});
$("#right").click(function() {
if (left < 0) {
var newLeft = left + step;
left = (newLeft<min) ? newLeft : min;
slider.animate({
"left": left + 'px'
}, "slow");
}
});
})(jQuery);
答案 2 :(得分:-2)
单击幻灯片按钮时,您必须进行一些检查。
这是执行此操作的代码。我将它放在一个闭合中并使其动态化,只是“步骤”是硬编码的,因为你的“滑块”没有你想要它滑动的宽度:
(function($) {
var slider = $('#slider'),
step = 100,
left = parseInt(slider.css('left'), 10),
max = $('#container').width() - slider.width(),
min = 0;
$("#right").click(function() {
if (left < max) {
var newLeft = left+step;
left = (newLeft<max) ? newLeft : max;
$("#slider").animate({
"left": left + 'px'
}, "slow");
}
});
$("#left").click(function() {
if (left > 0) {
var newLeft = left - step;
left = (newLeft>min) ? newLeft : min;
slider.animate({
"left": left + 'px'
}, "slow");
}
});
})(jQuery);