JQuery Scroll检测一次

Avi*_*X14 5 jquery jquery-animate

我有一小段JQuery旨在折叠我的标题,更改其尺寸,然后将其粘贴到屏幕的一侧.这一切都与一个部分不同,高度'切换'功能每次用户滚动时都会触发,这真的很烦人.

有没有办法只检测一次滚动或只切换一次?

$(window).scroll(function () {
var width = $(document).width() - 205;
var height = $(document).height();
  $('#me').animate({
    marginLeft: width,
    width: '22px',
    height: 'toggle',
    borderLeftWidth: "10px",
    borderRightWidth: "10px"    
  }, 1000, function() {
    $("#me:first").css("margin-top","-90px");
    var div = $('#me');
    $('#me h1').css("font-size","30px");
    var start = $(div).offset().top;
    $(div).css("height", height);
    $.event.add(window, "scroll", function() {
        var p = $(window).scrollTop();
        $(div).css('position',((p)>start) ? 'fixed' : 'static');
        $(div).css('top',((p)>start) ? '0px' : '');
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

ila*_*rci 10

看看jquery的one()功能http://api.jquery.com/one/

它只会发射一次,然后立即自行移除..