Javascript 首先单击幻灯片右键,然后使用jquery单击幻灯片左键

Javascript 首先单击幻灯片右键,然后使用jquery单击幻灯片左键,javascript,jquery,Javascript,Jquery,你好,我想做一些像github treeslider的东西 我是javascript新手 但这就是我的工作 jQuery(document).ready(function(){ $('#red-side-menu a').pjax({ container: '#ajax-users-v1', }); var toggle; if (typeof toggle != 'undefined') { toggle = true;

你好,我想做一些像github treeslider的东西

我是javascript新手

但这就是我的工作

jQuery(document).ready(function(){
    $('#red-side-menu a').pjax({
        container: '#ajax-users-v1',
    });


    var toggle;
    if (typeof toggle != 'undefined') {
        toggle = true;
    }

    if (toggle) {
        $('body')
        .bind('start.pjax', function() { $('#ajax-users-v1').show("slide", { direction: "right" });  })
        .bind('end.pjax',   function() {  })
        toggle = false;
    };

    if (!toggle) {
    $('body')
      .bind('start.pjax', function() { $('#ajax-users-v1').show("slide", { direction: "left" });  })
      .bind('end.pjax',   function() {  })
      toggle = true;
    }


    $('#red-edit-full a').pjax({
        container: '#ajax-users-v1',
    });     
});
在上面的文章之前,我使用php来获取$\u get['infolder'],但它似乎没有那么好。 问题现在一直向左滑动


有办法做到这一点吗?

只需在函数外给x一个值即可

var x = true;
jQuery(document).ready(function(){ ...

这里发生的事情是,您声明
var x
,而不给它一个值。因此它是
未定义的
,因此
类型为x!='未定义的“
为false,因此您不会最终将
x
设置为true

(顺便问一下,
x
表示什么?请说出你的变量,一个人可以理解的…)

然后,由于
undefined
是“falsy”,因此执行
if(!x)
中的代码
$('body')
绑定这些事件,并且
x
设置为true


然后,传递给
jQuery(document).ready
的函数结束,并且
x
超出范围,任何人都不会再看到或使用它。所以它的存在是毫无意义的;您可以注释掉所有代码,除了
varx
之前、
if(!x)
内部和
$(“#红色编辑全a”)。pjax…

我认为这可以简化

# this variable maintains state for the lifetime of the page
var direction = "right";
jQuery(document).ready(function(){
  # this whole statement only executes once
  $('#red-side-menu a').pjax({
    container: '#ajax-users-v1'
  });

  # this line executes once
  $('body')
    # this line executes once
    .bind('start.pjax', function() {
      # this line executes every time the start.pjax event reaches the body element
      $('#ajax-users-v1').show("slide", { direction: direction });  
    })
    # this line executes once
    .bind('end.pjax', function() {
      # this line executes every time the end.pjax event reaches the body element (hint: this event is raised after start.pjax is finished) 
      direction = (direction == "right" ? "left" : "right"); 
    });

  # this whole statement only executes once
  $('#red-edit-full a').pjax({
    container: '#ajax-users-v1'
  });     
});

文档就绪事件仅自动运行一次。我已经对代码进行了注释,以显示何时执行每一行或语句块。了解事件处理程序中的代码是页面加载完成后将要运行的唯一代码,这一点非常重要。

好的,您的预期功能是什么,因为不清楚您想要实现什么?我可以做些什么来实现它,当第一次点击它时,它会向右移动,下一次点击它会向左移动。你能描述一下你想要的行为吗。点击是什么?第一次加载页面时,会出现一个链接。当你点击它时,会发生什么?然后,如果你再次点击链接,会发生什么?成功了!,但是我似乎不明白javascript是如何工作的,哈哈。它会在右边,然后在左边生成一个动画。我该怎么做才能使它向右移动,下一次单击后它将向左移动。我认为应该在bind回调函数中测试x的值,而不是两个单独的if语句尝试if/else。当x为true时写入的方式两个if语句都将执行,并且两个处理程序都将绑定到同一个操作。