Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cursors position in percentage

I'm trying to create a custom range slider. I'm having trouble getting the cursors position in percentage (i.e. 1 - 100).

Here's the relevant code:

var cursorPosition = e.clientX - startPoint.left - cursorRadius;
cursorPosition = clamp(cursorPosition, startPoint.left- cursorRadius, sliderDimention- cursorRadius*2);

function clamp(value, min, max) {
    return Math.min(Math.max(value, min), max);
}

I tried the following:

console.log((cursorPosition / sliderDimention) * 100);

It almost works correctly. The only problem is, it is from -2 - 95 (rounded). How can I get the sliderCursors position in percentages?

(Please don't post any JQuery or other "you can use 'this' plugin" answers. I'm doing this for learning purposes, and I want to create my own. Thanks!)

JSFiddle

function RangeSlider( /** DOM Elem */ parentElem) {
  var wrapperElem = document.getElementsByClassName('wrapperElem')[0],
    slider = document.getElementsByClassName('slider')[0],
    sliderCursor = document.getElementsByClassName('sliderCursor')[0];

  var sliderDimention = slider.offsetWidth,
    cursorRadius = sliderCursor.offsetHeight / 2,
    startPoint,
    currentTarget;


  function sliderDown(e) {
    e.preventDefault();

    currentTarget = null;
    var sliderWithDescendents = wrapperElem.querySelectorAll('*');
    for (var i = 0; i < sliderWithDescendents.length; i++) {
      sliderWithDescendents[i]
      if (sliderWithDescendents[i] === e.target || wrapperElem === e.target) {
        currentTarget = wrapperElem.children[0];
        break;
      }
    }
    if (currentTarget === null) return;

    startPoint = getOrigin(currentTarget);
    sliderDimention = slider.offsetWidth;

    window.addEventListener('mousemove', sliderMove);
    sliderMove(e);
  }

  function sliderMove(e) {
    var cursorPosition = e.clientX - startPoint.left - cursorRadius;
    cursorPosition = clamp(cursorPosition, startPoint.left - cursorRadius, sliderDimention - cursorRadius * 2);
    console.log((cursorPosition / sliderDimention) * 100);
    sliderCursor.style.transform = 'translateX(' + (cursorPosition) + 'px)';
  }

  function mouseUpEvents() {
    window.removeEventListener('mousemove', sliderMove);
  }
  wrapperElem.addEventListener('mousedown', sliderDown);
  window.addEventListener('mouseup', mouseUpEvents);
}

var sliderTest = document.getElementById('sliderTest');
var test = new RangeSlider(sliderTest);






function clamp(value, min, max) {
  return Math.min(Math.max(value, min), max);
}


function getOrigin(elm) {
  var box = (elm.getBoundingClientRect) ? elm.getBoundingClientRect() : {
      top: 0,
      left: 0
    },
    doc = elm && elm.ownerDocument,
    body = doc.body,
    win = doc.defaultView || doc.parentWindow || window,
    docElem = doc.documentElement || body.parentNode,
    clientTop = docElem.clientTop || body.clientTop || 0, // border on html or body or both
    clientLeft = docElem.clientLeft || body.clientLeft || 0;

  return {
    left: box.left + (win.pageXOffset || docElem.scrollLeft) - clientLeft,
    top: box.top + (win.pageYOffset || docElem.scrollTop) - clientTop
  };
}
.wrapperElem {
  height: 18px;
  width: 100%;
  cursor: pointer;
  display: flex;
}
.slider {
  height: 100%;
  width: calc(100% - 62px);
  border: 1px solid black;
}
.sliderCursor {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid black;
}
<div class="wrapperElem">
  <div class="slider">
    <div class="sliderCursor"></div>
  </div>
</div>
like image 463
Jessica Avatar asked Jul 05 '16 21:07

Jessica


1 Answers

You forgot take into account few things:

  1. Borders of .slider for defining cursorPosition
  2. Subtract borders and start position of .slider from sliderDimention

    cursorPosition = clamp(cursorPosition, startPoint.left - cursorRadius + 1, sliderDimention - 1 - cursorRadius*2);
    
    console.log((cursorPosition / (sliderDimention - 2 - startPoint.left - cursorRadius)) * 100);
    

JSFiddle

like image 116
form3 Avatar answered Sep 22 '22 00:09

form3