Ben
Ben

Reputation: 2564

jquery update var's value for another function after click

$(document).delegate('.open', 'click', function(){
    var t = window.pageYOffset;
    $('body').css({position:'fixed', 'overflow-y':'scroll', 'top':-t + 'px'});
    return false;//prevent href go next page
}); 

$('.close').click(function(){
    $(window).scrollTop(t);

});

I have a page when button click, it will set body fixed and get page's scroll height.

another button click, body will back static and scroll bar will back to the position it was

my problem is var t cant get the value from .open clicked. is any way to solve this.

Upvotes: 0

Views: 47

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

you need to declare it in a share scope(possible as a closure variable in a dom ready handler)

var t =0;
$(document).delegate('.open', 'click', function(){
    t = window.pageYOffset;
    $('body').css({position:'fixed', 'overflow-y':'scroll', 'top':-t + 'px'});
    return false;//prevent href go next page
}); 

$('.close').click(function(){
    $(window).scrollTop(t);

});

Upvotes: 1

Clément Andraud
Clément Andraud

Reputation: 9269

t = 0;

$(document).delegate('.open', 'click', function(){    
    t = window.pageYOffset;
    $('body').css({position:'fixed', 'overflow-y':'scroll', 'top':-t + 'px'});
    return false;//prevent href go next page
}); 

$('.close').click(function(){
    $(window).scrollTop(t);

});

You need to declare a globale var t ;)

Upvotes: 2

Related Questions