无法使用 window.scrollTo(0,content_scroll_pos);使窗口滚动到所需位置
cannot make window scroll to desired position with window.scrollTo(0,content_scroll_pos);
我手头有一个非常简单的任务,但有些东西无法正常工作:|
有一个按钮可以打开全屏菜单,淡出所有其他内容。还有另一个按钮可以关闭菜单并淡入内容。
但菜单窗口高度可能与内容高度大不相同。因此,当用户单击打开菜单时,他会跳到窗口顶部。但是当用户关闭窗口时,他应该返回到原始窗口滚动位置(正是单击打开菜单的位置)。
到目前为止,我有这个*window.scrollTo(0,content_scroll_pos);
不起作用!好吧,它跳到 0 0。但鉴于我知道content_scroll_pos变量不是 0。
//Content window position, before calling menu
var content_scroll_pos;
//Show menu
$("#open_nav").on("click", function(){
//
content_scroll_pos = $(document).scrollTop();
//
$("#grid_index, #sidebar-big").fadeOut("fast", function(){
window.scrollTo(0,0);
$("nav").fadeIn();
});
return false;
});
//Hide menu
$("#close_nav").on("click", function(){
$("nav").fadeOut("fast", function(){
window.scrollTo(0,content_scroll_pos);
$("#grid_index, #sidebar-big").fadeIn();
});
return false;
});
通过重新排列事件来修复问题:
//Content window position, before calling menu
var content_scroll_pos;
//Show menu
$("#open_nav").on("click", function(){
//
content_scroll_pos = $(window).scrollTop();
//
$("#grid_index, #sidebar-big").fadeOut("fast", function(){
window.scrollTo(0,0);
$("nav").fadeIn("fast");
});
return false;
});
//Hide menu
$("#close_nav").on("click", function(){
$("nav").fadeOut("fast", function(){
$("#grid_index, #sidebar-big").fadeIn("fast");
window.scrollTo(0,content_scroll_pos);
});
return false;
});
相关文章:
- 为什么不'我的窗口滚动事件根本没有启动.其他答案没有解决问题
- AngularJS窗口滚动事件未触发
- Jquery - 使用窗口滚动时抖动滚动
- 摩尔工具覆盖窗口滚动
- 窗口滚动不起作用
- 图像宽度上的转换不适用于窗口滚动
- colorbox jquery lightbox随父窗口滚动
- jQuery-在窗口滚动上运行一个函数,没有任何延迟
- 长表滚动,但阻止窗口滚动
- 使用窗口滚动而不是容器滚动隐藏
- 使用窗口滚动查找光标的位置
- 窗口滚动 Y = 0 在高图表重绘()
- 灯箱演变防止窗口滚动问题
- jQuery 或 Javascript - 如何禁用窗口滚动而不使用 overflow:hidden;.
- 获取窗口滚动或 yui 中的滚动 Y 位置
- 如何使用锚滚动()将窗口滚动到AngularJS中的元素
- 淡入和淡出在窗口滚动后发生多次
- j查询在scrollTop()调用后重置窗口滚动
- 禁用任何类型的浏览器窗口滚动
- “返回顶部”按钮仅在窗口滚动时可见(但移动设备不起作用)