我认为您需要做的是根据范围元素的宽度计算百分比并将偏移元素添加到其中。然后需要将 ack 转换为像素以用于工具提示的绝对定位。我在这里整理了一个例子http://jsfiddle.net/CYSeY/4/
下面的代码
CSS:
#range {
margin-left: 100px;
}
#tip {
position: absolute;
display: none;
border: 1px solid black;
padding: 4px;
}
HTML:
<input type="range" id="range" min="0" max="20" value="0" />
<div id='tip'>Some tip</div>
JavaScript:(onload 或 WinJS 页面就绪事件)
var r = document.getElementById('range');
var max = r.getAttribute('max');
var min = r.getAttribute('min');
var w = r.clientWidth;
//w += r.offsetLeft;
var isDragging = false;
var moveTip = (function (e) {
if (isDragging) {
var posPerc = (r.value / max) * 100;
var pixPos = (posPerc / 100) * w;
pixPos += r.offsetLeft;
document.getElementById('tip').style.display = 'block';
document.getElementById('tip').style.left = pixPos + 'px';
}
});
r.onmousedown = (function (e) {
isDragging = true;
r.addEventListener('mousemove', moveTip, false);
});
r.onmouseup = (function (e) {
isDragging = false;
r.removeEventListener('mousemove', moveTip);
document.getElementById('tip').style.display = 'none';
});
上面的代码可以稍微整理一下,但这是一个很好的起点。'
演示:
var r = document.getElementById('range');
var max = r.getAttribute('max');
var min = r.getAttribute('min');
var w = r.clientWidth;
//w += r.offsetLeft;
var isDragging = false;
var moveTip = (function(e) {
if (isDragging) {
var posPerc = (r.value / max) * 100;
var pixPos = (posPerc / 100) * w;
pixPos += r.offsetLeft;
document.getElementById('tip').style.display = 'block';
document.getElementById('tip').style.left = pixPos + 'px';
}
});
r.onmousedown = (function(e) {
isDragging = true;
r.addEventListener('mousemove', moveTip, false);
});
r.onmouseup = (function(e) {
isDragging = false;
r.removeEventListener('mousemove', moveTip);
document.getElementById('tip').style.display = 'none';
});
#range {
margin-left: 100px;
}
#tip {
position: absolute;
display: none;
border: 1px solid black;
padding: 4px;
}
<input type="range" id="range" min="0" max="20" value="0" />
<div id='tip'>Some tip</div>