jquery React滚动到顶部动画按钮不起作用

zi8p0yeb  于 2024-01-07  发布在  jQuery
关注(0)|答案(3)|浏览(152)

我尝试在React上实现jquery按钮点击滚动到顶部。我也想实现按钮,它出现在400px之后。我尝试了逻辑,但似乎按钮点击不起作用。
这是我的React组件

  1. import React from "react";
  2. import { createLogEntry } from "./Api";
  3. import Show from "./Show";
  4. export default function Form() {
  5. const [loading, setLoading] = React.useState(false);
  6. const [error, setError] = React.useState("");
  7. const [state, setState] = React.useState({
  8. name: " ",
  9. description: " "
  10. });
  11. const changeHandler = e => {
  12. setState({ ...state, [e.target.id]: e.target.value });
  13. };
  14. const handleSubmit = async e => {
  15. e.preventDefault();
  16. setLoading(true);
  17. try {
  18. await createLogEntry({
  19. name: state.name,
  20. description: state.description
  21. });
  22. } catch (error) {
  23. setError(error.message);
  24. }
  25. setLoading(false);
  26. setState({ name: " ", description: " " });
  27. };
  28. return (
  29. <React.Fragment>
  30. <div className="app">
  31. <button className="btnScrollToTop"> //THIS IS MY SCROLL BUTTON
  32. <i className="material-icons">arrow_upward</i>
  33. </button>
  34. <div className="row">
  35. <form className="col s12" onSubmit={handleSubmit}>
  36. {error ? <p className="error">{error}</p> : null}
  37. <div className="row">
  38. <div className="input-field col s3">
  39. <input
  40. id="name"
  41. type="text"
  42. data-length="4"
  43. onChange={changeHandler}
  44. required
  45. value={state.name}
  46. />
  47. <label htmlFor="input_text">Topic</label>
  48. </div>
  49. </div>
  50. <div className="row">
  51. <div className="input-field col s8">
  52. <textarea
  53. id="description"
  54. className="materialize-textarea"
  55. data-length="120"
  56. onChange={changeHandler}
  57. required
  58. value={state.description}
  59. ></textarea>
  60. <label htmlFor="textarea2">Description</label>
  61. </div>
  62. </div>
  63. <button
  64. className="btn waves-effect blue lighten-1"
  65. type="submit"
  66. name="action"
  67. disabled={loading}
  68. >
  69. {loading ? "Loading..." : "Create Entry"}
  70. </button>
  71. </form>
  72. </div>
  73. </div>
  74. <Show />
  75. </React.Fragment>
  76. );
  77. }

字符串

这是CSS。CSS样式工作正常,如预期。

  1. .btnScrollToTop {
  2. position: fixed;
  3. right: 10px;
  4. bottom: 10px;
  5. width: 50px;
  6. height: 50px;
  7. border-radius: 50%;
  8. background: #e62739;
  9. box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
  10. color: #ffffff;
  11. border: none;
  12. outline: none;
  13. cursor: pointer;
  14. }
  15. .btnScrollToTop:active {
  16. background: #cc2333;
  17. }

这是React的index.html文件,我想问题出在这里。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7. <meta name="theme-color" content="#000000" />
  8. <meta
  9. name="description"
  10. content="Web site created using create-react-app"
  11. />
  12. <link
  13. rel="stylesheet"
  14. href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"
  15. />
  16. <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
  17. <!--
  18. manifest.json provides metadata used when your web app is installed on a
  19. user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
  20. -->
  21. <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
  22. <link
  23. href="https://fonts.googleapis.com/icon?family=Material+Icons"
  24. rel="stylesheet"
  25. />
  26. <title>React App</title>
  27. </head>
  28. <body>
  29. <noscript>You need to enable JavaScript to run this app.</noscript>
  30. <div id="root"></div>
  31. <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
  32. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  33. <script>
  34. const btnScrollToTop = document.getElementsByClassName(".btnScrollToTop"); //HERE I called the button className from the component and below DOM manipulation.
  35. btnScrollToTop.addEventListener("click", () => {
  36. window.scrollTo({
  37. top: 0,
  38. left: 0,
  39. behavior: "smooth"
  40. });
  41. });
  42. </script>
  43. </body>
  44. </html>

w8rqjzmb

w8rqjzmb1#

而不是使用getElementsByClassName只是添加侦听器的所有类,也把它放在document.ready函数。

  1. $(document).ready(function() {
  2. $(".btnScrollToTop").addEventListener("click", () => {
  3. window.scrollTo({
  4. top: 0,
  5. left: 0,
  6. behavior: "smooth"
  7. });
  8. });
  9. });

字符串

f3temu5u

f3temu5u2#

既然你在使用materialize,为什么不使用内置的scrollspy呢?
要滚动到的元素:

  1. <div id="header" class="scrollspy">
  2. ...
  3. </div>

字符串
在目标div上,我们分配一个ID作为目标,并分配一个scrollspy类来获得平滑的动画。
滚动触发器:

  1. <a href="#header">Scroll to trigger!</a>


初始化:

  1. document.addEventListener('DOMContentLoaded', function() {
  2. var elems = document.querySelectorAll('.scrollspy');
  3. var instances = M.ScrollSpy.init(elems);
  4. });


https://materializecss.com/scrollspy.html

展开查看全部
a8jjtwal

a8jjtwal3#

@estinamir的其他答案,

我不知道为什么ReactscrollIntoViewscrollTop动画不工作。即使使用useref。(我使用laravel 10与惯性)
我的解决方案如下:
首先安装npm install jquery,然后导入
import $ from "jquery";
代码如下

  1. const handleScrolltop = () => {
  2. $('html, body').animate({
  3. scrollTop: 0
  4. }, 1500);
  5. };

字符串
如何工作:

  1. <button onClick={handleScrolltop}>Scroll to Top</button>

展开查看全部

相关问题