简体   繁体   English

我想在 HTML 中留言 window 但 javascript 不起作用

[英]I want to make a message window in HTML but the javascript not work

I made a message window that will show a message when the user slide down the website and hide the message when the user slides up.我做了一个消息 window 当用户向下滑动网站时会显示一条消息,当用户向上滑动时隐藏消息。 I referenced this tutorial but it did not work as intended.我参考了本教程,但它没有按预期工作。

  • This is my code:这是我的代码:
<html>
  <body onscroll="scroll()">
    <div
      id="message"
      style="position:fixed;left:0%;top:30%;height:20%,width:5%;border-radius:10px;background-image:linear-gradient(90deg,rgba(100,255,200,0.5),rgba(100,255,255,0.5),rgba(100,255,200,0.5));box-shadow: 10px 10px 10px 5px rgba(100,100,100,50);"
    >
      <p>
        testmessage:
      </p>
    </div>

    <script language="javascript">
      function scroll() {
        //console.log("打印log日志");实时看下效果
        //console.log("开始滚动!");
      }

      var scrollFunc = function (e) {
        e = e || window.event;
        if (e.wheelDelta) {
          //第一步:先判断浏览器IE,谷歌滑轮事件
          if (e.wheelDelta > 0) {
            //当滑轮向上滚动时
            //console.log("滑轮向上滚动");
            document.getElementById("message").width = 0;
            document.getElementById("message").height = 0;
          }

          if (e.wheelDelta < 0) {
            //当滑轮向下滚动时
            //console.log("滑轮向下滚动");
            document.getElementById("message").width = 20%;
            document.getElementById("message").height = 5%;
          }
        } else if (e.detail) {
          //Firefox滑轮事件
          if (e.detail > 0) {
            //当滑轮向上滚动时
            //console.log("滑轮向上滚动");
            document.getElementById("message").width = 0;
            document.getElementById("message").height = 0;
          }
          if (e.detail < 0) {
            //当滑轮向下滚动时
            //console.log("滑轮向下滚动");
            document.getElementById("message").width = 20%;
            document.getElementById("message").height = 5%;
          }
        }
      };
      //给页面绑定滑轮滚动事件
      if (document.addEventListener) {
        //firefox
        document.addEventListener("DOMMouseScroll", scrollFunc, false);
      }
      //滚动滑轮触发scrollFunc方法 //ie 谷歌
      window.onmousewheel = document.onmousewheel = scrollFunc;
    </script>
  </body>
</html>

I wonder what's wrong with my code and how could I fix it.我想知道我的代码有什么问题,我该如何解决。
Thank you.谢谢你。

You need to invoke your scrollFunc()你需要调用你的scrollFunc()

Add some height, so that it is scrollable.添加一些高度,以便它可以滚动。

    <body onscroll="scrollFunc()" style="height: 5000px;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 2 个功能放在一起并使其在 javascript 和 html5 中按我的意愿工作 - how do I put 2 functions together and make it work as I want in javascript and html5 我想在 javascript/html 中制作随机数生成器 - I want to make Random number generator in javascript/html 我想使用 window.getSelection 制作一个具有粗体功能的 HTML 文本编辑器 - I want to make a HTML text editor having bold functionality using window.getSelection 如何使我的 HTML 命令框与 JavaScript 一起使用? - How can I make my HTML command box work with JavaScript? 如何使用html工作的javascript也可以在cshtml中工作? - How can I make a javascript that works in html also work in cshtml? 我在HTML中缺少什么才能使JavaScript工作? - What am I missing in my html to make the javascript work? 当用户在新窗口中打开时,如何使onclick“ window.location”也起作用 - javascript - How do I make onclick“window.location” also work when user opens in new window 我想在JavaScript的“确认”对话框中显示一条消息,而不使用html或其他任何消息 - I want to display a message in confirm dialog in javascript without using html or any other 我想用JavaScript分割视窗网址 - I want to split the window url with javascript 如何使用结尾的消息制作 HTML/Javascript 倒计时 - How to make HTML/Javascript Countdown with Message at End
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM