Home > Mobile >  Constraining block displacement using transform translate, mousemove and pageX (pageY)
Constraining block displacement using transform translate, mousemove and pageX (pageY)

Time:12-22

I am resizing and positioning a box using the mousemove event. Those. i change transform translate and width (height) with pageX (pageY). But due to the fact that the mouse event mousemove does not always have time to be processed (for example, if you move the mouse quickly) or does not have time to read conditions, the block goes out of bounds. Question: what do I need to do in this case so that the block does not go beyond the boundaries?

This is how it looks roughly. Those. in this example, the second_block is outside the first_block (500px), i.e. it does not have time to read the condition. How should this issue be resolved? Also for convenience https://jsfiddle.net/ManuOP/t1r4szdx/3/

<div id="first_block" >
    <div id="auxiliary_block"> 
        <div id="second_block" ></div>
        <input id="point"  name="name_point" type="button">
    </div>
</div>
<script src="1.block_in_center_question.js"></script>

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
div {
    display: flex;
    flex-direction: column;
}
div.first_block {
    height: 300px;
    width: 500px;
    background: green;
}
div#auxiliary_block {
    position: absolute;
}
div.second_block {
    height: 200px;
    width: 300px;
    background: orange;
}
input.point {
    position: absolute;
    cursor: pointer;
    height: 14px;
    width: 14px;
    border: none;
    background: black;
    right: -7px;
    top: 50%;
}

"use strict";
let second_block = document.getElementById('second_block');
let point = document.getElementById('point');
function change_second_block() {
    if(second_block.clientWidth < 500) {
        second_block.style.width = `${start_x   event.pageX}px`;
    }
}
point.addEventListener('mousedown', (event) => {
    window.start_x = second_block.clientWidth - event.pageX;
    document.addEventListener('mousemove', change_second_block);
});

CodePudding user response:

You could just test the new width and if it's too large then constrain it to be no more than the maximum.

This snippet does this for the x direction and forces it to remain at or below 500px.

"use strict";
let second_block = document.getElementById('second_block');
let point = document.getElementById('point');

function change_second_block() {
  if (second_block.clientWidth < 500) {
    second_block.style.width = (start_x   event.pageX) < 500 ? `${start_x   event.pageX}px` : '500px';
  }
}
point.addEventListener('mousedown', (event) => {
  window.start_x = second_block.clientWidth - event.pageX;
  document.addEventListener('mousemove', change_second_block);
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

div {
  display: flex;
  flex-direction: column;
}

div.first_block {
  height: 300px;
  width: 500px;
  background: green;
}

div#auxiliary_block {
  position: absolute;
}

div.second_block {
  height: 200px;
  width: 300px;
  background: orange;
}

input.point {
  position: absolute;
  cursor: pointer;
  height: 14px;
  width: 14px;
  border: none;
  background: black;
  right: -7px;
  top: 50%;
}
<div id="first_block" >
  <div id="auxiliary_block">
    <div id="second_block" ></div>
    <input id="point"  name="name_point" type="button">
  </div>
</div>

CodePudding user response:

You can work around this issue if the size of the change in the item is above a certain limit, or by checking the limit and stopping the update. I prevented the overflow caused by rapid mouse movement by updating its code as follows:

function change_second_block()
{
    console.log("Event.PageX: "   event.pageX);

    if(event.pageX < 500 )
    {
        if(second_block.clientWidth < 500)
        {
            second_block.style.width = `${start_x   event.pageX}px`;
        }
    }
}

Related Resources

  • Related