javascript - 如何保存iframe(scrollto)的x和y?是否可以?

标签 javascript iframe scroll

在寻找了几个小时但没有找到答案后,我决定来找你,哦,我一直向他们寻求帮助的伟大社区。我有一个 iframe,当我的用户滚动时,他们会离开,然后再回来,我们想使用scrollto 返回到正确的位置。那么我如何保存/导出/添加此 iframe 的 x 和 y 变量,或者这是不可能的?

最佳答案

您可以使用此 jquery 插件将 x,y 变量保存到 cookie:https://github.com/carhartl/jquery-cookie

示例:

$(window).unload(function() {
    //save scroll on unload
    $.cookie('scroll_x',$("#yourElementId").scrollLeft());
    $.cookie('scroll_y',$("#yourElementId").scrollTop());
});
$(document).ready(function() {
    //restore scroll when the dom is ready
    var x = $.cookie('scroll_x');
    var y = $.cookie('scroll_y');
    if( x != null ) $("#yourElementId").scrollLeft(x);
    if( y != null ) $("#yourElementId").scrollTop(y);
});

您想要滚动的元素的 ID 是“yourElementId”。

关于javascript - 如何保存iframe(scrollto)的x和y?是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12119078/

相关文章:

javascript - 移动webkit内存消耗

javascript - 当散列更改时防止页面滚动

android - 检测 RecyclerView 滚动时何时到达最底部位置

javascript - Treeview 中的递归复选框选择 | Telerik MVC

javascript - jQuery 验证 select2

html - 对齐 iframe

javascript - 获取 iframe 表面中发生的 JavaScript 异常

css - 尝试仅使用 div 和 css 而不是表格来实现格式化

scroll - 在 Windows Phone 8 Web 浏览器中禁用滚动和缩放

javascript - 移动谷歌地图中心javascript api