向下滚动到位置
scroll down to position
我有这个代码使页面上下滚动,请告诉我如何使它回到滚动位置,而不是当我点击下页?
<a href="javascript://" id="toDown">Down</a>
<a href="javascript://" id="toTop">Top</a>
<script type="text/javascript">
jQuery.noConflict();
$(window).scroll(function(){
if($(window).scrollTop() > "0"){
$('#toTop').fadeIn();
$('#toDown').hide();
}else if($(window).scrollTop() == "0"){
$('#toTop').fadeOut();
$('#toDown').fadeIn();
}
});
$('#toTop').click(function(){
$('html, body').animate({scrollTop:0}, 0);
$('#toDown').fadeIn();
});
$('#toDown').click(function(){
$('html, body').animate({$('body').height()}, 0);
$('#toDown').fadeOut();
});
</script>
添加一个变量来存储点击" top "时的滚动位置:
var iPosition = null;
如果'top'被点击,保存当前滚动位置:
iPosition = $(window).scrollTop();
如果点击了'down',检查滚动位置是否已经存储;如果是,则移动到该位置,否则移动文档末尾:
$('html, body').animate({scrollTop: (iPosition === null ? $('body').height() : iPosition)}, 0);
完整的源代码:
var iPosition = null; // new
jQuery.noConflict();
$(window).scroll(function(){
if($(window).scrollTop() > "0"){
$('#toTop').fadeIn();
$('#toDown').hide();
}else if($(window).scrollTop() == "0"){
$('#toTop').fadeOut();
$('#toDown').fadeIn();
}
});
$('#toTop').click(function(){
iPosition = $(window).scrollTop(); // new
$('html, body').animate({scrollTop:0}, 0);
$('#toDown').fadeIn();
});
$('#toDown').click(function(){
$('html, body').animate({scrollTop: (iPosition === null ? $('body').height() : iPosition)}, 0); // changed
$('#toDown').fadeOut();
});
相关文章:
- 固定位置菜单时滚动,直到它击中一个相对容器的底部
- jquery:将动画绑定到滚动条位置的更好方法
- 如何查明鼠标按下事件是否发生在滚动条上或元素中的其他任何位置
- 有没有一种方法可以直接从cordova获得滚动位置
- 根据滚动位置动态更改href
- css/js:通过滚动固定位置
- 是否可以使用flexbox显示模型设置元素的滚动位置
- 固定位置侧边栏不滚动-设置内容的高度
- 浏览器持续滚动位置
- 当静态元素与滚动上的固定元素位置重叠时进行检测
- javascript/获取滚动条位置的回调
- Sammy.js缓存和滚动位置时,返回历史
- 使用绝对位置时,jQuery向左滚动不起作用
- PHP保持滚动位置
- 按下后退按钮时,如何保持可滚动区域的滚动位置
- 刷新jqGrid;t在启用无限滚动的情况下保存当前页面位置
- 将滚动条移动到页面中间的控制位置
- 如何使用位置获取滚动功能获取 HTML 中的元素标签
- 滚动位置 (scrollTop) 硬件是否在浏览器中加速
- 鼠标位置滚动