【问题标题】:JS function that is running after a delay延迟后运行的 JS 函数
【发布时间】:2015-10-08 13:03:21
【问题描述】:

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

$(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>

【问题讨论】:

  • 在javascript中查找setTimeout
  • 等等,你要不要延迟?你在说什么向下滚动功能?
  • @AkshayKhandelwal 这与我如何理解它的问题无关
  • 正如您所见,当您单击“向下”时,它会立即转到链接中指向的 div。但是,当单击“向右走”和“向左走”时,会出现延迟,我不确定它来自哪里。我想消除此延迟,以便它可以与用户在页面中上下移动时相同。我希望你现在能更好地理解我。
  • 您首先在此元素上调用 scroll top ,即使它滚动到相同的值(意味着垂直滚动到 0)也需要一秒钟才能完成。 animate() 方法使用 fx 队列

标签: javascript jquery jquery-animate smooth-scrolling


【解决方案1】:

正如您所看到的,当您单击“向下”时,它会立即转到 div 指向链接。然而,当点击“Go Right”和“Go 左”有一个延迟,我不确定它来自哪里。

您首先在此元素上调用 scroll top,即使它滚动到相同的值(意味着垂直滚动到 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>

【讨论】:

  • 完成了这项工作。非常感谢,真的很感激!
  • @DimchoStoimenov 仅供参考,如果您仍想使用两个不同的处理程序(即使您说没有问题),您可以为每个 animate() 方法设置 otpion queue: false
【解决方案2】:

尝试调整选择器,首先将#SectionRight a#SectionLeft a添加到:not().animate();在第二次使用'#SectionLeft a, #SectionRight a' .animate()

$(function() {
  $('a[href*=#]:not([href=#], #SectionLeft a, #SectionRight a)').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(){
    $('#SectionLeft a, #SectionRight a').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>

【讨论】:

猜你喜欢
  • 2015-06-23
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
相关资源
最近更新 更多