Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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 - Fatal编程技术网

Javascript 平滑滚动不适用于单击

Javascript 平滑滚动不适用于单击,javascript,Javascript,我试图在我的网站上实现平滑滚动。我有要用于滚动到元素的链接: <nav id="page-nav" class="nav section-links"> <button class="active" data-target="#info">Info</button> <button data-target="#videos">Videos</button> <button data-target="#stats"

我试图在我的网站上实现平滑滚动。我有要用于滚动到元素的链接:

<nav id="page-nav" class="nav section-links">
   <button class="active" data-target="#info">Info</button>
   <button data-target="#videos">Videos</button>
   <button data-target="#stats">Statistics</button>
</nav>
这是
doscorling
功能:

function getElementY(query) {
  return window.pageYOffset + document.querySelector(query).getBoundingClientRect().top
}

function doScrolling(element, duration) {
  var duration = duration || 1000;
  var startingY = window.pageYOffset
  var elementY = getElementY(element)

  // If element is close to page's bottom then window will scroll only to some position above the element.
  var targetY = document.body.scrollHeight - elementY < window.innerHeight ? document.body.scrollHeight - window.innerHeight : elementY
  var diff = targetY - startingY
  // Easing function: easeInOutCubic
  // From: https://gist.github.com/gre/1650294
  var easing = function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
  var start

  if (!diff) return

    // Bootstrap our animation - it will get called right before next frame shall be rendered.
    window.requestAnimationFrame(function step(timestamp) {
    if (!start) start = timestamp
    // Elapsed miliseconds since start of scrolling.
    var time = timestamp - start
        // Get percent of completion in range [0, 1].
    var percent = Math.min(time / duration, 1)
    // Apply the easing.
    // It can cause bad-looking slow frames in browser performance tool, so be careful.
    percent = easing(percent)

    window.scrollTo(0, startingY + diff * percent)

        // Proceed with animation as long as we wanted it to.
    if (time < duration) {
      window.requestAnimationFrame(step)
    }
  })
}
函数getElementY(查询){ return window.pageYOffset+document.querySelector(query).getBoundingClientRect().top } 功能文件归档(要素、持续时间){ var持续时间=持续时间| | 1000; var startingY=window.pageYOffset var elementY=getElementY(元素) //若元素靠近页面底部,那个么窗口将只滚动到元素上方的某个位置。 var targetY=document.body.scrollHeight-elementY 您的事件侦听器正常,请参阅:

$(文档).ready(函数(){
var navigationLinks=document.GetElementsByCassName('section-links')[0].getElementsByTagName('button');
Array.prototype.forEach.call(navigationLinks,child=>{
console.log(子级);
addEventListener('click',function(){console.log(“click”)});
});
});

信息
视频
统计

您实际执行的操作是将doScrolling函数调用的结果附加为事件处理程序。这就是它不能按预期工作的原因

您应该将doscorling调用包装到函数中,并将目标值存储在闭包中

$(文档).ready(函数(){
var navigationLinks=document.GetElementsByCassName('section-links')[0].getElementsByTagName('button');
Array.prototype.forEach.call(navigationLinks,child=>{
var target=child.getAttribute('data-target');
addEventListener('click',函数(){doscorling(target)});
});
});
函数getElementY(查询){
return window.pageYOffset+document.querySelector(query).getBoundingClientRect().top
}
功能文件归档(要素、持续时间){
var持续时间=持续时间| | 1000;
var startingY=window.pageYOffset
var elementY=getElementY(元素)
//若元素靠近页面底部,那个么窗口将只滚动到元素上方的某个位置。
var targetY=document.body.scrollHeight-elementYfunction getElementY(query) {
  return window.pageYOffset + document.querySelector(query).getBoundingClientRect().top
}

function doScrolling(element, duration) {
  var duration = duration || 1000;
  var startingY = window.pageYOffset
  var elementY = getElementY(element)

  // If element is close to page's bottom then window will scroll only to some position above the element.
  var targetY = document.body.scrollHeight - elementY < window.innerHeight ? document.body.scrollHeight - window.innerHeight : elementY
  var diff = targetY - startingY
  // Easing function: easeInOutCubic
  // From: https://gist.github.com/gre/1650294
  var easing = function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
  var start

  if (!diff) return

    // Bootstrap our animation - it will get called right before next frame shall be rendered.
    window.requestAnimationFrame(function step(timestamp) {
    if (!start) start = timestamp
    // Elapsed miliseconds since start of scrolling.
    var time = timestamp - start
        // Get percent of completion in range [0, 1].
    var percent = Math.min(time / duration, 1)
    // Apply the easing.
    // It can cause bad-looking slow frames in browser performance tool, so be careful.
    percent = easing(percent)

    window.scrollTo(0, startingY + diff * percent)

        // Proceed with animation as long as we wanted it to.
    if (time < duration) {
      window.requestAnimationFrame(step)
    }
  })
}