Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 延迟后滚动_Javascript_Scroll_Delay_Smooth - Fatal编程技术网

Javascript 延迟后滚动

Javascript 延迟后滚动,javascript,scroll,delay,smooth,Javascript,Scroll,Delay,Smooth,我试图使我的网站滚动到特定的职位是在单独的页面。我认为这背后的PHP部分可以为我生成锚,但我仍然坚持使用JS部分。我设法使网页从位置0,0开始,然后转到静态锚标记。我要解决的是如何让JS从当前URL获取锚定标记,然后在短时间延迟后平滑地滚动到给定标记 我目前的代码是: $(document).ready(function() { if (location.hash) { window.scrollTo(0, 0); } }); setTimeout("win

我试图使我的网站滚动到特定的职位是在单独的页面。我认为这背后的PHP部分可以为我生成锚,但我仍然坚持使用JS部分。我设法使网页从位置0,0开始,然后转到静态锚标记。我要解决的是如何让JS从当前URL获取锚定标记,然后在短时间延迟后平滑地滚动到给定标记

我目前的代码是:

$(document).ready(function() {
    if (location.hash) {
        window.scrollTo(0, 0);
      }
});

setTimeout("window.location.hash = '#scroll';", 5000);
我发现以下截取了URL上的锚标记,但我不确定如何使其在延迟后执行

    $(document).ready(function() {
      function filterPath(string) {
      return string
        .replace(/^\//,'')
        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
        .replace(/\/$/,'');
      }
      var locationPath = filterPath(location.pathname);
      var scrollElem = scrollableElement('html', 'body');

      $('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if (  locationPath == thisPath
        && (location.hostname == this.hostname || !this.hostname)
        && this.hash.replace(/#/,'') ) {
          var $target = $(this.hash), target = this.hash;
          if (target) {
            var targetOffset = $target.offset().top;
            $(this).click(function(event) {
              event.preventDefault();
              $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
                location.hash = target;
              });
            });
          }
        }
      });

      // use the first element that is "scrollable"
      function scrollableElement(els) {
        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
          var el = arguments[i],
              $scrollElement = $(el);
          if ($scrollElement.scrollTop()> 0) {
            return el;
          } else {
            $scrollElement.scrollTop(1);
            var isScrollable = $scrollElement.scrollTop()> 0;
            $scrollElement.scrollTop(0);
            if (isScrollable) {
              return el;
            }
          }
        }
        return [];
      }

    }); 
$(文档).ready(函数(){
函数筛选器路径(字符串){
返回字符串
.替换(/^\/,'')
.replace(/(索引|默认值)。[a-zA-Z]{3,4}$/,“”)
.替换(/\/$/,'');
}
var locationPath=filterPath(location.pathname);
var scrolleem=scrollableElement('html','body');
$('a[href*=#]')。每个(函数(){
var thisPath=filterPath(this.pathname)| | locationPath;
如果(locationPath==此路径
&&(location.hostname==this.hostname | |!this.hostname)
&&this.hash.replace(/#/,''){
var$target=$(this.hash),target=this.hash;
如果(目标){
var targetOffset=$target.offset().top;
$(此)。单击(函数(事件){
event.preventDefault();
$(scrollElem).animate({scrollTop:targetOffset},400,function(){
location.hash=目标;
});
});
}
}
});
//使用第一个“可滚动”的元素
函数可滚动元素(els){
for(var i=0,argLength=arguments.length;i 0){
返回el;
}否则{
$scrollElement.scrollTop(1);
变量isScrollable=$scrollElement.scrollTop()>0;
$scrollElement.scrollTop(0);
如果(可循环使用){
返回el;
}
}
}
返回[];
}
}); 

我认为setTimeout不接受任何作为字符串传递的旧代码,只接受函数名。请尝试改用匿名函数:


setTimeout(函数(){window.location.hash='#scroll';},5000)

我使用在上找到的代码片段设法解决了问题。它在页面加载时滚动到锚定标记,始终从页面的最顶端开始

$(window).bind("ready", function() {
   var urlHash = window.location.href.split("#")[1];
    $('html,body').animate({scrollTop:$('a[href="#'+urlHash+'"]').offset().top}, 1500);        
});

将字符串传递给setTimeout/Interval是有效的,并且可以工作,这不是一个好的做法<代码>设置超时(“警报('Hello!')”,1000)