Your var cs
is local variable. if you want to use it in another function,try to initialize it in the global scope.
Also, You should define it in ‘px’. mean add px
at the end when you assign it as css
var sc = $(window).scrollTop();
$(window).scroll(function (event) {
sc = $(window).scrollTop();
$("body main").css('top',sc + 'px');
});
$("header .nav-click").click(function(){
$("body main").css('top',sc + 'px');
});
I have this CSS code by:
How can I get a fixed address-bar in Google Chrome for Android with javascript
Here is the preview image:
http://s7.picofile.com/file/8388260676/photo.jpg
When I scroll down the page, The code shows < main style=”top:2268px;” > ;
But when I click number one, The code shows < main style=”top:0px;” >
I want it to be 2268px when I click it.
My code (javascript):
$(window).scroll(function (event) {
var sc = $(window).scrollTop();
// console.log(sc);
$("body main").css('top',sc);
});
$("header .nav-click").click(function(){
$("body main").css('top',sc);
});