3

这应该很容易,但我是新手...

如何使显示功能和单击动画功能合二为一?

function scrollit() {
    $("#scrollDown").show();
    $("#scrollDown").click(function() {
        $('html,body').animate({scrollTop: $('#scrollDown').offset().top}, 1500);
    });
}

见:http: //jsfiddle.net/elasticGurl/3r6Jp/4/

应该发生的是

  1. 按钮被点击
  2. 显示隐藏的 div scrollDown
  3. 与#2同时,它也应该同时向下移动一点

问题是它需要点击两次才能向下移动。

4

4 回答 4

2

您只是绑定了单击事件,您也需要触发该事件:

function toggleDiv() {

$("#scrollDown").show().click(function() {
  $('html,body').animate({scrollTop: $('#scrollDown').offset().top}, 1500);
});
   $("#scrollDown").click();
}
于 2012-11-29T05:15:03.700 回答
2

那是因为您的点击功能在隐藏的 div 上,而不是在您的按钮上

function scrollit() {

    $("#scrollDown").show();
    $('html,body').animate({scrollTop: $('#scrollDown').offset().top}, {duration:1500, queue:false});
}​
于 2012-11-29T05:16:33.623 回答
0

尝试使用jQuery 1.8.2

$(document).on('click','p#scrolltest', function (){
  $("div#scrollDown").show('fast', function(){
       $('body').animate({
           scrollTop: $('#scrollDown').offset().top
           }, 1500);
        });
});

Working Demo

于 2012-11-29T05:23:58.343 回答
0

这是工作示例:http: //jsfiddle.net/FSfRR/1/

   var count = 0;
   $(".checkout_btn").click(function() {
       $("#scrollDown").show();
      count++;
      $('html,body').animate({scrollTop: $('#scrollDown').offset().top}, 1500);
      if(count==1)
          $(this).trigger('click');
      else 
          count = 0;
   });
于 2012-11-29T05:32:02.263 回答