为什么在这里我没有滚动活动,而点击事件可能是我做错的事情

发布于 2025-01-30 19:20:14 字数 387 浏览 3 评论 0原文

因此,这是代码,我想将滚动事件对象放在div的滚动中,其中的内容很大,因此DIV变得可以滚动我们如何获得滚动事件,以及为什么它不起作用

import React from "react";

function App() {
  return (
    <div onScroll={(e) => console.log(e)}>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, facilis
      magni d.......
      ...............
      vero!
    </div>
  );
}

export default App;

So here is the code and I want to get the scroll event object on the scroll of div which content is a lot so div becomes scrollable how we can get scroll event and why its not working

import React from "react";

function App() {
  return (
    <div onScroll={(e) => console.log(e)}>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, facilis
      magni d.......
      ...............
      vero!
    </div>
  );
}

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2025-02-06 19:20:14

您的问题是,您不是在滚动div,即onscroll on(从注释中的链接中,即pagesContainer div),而是滚动你的整个身体。查看是否在onScroll div下方添加另一个元素,因为您会在滚动时看到内容变化。在您共享的可重现示例中,您的.pagesContainer div为500VW,因此您需要滚动该Div(即:正文)的容器才能查看内容。相反,您要保留.pagesContainer屏幕的宽度(即:100VW),但将每个页面(.pages)放在容器中该宽度的Div Span 100%(可以使用flex:0 0 100%;,请参见在这里)。这样,您将在pagesContainer div“溢出”其宽度中具有元素。您需要确保向父添加Overflow-x:scroll;,以指定当内容溢出而不是在容器之外渲染时,应添加滚动条。

请参阅下面的示例(查看日志浏览器控制台):

function App() {
  return (
    <div className="pagesContainer" onScroll={(e) => console.log("scroll")}>
      <div className="pages">page1</div>
      <div className="pages">page2</div>
      <div className="pages">page3</div>
      <div className="pages">page4</div>
      <div className="pages">page5</div>
    </div>
  );
}

ReactDOM.render(<App />, document.body);
body {
  margin: 0; /* just to remove whitespace padding */
}
.pagesContainer {
  display: flex;
  width: 100vw; /* change to 100vw*/
  overflow-x: scroll; /* important to specify scrolling behaviour when the content overflows */
}

.pages {
  flex: 0 0 100%; /* make each page 100% of the container */
  height: 100vh;
}
.pages:nth-child(even) {
  background-color: rgb(225, 143, 143);
}
.pages:nth-child(odd) {
  background-color: rgb(130, 130, 215);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

Your issue is that you're not scrolling your div that you have onScroll on (from your link in the comments, that is the pagesContainer div), but rather, are scrolling your entire body. It's easier to see if you add another element below your onScroll div as you'll see that content shift as you scroll. In the reproducible example you shared, your .pagesContainer div is 500vw, so you need to scroll the container of that div (ie: the body) to see the contents. Instead, you want to keep .pagesContainer the width of your screen (ie: 100vw) but make each page (.pages) within the container of your div span 100% of that width (this can be done by using flex: 0 0 100%;, see here). This way, you will have elements within your pagesContainer div "overflowing" its width. You need to ensure that you add overflow-x: scroll; to the parent to specify that it should add a scrollbar when the content overflows rather than render it outside of the container.

See example below (view browser console for logs):

function App() {
  return (
    <div className="pagesContainer" onScroll={(e) => console.log("scroll")}>
      <div className="pages">page1</div>
      <div className="pages">page2</div>
      <div className="pages">page3</div>
      <div className="pages">page4</div>
      <div className="pages">page5</div>
    </div>
  );
}

ReactDOM.render(<App />, document.body);
body {
  margin: 0; /* just to remove whitespace padding */
}
.pagesContainer {
  display: flex;
  width: 100vw; /* change to 100vw*/
  overflow-x: scroll; /* important to specify scrolling behaviour when the content overflows */
}

.pages {
  flex: 0 0 100%; /* make each page 100% of the container */
  height: 100vh;
}
.pages:nth-child(even) {
  background-color: rgb(225, 143, 143);
}
.pages:nth-child(odd) {
  background-color: rgb(130, 130, 215);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

铁轨上的流浪者 2025-02-06 19:20:14

不确定您是要使元素本身滚动或只想让应用程序机。每当您滚动页面时log对象....

但是,如果您想安装对象。这样的文档...

请注意,您需要获取getBoundingClientRect()才能知道屏幕是否在元素上您想要的元素,

function scroll() {
const element = document.querySelector("div")
const rect = element.getBoundingClientRect()
if( rect.top <= 200 )//any number above 100 will work
       {console.log("scrolled")}
}
document.addEventListener('scroll', scroll);

每当您在元素上滚动窗口时,滚动函数都会工作
希望它能帮助您

not sure if you want to make the element itself scrollable or just want the app to console.log the object whenever you scroll the page....

but if you want to console.log the object anyway try to attach an event listener to the document like this ...

note that you need to get the getBoundingClientRect() to know if the screen is on the element you want

function scroll() {
const element = document.querySelector("div")
const rect = element.getBoundingClientRect()
if( rect.top <= 200 )//any number above 100 will work
       {console.log("scrolled")}
}
document.addEventListener('scroll', scroll);

this way whenever you scroll the window over the element, the scroll function will work
hope it helps you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文