Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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 未捕获引用错误:未定义目标(SmoothScroll)_Javascript - Fatal编程技术网

Javascript 未捕获引用错误:未定义目标(SmoothScroll)

Javascript 未捕获引用错误:未定义目标(SmoothScroll),javascript,Javascript,我用于平滑滚动的JS代码返回了一个错误,即目标未定义,我不能100%确定这意味着什么,因此我自己无法修复该错误 错误: 未捕获引用错误:未定义目标 我需要定义什么才能使其正常工作 工作示例: JS: 您正在使用名为target的变量,就像这样if(target==document)但我看不到您在严格模式下声明和启动此变量的位置所有变量都必须显式声明。我假定参数body应该命名为target(但我猜是这样的)。另一种可能性:通常从事件中检索target,因此您可能缺少事件的参数(然后可以从该参数中

我用于平滑滚动的JS代码返回了一个错误,即目标未定义,我不能100%确定这意味着什么,因此我自己无法修复该错误

错误:

未捕获引用错误:未定义目标

我需要定义什么才能使其正常工作

工作示例:

JS:


您正在使用名为
target
的变量,就像这样
if(target==document)
但我看不到您在严格模式下声明和启动此变量的位置所有变量都必须显式声明。我假定参数
body
应该命名为
target
(但我猜是这样的)。另一种可能性:通常从事件中检索
target
,因此您可能缺少事件的参数(然后可以从该参数中检索
目标
)。您的作用域中没有任何名为
target
的声明变量,因此您的scopreManager无法在词法作用域中找到此变量。看起来它应该是
函数SmoothScroll(目标、速度、平滑)
  function init(){
    new SmoothScroll(document,30,24)
  }

  function SmoothScroll(body, speed, smooth) {
    if (target === document)
      target = (document.scrollingElement 
                || document.documentElement 
                || document.body.parentNode 
                || document.body) // cross browser support for document scrolling

    var moving = false
    var pos = target.scrollTop
    var frame = target === document.body 
                && document.documentElement 
                ? document.documentElement 
                : target // safari is the new IE

    target.addEventListener('mousewheel', scrolled, { passive: false })
    target.addEventListener('DOMMouseScroll', scrolled, { passive: false })

    function scrolled(e) {
      e.preventDefault(); // disable default scrolling

      var delta = normalizeWheelDelta(e)

      pos += -delta * speed
      pos = Math.max(0, Math.min(pos, target.scrollHeight - frame.clientHeight)) // limit scrolling

      if (!moving) update()
    }

    function normalizeWheelDelta(e){
      if(e.detail){
        if(e.wheelDelta)
          return e.wheelDelta/e.detail/40 * (e.detail>0 ? 1 : -1) // Opera
        else
          return -e.detail/3 // Firefox
      }else
        return e.wheelDelta/120 // IE,Safari,Chrome
    }

    function update() {
      moving = true

      var delta = (pos - target.scrollTop) / smooth

      target.scrollTop += delta

      if (Math.abs(delta) > 0.5)
        requestFrame(update)
      else
        moving = false
    }

    var requestFrame = function() { // requestAnimationFrame cross browser
      return (
        window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(func) {
          window.setTimeout(func, 1000 / 50);
        }
      );
    }()
  }