javascript - topfunction 未定义

标签 javascript html css

你好,我想做一个滚动,当我滚动时,我会出现按钮,我可以点击并返回到顶部,但我有一个错误提示:Topfunction is not defined 而且我没有'不知道为什么它不起作用。那里有我的代码,所以如果你能帮助我,那就太好了!它用于学校元素!

window.onscroll = function() {scrollFunction()};

function scrollFunction() {
    if (document.body.scrollTop > 500 || document.documentElement.scrollTop > 500) {
        document.getElementById("monBoutton").style.display = "block";
    } else {
        document.getElementById("monBoutton").style.display = "none";
    }
}

function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}

#monBoutton {
  /*display: none;*/
  position: fixed;
  bottom: 20px;
  right: 50%;
  z-index: 99;
  font-size: 18px;
  border: none;
  outline: none;
  color:#df5791;
  /*color: white;*/
  cursor: pointer;
  /*padding: 15px;*/
  /*border-radius: 4px;*/
    background-color: #ffffff00;
}

#monBoutton:hover {
  transform:  scale(2);
}


::-webkit-scrollbar {
	/*height: 10px;*/
    width: 7px;
}

/* Track */
::-webkit-scrollbar-track {
    /*box-shadow: inset 0 0 0px grey; */
    /*border-radius: 20px;*/
}
 
/* Handle */
::-webkit-scrollbar-thumb {
    background: pink; 
    border-radius:20px;
    /*box-shadow: inset 0 0 6px rgba(0,0,0,0.5);*/
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="scrolldown.css">
<link rel="stylesheet" type="text/javascript" href="debut.js">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
    
</head>
<body>

<button onclick="topFunction()" id="monBoutton" title="Go to top">
        <i class="fa fa-chevron-up"></i>
</button>

<div style="background-color:black;color:white;padding:30px">Scroll</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div>


</body>
</html>

最佳答案

您的代码确实有效。例如,如果您单击“运行代码段”,您会注意到该函数被触发。

出现“function is not defined”错误肯定意味着无论函数在哪里,都找不到它。您是否将它放在一个单独的文件中,并且可能缺少导入?

关于javascript - topfunction 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52897863/

相关文章:

javascript - 如何检测浏览器内部 JavaScript 错误?

javascript - 这个 <img> 标签是怎么回事?

css - Bootstrap 多级下拉菜单未扩展

html - 基于屏幕宽度的可变列数

javascript - 如何解决我的案例中的单元测试问题

javascript - 同时使用 onepage-scroll.js 和 scrollReveal.js

javascript - 添加表格行和单元格

css - HTML 电子邮件文本被剪裁

javascript - Firebase Realtime Cloud Function - 无法读取空快照的属性

javascript - 我应该在我的网站中包含 "prefix-free"插件吗