【问题标题】:sliding divs horizontally using jquery使用jquery水平滑动div
【发布时间】:2012-05-26 05:59:16
【问题描述】:

我正在尝试为我的页面设计一个滑块,我想知道是否有人知道如何创建从左到右的滑动 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">&laquo;</button> <button id="sright">&raquo;</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>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    [工作演示][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");
    });
    });​
    

    【讨论】:

      【解决方案2】:

      尝试像这样在 px 中制作动画...

      确保你所有的 div 在动画时都可用,然后在一个块 div 上试试这个

      $('#block1').animate({"left": "-=150px"}, 500);

      计算可用图像的数量。

      点击时确保不超过最大图像,然后您再次恢复到第一个图像,动画回到第一个图像到 0px。

      同样,检查第一张图像,然后当总图像宽度小于总滑块宽度时,不允许将图像动画到右侧。

      看看这是否有帮助slider tut

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-01
        • 1970-01-01
        • 2015-03-20
        相关资源
        最近更新 更多