比如,我删除了页面某个东西,然后,页面刷新了,刷新后,我想让页面处于操作区域,而不是回到页面顶部。我目前想到的办法是:当删除操作成功后,给后端一个请求。后端在页面中埋一个标识。如果该标识存在,页面在载入时,执行document的srcoll方法;否则不执行。不知有更好的办法没。ps:搞来搞去,在页面重载后,要跳到对应的锚点,只更改location.hash肯定不行了,我借助了sesstionStorage来做标记。刷新页面后根据标记来判断是否进行更改location.hash和更改document的scrollTop位置。----------更新答案----------html:<button class="btn">click me</button><div id="header"> <h1>header</h1></div><div id="content"> <h1>content</h1></div><div id="footer"> <h1>footer</h1></div>css:div { height: 400px; background: #eee; margin: 20px;}js:$(function() { console.log('refresh'); var cotentOffset = $('#content').offset(); var to = function() { $('body,html').animate({ scrollTop: cotentOffset.top }, 200); }; if (sessionStorage.getItem('anchor')) { location.hash = 'content'; to(); } $('.btn').on('click', function() { sessionStorage.setItem('anchor', true); location.reload(true);//只能reload });});jsfiddle demo:http://jsfiddle.net/xmlovecss/y6k87/
添加回答
举报
0/150
提交
取消