javascript - 延迟后运行的JS函数

标签 javascript jquery jquery-animate smooth-scrolling

我有两个功能。我想将它们组合起来,以便当您从左到右移动(或反之亦然)时,它不会有延迟(就像向下滚动功能一样)。

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});


$(function(){
    $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().left;
                $('html,body').animate({scrollLeft: targetOffset}, 1000);
                return false;
            }
        }
    });
});
body{
    padding:0;
    margin:0;
    overflow:hidden;
}

#Home{
    position:relative;
	width:100vw;
	height:100vh;
	background-color:#FFF;
}

#SectionLeft{
    position:relative;
	width:100vw;
	height:100vh;
	background-color:#989898;
	float:left;
}

#SectionRight{
    position:relative;
	margin-left:100vw;
	width:100vw;
	height:100vh;
    color:000;
	background-color:#838383;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<div id="Home">
    <a href="#SectionLeft">Go Down</a>
</div>
         
<div id="SectionLeft">
     <a href="#SectionRight">Go Right</a>   
</div>

<div id="SectionRight">
     <a href="#SectionLeft">Go Left</a>   
</div>

最佳答案

As you can see when you click "Go down" it immediately goes to the div directed in the link. However, when clicking on "Go Right" and "Go Left" there is a delay that I am not sure from where it is coming.

您首先在此元素上调用滚动顶部,即使它滚动到相同的值(意味着垂直滚动到 0),也需要一秒钟才能完成。 animate() 方法使用 fx 队列,因此所有动画都放入队列中,一次只运行一个。

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top,
          scrollLeft: target.offset().left
        }, 1000);
        return false;
      }
    }
  });
});
body{
    padding:0;
    margin:0;
    overflow:hidden;
}

#Home{
    position:relative;
	width:100vw;
	height:100vh;
	background-color:#FFF;
}

#SectionLeft{
    position:relative;
	width:100vw;
	height:100vh;
	background-color:#989898;
	float:left;
}

#SectionRight{
    position:relative;
	margin-left:100vw;
	width:100vw;
	height:100vh;
    color:000;
	background-color:#838383;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<div id="Home">
    <a href="#SectionLeft">Go Down</a>
</div>
         
<div id="SectionLeft">
     <a href="#SectionRight">Go Right</a>   
</div>

<div id="SectionRight">
     <a href="#SectionLeft">Go Left</a>   
</div>

关于javascript - 延迟后运行的JS函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016700/

相关文章:

javascript - Firefox中如何在子窗口打开时自动关闭父窗口

javascript - 用 CSS 和一点 JS 来设计输入类型文件的样式,这样可以吗?

JavaScript/jQuery - onhashchange 事件解决方法

c# - DNN 6 jQuery 选项卡

c++ - 在 Borland 2006 中创建 TAnimate

javascript - Node.js 中的单线程到底是如何工作的?

javascript - Three.js - 查找上次鼠标位置和当前鼠标位置之间的所有交点

jquery - 页脚在调整大小时丢失列背景颜色

javascript - 与 setInterval() 冲突的动画

jquery - 如何使用 jQuery 使用 animate 和逗号来增加数字?