Div走错了位置

时间:2016-05-04 02:16:51

标签: javascript css

我正在编辑this color picker中使用的垂直滑块。它使用transform translateY来调整光标位置。我想改用hsv_barcursor.style.top = ((1 - color.hsv.v) * hsv_barHeight) + 'px'; 。当我这样做时,我显然必须以不同的方式计算它。

原始计算是:(Line: #286

hsv_barcursor.style.transform = 'translateY(calc(' + (color.RND.hsv.v * 10) + '% - ' + cursorRadius + 'px))';

我的更新版本是:( JSFiddle下面的第43行)

top

我的版本的位置是错误的。

  1. 为什么tranlateY translateY的数学运算没有?
  2. 用于html's的正确数学是什么?
  3. 我不是在寻找JQuery的答案,也不是在寻找var luminenceBarWrapper = document.getElementById('luminenceBarWrapper'), hsv_barBGLayer = document.getElementById('bar-bg'), hsv_barcursor = document.getElementById('hsv-barcursor'), hsv_barCursors = document.getElementById('hsv-barcursors'), hsv_barHeight = hsv_barCursors.offsetHeight, cursorRadius = hsv_barcursor.offsetHeight / 2, startPoint, currentTarget, myColor = new Colors(); // Create Event Functions var hsvDown = function(e) { // mouseDown callback e.preventDefault(); if (e.target === hsv_barcursor) currentTarget = e.target.parentNode; else if (e.target === hsv_barCursors) currentTarget = e.target; else return; startPoint = getOrigin(currentTarget); window.addEventListener('mousemove', hsvMove); hsvMove(e); startRender(); }, hsvMove = function(e) { // mouseMove callback myColor.setColor({ v: (hsv_barHeight - (e.clientY - startPoint.top)) / hsv_barHeight * 100 }, 'hsv'); }; // Initial Rendering doRender(myColor.colors); // Adde Events To Objects luminenceBarWrapper.addEventListener('mousedown', hsvDown); window.addEventListener('mouseup', function() { window.removeEventListener('mousemove', hsvMove); stopRender(); }); function doRender(color) { hsv_barcursor.style.transform = 'translateY(calc(' + (color.RND.hsv.v * 10) + '% - ' + cursorRadius + 'px))'; //hsv_barcursor.style.top = ((1 - color.hsv.v) * hsv_barHeight) + 'px'; } var renderTimer, startRender = function(oneTime) { renderTimer = window.setInterval(function() { doRender(myColor.colors); }, 13); // 1000 / 60); // ~16.666 -> 60Hz or 60fps }, stopRender = function() { window.clearInterval(renderTimer); }; 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 }; }输入范围。

    JSFiddle

    
    
    body {
      position: absolute;
    }
    #bar-bg {
      width: 15px;
      height: 500px;
      background-color: greenyellow;
    }
    #hsv-barcursors {
      position: absolute;
      right: -7.5px;
      width: 30px;
      top: 0;
      height: 500px;
      overflow: hidden;
      cursor: pointer;
    }
    #hsv-barcursor {
      position: absolute;
      right: 7.5px;
      width: 11px;
      height: 11px;
      border-radius: 50%;
      border: 2px solid black;
      margin-bottom: 5px;
    }
    
    <script src="https://rawgit.com/PitPik/colorPicker/master/colors.js"></script>
    
    <div id="luminenceBarWrapper">
      <div id="bar-bg"></div>
      <div id="hsv-barcursors" id="hsv_cursors">
        <div id="hsv-barcursor"></div>
      </div>
    </div>
    &#13;
    {{1}}
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

translateY(Npx)如果N > 0向下翻译某些内容,那么当标记结束时您需要translate(0px),而当标记位于组件底部时需要translate(Npx)

您的变量是

  • color.RND.hsv.v当标记为up时为100,而当标记为down时为0
  • hsv_barHeight是组件的高度

第一步是反转color.RND.hsv.v(100 - color.RND.hsv.v)的值,然后我们将其映射到范围[0,1],这只是通过一个简单的除法t = (100 - color.RND.hsv.v) / 100完成的我们这样做,以便使用此值t线性映射高度,使其位于[0, height]范围内,即(100 - color.RND.hsv.v) / 100 * hsv_barHeight这是最终等式

var luminenceBarWrapper = document.getElementById('luminenceBarWrapper'),
  hsv_barBGLayer = document.getElementById('bar-bg'),
  hsv_barcursor = document.getElementById('hsv-barcursor'),
  hsv_barCursors = document.getElementById('hsv-barcursors'),

  hsv_barHeight = hsv_barCursors.offsetHeight,
  cursorRadius = hsv_barcursor.offsetHeight / 2,
  startPoint,
  currentTarget,
  myColor = new Colors();

// Create Event Functions
var hsvDown = function(e) { // mouseDown callback
    e.preventDefault();

    if (e.target === hsv_barcursor) currentTarget = e.target.parentNode;
    else if (e.target === hsv_barCursors) currentTarget = e.target;
    else return;

    startPoint = getOrigin(currentTarget);

    window.addEventListener('mousemove', hsvMove);
    hsvMove(e);
    startRender();
  },
  hsvMove = function(e) { // mouseMove callback
    myColor.setColor({
      v: (hsv_barHeight - (e.clientY - startPoint.top)) / hsv_barHeight * 100
    }, 'hsv');
  };

// Initial Rendering
doRender(myColor.colors);

// Adde Events To Objects
luminenceBarWrapper.addEventListener('mousedown', hsvDown);
window.addEventListener('mouseup', function() {
  window.removeEventListener('mousemove', hsvMove);
  stopRender();
});

function doRender(color) {
  hsv_barcursor.style.transform = 'translateY(' + (100 - color.RND.hsv.v) / 100 * (hsv_barHeight - cursorRadius) + 'px)';
  //hsv_barcursor.style.top = ((1 - color.hsv.v) * hsv_barHeight) + 'px';
}

var renderTimer,
  startRender = function(oneTime) {
    renderTimer = window.setInterval(function() {
      doRender(myColor.colors);
    }, 13); // 1000 / 60); // ~16.666 -> 60Hz or 60fps
  },
  stopRender = function() {
    window.clearInterval(renderTimer);
  };

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
  };
}
body {
  position: absolute;
}
#bar-bg {
  width: 15px;
  height: 500px;
  background-color: greenyellow;
}
#hsv-barcursors {
  position: absolute;
  right: -7.5px;
  width: 30px;
  top: 0;
  height: 500px;
  overflow: hidden;
  cursor: pointer;
}
#hsv-barcursor {
  position: absolute;
  right: 7.5px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid black;
  margin-bottom: 5px;
}
<script src="https://rawgit.com/PitPik/colorPicker/master/colors.js"></script>

<div id="luminenceBarWrapper">
  <div id="bar-bg"></div>
  <div id="hsv-barcursors" id="hsv_cursors">
    <div id="hsv-barcursor"></div>
  </div>
</div>

相关问题