I'm looking to set a "scroll up" button using JQuery for my rails application. Here's the JQuery script:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(window).scroll(function() {
if($(this).scrollTop() > 300) {
$('#toTop').fadeIn();
} else {
$('#toTop').fadeOut();
}
});
$('#toTop').click(function() {
$('body,html').animate({scrollTop:0},300);
});
});
</script>
Here's CSS for the button:
#toTop {
width:100px;
text-align:center;
padding:5px;
position:fixed;
bottom:5px;
left:50%;
cursor:pointer;
display:none;
color:#333;
font-size:20px;
&:hover {
color: #8B0000;
}
}
When I open a new page, say localhost:3000/signup, the button scrolls up just fine. But if I click on another page, the scroll event doesn't happen. Thus, the button works properly either if I reload the page or open a new one in another tab. What might cause the problem?