javascript - jQuery 单击事件在第一次单击时未运行。 (转到页面顶部操作)

标签 javascript jquery

如标题所述,这是代码。刷新页面后第一次点击将不起作用。

  $(document).ready(function () {
    //Check to see if the window is top if not then display button
    $(window).scroll(function () {
      if ($(this).scrollTop() > 100) {
        $('#myBtn').fadeIn();
      } else {
        $('#myBtn').fadeOut();
      }
    });
  });

  function topFunction() {
    //Click event to scroll to top
    $('#myBtn').click(function () {
      $('html, body').animate({ scrollTop: 0 }, 200);
      return false;
    });

  }

最佳答案

$(document).ready(function () {
    //Check to see if the window is top if not then display button
    $(window).scroll(function () {
      if ($(this).scrollTop() > 100) {
        $('#myBtn').fadeIn();
      } else {
        $('#myBtn').fadeOut();
      }
    });

    //Click event to scroll to top
    $('#myBtn').click(function () {
      $('html, body').animate({ scrollTop: 0 }, 200);
      return false;
    });
  });

关于javascript - jQuery 单击事件在第一次单击时未运行。 (转到页面顶部操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60203673/

相关文章:

javascript - zone.js 和 rxjs 在 angular2 中的重要性

jquery - 在旁边的框中放置复选标记后,希望文本变为粗体

javascript - react 表单无法识别键入的输入

javascript - 将响应列表从水平更改为垂直 CSS

javascript - aws-serverless-express 连接到 mongo 并获取 mongo 文档

javascript - 在 Angular Controller 中迭代 JSON 数组

javascript - 如何用jquery裁剪图像?

javascript - 从其他控件关闭折叠面板( Accordion )

javascript - 在 Javascript 中使用 var 声明变量

javascript - JQuery 可拖动元素在 droppable.append(draggable) 上消失在 droppable 中