我正在尝试为我的页面设计一个 slider ,我想知道是否有人知道如何创建从左到右的滑动 div,反之亦然,仅当按下相应的按钮(左和右)时?你能帮忙吗?
这是我正在尝试的代码。我可以让幻灯片工作,但在到达两端时,它仍然不断地向右或向左滑动。我希望它在到达末端后动态停止,并且进一步单击时应该通过弹出窗口发出警报。
任何人都可以指导我一些示例编码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Sample Slide</title>
<style type="text/css">
.total
{
height:350px;
width:75%;
border:1px solid black;
margin-left:auto;
margin-right:auto;
margin-top:15%;
}
.slidepanel
{
border:1px solid purple;
width:100%;
height:100%;
overflow-x: scroll;
overflow-y: hidden;
}
.box-wrapper
{
width: 400%;
height: 90%;
overflow: hidden;
}
.block
{
/* position:absolute;
background-color:#abc;
left:50px;
width:90px;
height:90px;
margin:5px;*/
border:1px solid red;
width:24.9%;
height:98%;
float:left;
margin-left:auto;
margin-right:auto;
position:relative;
left:0px;
}
.block:nth-child(odd) {
background: cyan;
}
.block:nth-child(even) {
background: red;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript">
$(document).ready(function() {
$("#sright").click(function(){
$("#block1,#block2,#block3 ,#block4").animate({"left": "+=24.9%"}, "slow");
});
});
$(document).ready(function() {
$("#sleft").click(function(){
$("#block1,#block2,#block3 ,#block4").animate({"left": "-=24.9%"}, "slow");
});
});
</script>
</head>
<body>
<div class="total">
<div class="slidepanel">
<center><button id="sleft">«</button> <button id="sright">»</button></center>
<div class="box-wrapper">
<div class="block" id ="block1"></div>
<div class="block" id ="block2"></div>
<div class="block" id ="block3"></div>
<div class="block" id ="block4"></div>
</div>
</div>
</div>
</body>
</html>
最佳答案
[工作演示][1]:http://jsfiddle.net/XJVYj/ (使用上面的代码)
希望这有帮助,这与我之前在同一行的回复不同:Stop .animate() when it reaches last div
无论如何,这个演示和代码将为您提供您想要的东西。
剩下的,我将让代码来说话;
请注意:我试图通过使用 class
属性而不是 div 元素的链接 id
来使其变得简单。
Jquery 代码
$(document).ready(function() {
var cur = 1;
var max = $(".box-wrapper div").length;
$("#sright").click(function(){
if (cur == 1 && cur < max)
return false;
cur--;
$(".block").animate({"left": "+=24.9%"}, "slow");
});
$("#sleft").click(function(){
if (cur+1 > max)
return false;
cur++;
$(".block").animate({"left": "-=24.9%"}, "slow");
});
});
关于jquery - 使用jquery水平滑动div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10764003/