H
H
hulktoster2019-06-21 20:58:49
JavaScript
hulktoster, 2019-06-21 20:58:49

How to write such JS code in React?

There is a button that lowers the page to certain coordinates:

<div class="container">
<div class="down">
    <div class="down-text">DOWN
    <button onclick="topFunction2Section()" class="myBtn2" title="Down">&#10095;</button>
    </div>
</div>
</div>


<style>
    .container{
        background: yellow;
        height: 3000px;
        width: 100%;
    }
</style>


<script>
function topFunction2Section() {
           document.body.scrollTop = 940;
           document.documentElement.scrollTop = 940;
}
    </script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-06-21
@hulktoster

const ScrollButton = props => {
  const onClick = () => window.scrollTo({
    top: props.scrollTo,
    behavior: 'smooth',
  });

  return (
    <div className="down" style={{ top: `${props.top}px` }}>
      Scroll to {props.scrollTo}
      <button onClick={onClick}>&#10095;</button>
    </div>
  );
};

const App = () => (
  <div className="container">
    <ScrollButton scrollTo={500} top={50} />
    <ScrollButton scrollTo={1000} top={100} />
    <ScrollButton scrollTo={2000} top={200} />
  </div>
);

.down {
  position: fixed;
}

https://jsfiddle.net/zm0x4ycj/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question