javascript - 使用后退按钮将 div 向后滑动

标签 javascript jquery

我在这段代码中使用滑动div:

html:

   <div id="container">

<div id="box1" class="box">Div #1</div>
<div id="box2" class="box">Div #2</div>
<div id="box3" class="box">Div #3</div>
<div id="box4" class="box">Div #4</div>


    </div>

CSS:

    body {
      padding: 0px;    
    }

    #container {
     position: absolute;
     margin: 0px;
     padding: 0px;
     width: 100%;
     height: 100%;
     overflow: hidden;  
    }

  .box {
    position: absolute;
    width: 50%;
    height: 300px;
    line-height: 300px;
    font-size: 50px;
    text-align: center;
    border: 2px solid black;
    left: 150%;
    top: 100px;
    margin-left: -25%;
  }

  #box1 {
   left: 50%;

js:

 $('.box').click(function() {

$(this).animate({
    left: '-50%'
}, 500, function() {
    $(this).css('left', '150%');
    $(this).appendTo('#container');
});

$(this).next().animate({
    left: '50%'
}, 500);
});​

这里有jsfiddle:http://jsfiddle.net/jtbowden/ykbgT/2/

我希望能够使用浏览器的上一个按钮返回到上一个 div。

有办法实现吗?

编辑:

我尝试仅添加 anchor ,但似乎不起作用。

最佳答案

我会使用类似 hashchange 插件 http://benalman.com/projects/jquery-hashchange-plugin/ 的东西

有一个在 hashchange 上执行的函数,将适当的框滑入 View

当您使用浏览器的后退和前进按钮时,它会更改哈希值并且您的代码将运行。

$(window).hashchange(function() {
    var hash = location.hash;
    slideBox(hash.substr(1));
});

关于javascript - 使用后退按钮将 div 向后滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977714/

相关文章:

jQuery自动完成修改Gmail类似匹配

javascript - 当详细信息传递到模态时,id 未定义

javascript - 如何迭代 getElementByClassName 返回

javascript - req.file 未定义(multer,node.js)

jquery - 迁移到knockout.js

jQuery 建议结果与表单值

javascript - 使用放大弹出窗口无法加载图像

javascript - 应用固定位置时的过渡 div

c# - 如何在浏览器中生成 'Are you sure you want to navigate away from this page' 警告消息?

javascript - 从 javascript 数组中删除所有虚假值,包括 NaN 但不包括 0 和空字符串