-1

I have a jQuery script that triggers a click based on scroll position. I want to be able to click this element again if the user returns to the top of the page so the contact form will hide itself. Problem is that it creates a loop.

    jQuery(window).scroll(function (event) {
        var y = $(this).scrollTop();
        if (y > 400) {
        $("#contactable-inner").click();
        $("#contactable-inner").unbind('click');}
    });

You can go here to see what I am working on: http://algk.me

UPDATE:

    $(window).scroll(function(){
        var y = $("body").scrollTop();
        var hidden = $('.hidden');
        if (y >= 1000 && (hidden.hasClass('visible'))){
        hidden.animate({"left":"-1000px"}, "slow").removeClass('visible');
        } else {
        hidden.animate({"left":"0px"}, "slow").addClass('visible');
        }
  });

Any idea how I could get this to work on scroll?

4

2 回答 2

1

What if you used jQuery's .show()? Instead of simulating a click.

jQuery(window).scroll(function (event) {
    var y = $(this).scrollTop();
    if (y > 400) {
      $("#contactable").show();
    }
    else {
      $("#contactable").hide();
    }
});
于 2013-07-24T00:16:03.663 回答
0

If you want the click handler to be executed only once, then you can use one, which will make your handler to be handled only once.

$("#contactable-inner").one('click', function(e){
    // code goes here
});

$(window).scroll(function (event) {
    var y = $(this).scrollTop();
    if (y > 400) {
        $("#contactable-inner").click();
    }
});

DEMO.

于 2013-07-24T00:31:43.343 回答