gpt4 book ai didi

javascript - div 不在光标位置

转载 作者:行者123 更新时间:2023-11-28 05:48:39 25 4
gpt4 key购买 nike

我正在尝试创建一个自定义范围 slider 。我无法以百分比形式获取光标位置(即 1 - 100)。

相关代码如下:

var cursorPosition = 1 - clamp((sliderDimention - (e.clientX - startPoint.left)) / sliderDimention, 0, 1);
sliderRange.style.transform = 'scaleX' + '(' + (cursorPosition * sliderDimention - cursorRadius) + ')';

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

我有一个 sliderCursor 和一个 sliderRange (绿色背景)。我能够实现 sliderCursor,但在使用 sliderRange 时遇到问题。它并不完全是光标所在的位置。

如何使 sliderRange 位于光标的准确位置?此外,sliderRange 超出了 slider 边框。我怎样才能防止这种情况发生?

(请不要发布任何 JQuery 或其他“您可以使用‘这个’插件”答案。我这样做是为了学习目的,我想创建自己的答案。谢谢!)

JSFiddile

function RangeSlider( /** DOM Elem */ parentElem) {
var wrapperElem = document.getElementsByClassName('wrapperElem')[0],
slider = document.getElementsByClassName('slider')[0],
sliderCursor = document.getElementsByClassName('sliderCursor')[0],
sliderRange = document.getElementsByClassName('sliderRange')[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 + 1, sliderDimention - 1 - cursorRadius * 2);
sliderCursor.style.transform = 'translateX(' + (cursorPosition) + 'px)';
cursorPosition = 1 - clamp((sliderDimention - (e.clientX - startPoint.left)) / sliderDimention, 0, 1);
sliderRange.style.transform = 'scaleX' + '(' + (cursorPosition * sliderDimention - cursorRadius) + ')';
}

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% - 100px);
border: 1px solid black;
position: relative;
}
.sliderCursor {
width: 14px;
height: 14px;
border-radius: 50%;
border: 2px solid black;
}
.sliderRange {
background-color: green;
width: 1px;
height: 100%;
transform-origin: left;
position: absolute;
}
<div class="wrapperElem">
<div class="slider">
<div class="sliderRange"></div>
<div class="sliderCursor"></div>
</div>
</div>

最佳答案

您的问题是,您正在从 scaleX 函数中减去 cursorRadius,同时,您已经在 cursorPosition 中考虑了光标半径.

尝试:

sliderRange.style.transform = 'scaleX' + '(' + (cursorPosition * sliderDimention) + ')';

function RangeSlider( /** DOM Elem */ parentElem) {
var wrapperElem = document.getElementsByClassName('wrapperElem')[0],
slider = document.getElementsByClassName('slider')[0],
sliderCursor = document.getElementsByClassName('sliderCursor')[0],
sliderRange = document.getElementsByClassName('sliderRange')[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 + 1, sliderDimention - 1 - cursorRadius * 2);
sliderCursor.style.transform = 'translateX(' + (cursorPosition) + 'px)';
cursorPosition = 1 - clamp((sliderDimention - (e.clientX - startPoint.left)) / sliderDimention, 0, 1);
sliderRange.style.transform = 'scaleX' + '(' + (cursorPosition * sliderDimention) + ')';
}

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% - 100px);
border: 1px solid black;
position: relative;
}
.sliderCursor {
width: 14px;
height: 14px;
border-radius: 50%;
border: 2px solid black;
}
.sliderRange {
background-color: green;
width: 1px;
height: 100%;
transform-origin: left;
position: absolute;
}
<div class="wrapperElem">
<div class="slider">
<div class="sliderRange"></div>
<div class="sliderCursor"></div>
</div>
</div>

关于javascript - div 不在光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38270978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com