jQuery on()无效

caf*_*igh 1 jquery

我有下面的代码,动画div(#feedback-form-container)可以滑入和滑出,单击时会跳转到屏幕中心,然后关闭这些幻灯片动画.一旦div中的表单被提交并且表单返回到它的位置,我想再次将这些动画重新打开,但是这不起作用.我试图使用on()没有成功.有人能发现问题吗?我很难过.

/////////////////////feedback form animation//////////////////////
jQuery(document).ready(function($) {
  //find center of screen
  var $screenwidth = $(window).width();
  var $screencenter = $screenwidth / 2 - 177;
  var $startpos = $screencenter + 260;
  var overlay = jQuery('<div id="simpleoverlay"> </div>');

  //hover to slide functions
  $('div#feedback-form-container').hover(function() {
    $('div#feedback-form-container').animate({
      left: '+=260px'
    },
    '2000');
  },

  function() {
    $('div#feedback-form-container').animate({
      left: '-=260px'
    },
    '6000');

  });
  //click to slide to center of screen
  $('div#feedback-form-container').click(function() {
    $('div#feedback-form-container').animate({
      left: '+=' + $screencenter
    },
    {
      duration: '2000',
      easing: 'swing',
      complete: setTimeout(function() {
        overlay.appendTo(document.body)
      },
      500)
    });

    //unbind functions
    if ($('div#feedback-form-container').is(':animated')) $('div#feedback-form-container').off('click').off('hover');
    //set cursor back to default
    $('div#feedback-form-container').css('cursor', 'default');

  });

  $('#wpcf7-f52-t1-o1').submit(function() {

    $('div#feedback-form-container').animate({
      left: '-=' + $startpos
    },
    '6000');

    $('div#feedback-form-container').on('hover').on('click');
  });
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 5

onoff方法不是用来打开和关闭的事件,尽管somwhat混淆的名称.该on方法用于绑定事件,该off方法用于取消绑定事件.

当你取消绑定一个事件时,它就从元素中消失了.要再次使用该事件,您必须再次绑定它,为此您必须指定要为其使用的事件处理程序.仅仅指定事件是不够的,因为该元素不记得之前的事件处理程序.

使用.click(func)与使用具有相同的效果.on('click', func).