Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How to set the limit for the dragging the div vise versa no more than minimum width which is 200

How can I set minimum width for not allowing drag left to no width in the following sample?

const App = () => {
  
    const [initialPos,   setInitialPos] = React.useState(null);
    const [initialSize, setInitialSize] = React.useState(null);
  
    const initial = (e) => {
        
        let resizable = document.getElementById('Resizable');
        
        setInitialPos(e.clientX);
        setInitialSize(resizable.offsetWidth);
        
    }
    
    const resize = (e) => {
        
        let resizable = document.getElementById('Resizable');
      
        resizable.style.width = `${parseInt(initialSize) + parseInt(e.clientX - initialPos)}px`;
      
    }
    
    return(
        <div className = 'Block'>
            <div id = 'Resizable'/>
            <div id = 'Draggable'
                draggable   = 'true'
                onDragStart = {initial} 
                onDrag      = {resize}
            />
        </div>
    );
  
}

ReactDOM.render(<App/>, document.getElementById('root'));

https://codepen.io/erikmartinjordan/pen/dyMEXJb?editors=0110

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can adjust your resize function to limit the width.

const resize = (e) => {
    let resizable = document.getElementById('Resizable');
    let newWidth = parseInt(initialSize) + parseInt(e.clientX - initialPos);
    if (newWidth < 200) {
    newWidth = 200;
    }
    resizable.style.width = `${newWidth}px`;
}

You can set 200 to be any maximum width. Just don't forget to change both 200 values, or even better make a variable for it!

about 4 years ago · Juan Pablo Isaza Report

0

This will allow you to move the end to either left or right with maximum of 200 pixels.

const initial = (e) => {
  let resizable = document.getElementById("Resizable");

  // lock the start position
  if (initialPos == null) {
    setInitialPos(e.clientX);
    setInitialSize(resizable.offsetWidth);
  }
};

const resize = (e) => {
  let resizable = document.getElementById("Resizable");
  // check for the absolute distance from the start point
  if (Math.abs(e.clientX - initialPos) < 200) {
    resizable.style.width = `${
      parseInt(initialSize) + parseInt(e.clientX - initialPos)
    }px`;
  }
};

CodePen

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!