页面重新加载后滚动到特定位置

时间:2016-04-08 14:38:47

标签: javascript jquery ruby-on-rails

我有一个视图页面,底部有一个按钮(haml文件):

.scroll-here

当我按下它时,页面重新加载,我希望它再次自动滚动到[Name of person]| ----------------| =IFERROR(LEN(LEFT(CONCATENATE("!",A2,"!",B2,"!",C2,"!"),FIND(CONCATENATE("!",$F$1,"!"),CONCATENATE("!",A2,"!",B2,"!",C2,"!"))-1))-LEN(SUBSTITUTE(LEFT(CONCATENATE("!",A2,"!",B2,"!",C2,"!"),FIND(CONCATENATE("!",$F$1,"!"),CONCATENATE("!",A2,"!",B2,"!",C2,"!"))-1),"!",""))+1,"")| ...| (但如果页面加载不是因为按下该按钮,焦点应该是常规的,在顶部页面)。

提前致谢!

2 个答案:

答案 0 :(得分:0)

也许你可以在要滚动到的元素中添加一个id,例如:

<div id="scrollHereIfButtonIsPressed"></div>

然后添加&#39;#scrollHereIfButtonIsPressed&#39;按钮的动作事件中的URL,以便在按下按钮转到该URL时滚动到该元素。

前:

<button onclick="window.location.href = window.location.href + '?r='+(+new Date())+'#scrollHereIfButtonIsPressed';window.location.reload(true);">
example button
</button>

答案 1 :(得分:0)

不太熟悉ruby-on-rails,但以下内容可能有效:

 $(document).ready(function () {
     if (window.location.href.indexOf("reload=1") >= 0) {
           $('html, body').animate({
               scrollTop: $(".scroll-here").offset().top
           });
     }
 });

使用以下jQuery代码

function recursivefunc($arr,$key =''){
if(is_array($arr)){
    foreach($arr as $k => $v){
        if (is_array($v) && !empty($v)) {
            recursivefunc($v,$k);
        } else {
            $keys = ($key=='') ? $k : $key;
            echo $keys.":".$v.'</br>';
        }
    }
}
}
recursivefunc($arr);
相关问题