下面的代码:
body, html {
margin: 0;
height: 100%;
}
.flX {
display: flex;
height: 100%;
}
#wrap {flex-direction: column}
#main {flex-grow: 0}
#preview {
display: flex;
flex-grow: 1;
height: 100%;
width: calc(100% - 256px);
background-color: gainsboro;
}
#properties {
width: 256px;
background-color: linen;
}
#propDrag {
width: 8px;
height: 16px;
background-color: silver;
cursor: pointer;
}
const qSlc = function(e) {return document.querySelector(e)},
main = qSlc("#main"),
properties = qSlc("#properties"),
propDrag = qSlc("#propDrag");
let dragProp = false;
function onMouseMove(e) {
let w=window.innerWidth,
x=e.clientX,
pw=w-x;
if (dragProp) {
properties.style.width = pw + 'px'
console.log(w,x,pw,properties.style.width)
}
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', () => dragProp = false);
propDrag.addEventListener('mousedown', () => dragProp = true);
我注意到当我移动propDrag的时候,指针和元素之间有一个差异,并且随着我向左移动而增加,我还注意到chrome〉inspector〉elements中的properties.width和它的viewport之间有一个差异(当我将鼠标悬停在chrome〉inspector〉elements〉properties上时)。我用屏幕标尺测量了一下,视口中属性矩形下方显示的宽度是正确的。为什么会出现这种差异,如何解决?
|