我正在编写一个滚动条来替换浏览器上的滚动条。为了学习经验,我更喜欢自己做,而不是使用 jQuery。
我得到了这段代码,但我有几个问题:
- 它会向下滚动,但不会向上滚动。
- 当我点击 100px 长和 10px 宽的滚动条元素时,鼠标指针似乎捕捉到滚动条元素的顶部,这对于滚动条来说看起来不自然。
html:
<div id="portfolioScrollbar">
<div id="scrollbarTrack"><a id="scrolla" href="#scrollbar"></a></div>
<div id="pbody">
...all the text stuff...
</div>
</div>
编辑-新代码 2013 年 1 月 27 日:
Javascript:
function CloseContact() {
setTimeout (addClose, 2200 );
setTimeout (addContact, 2400 );
var foo = document.getElementById("scrolla");
foo.addEventListener( "mousedown", scrollObject.sbar1, false);
foo.addEventListener( "mouseup", removeAll, false);
foo.addEventListener( "mouseout", removeAll, false);
}
function removeAll(event) {
console.log("REMOVEaLL");
var foo = document.getElementById("scrolla");
foo.removeEventListener("mouseup", scrollObject.sbar2, false);
foo.removeEventListener("mouseout", scrollObject.sbar2, false);
foo.removeEventListener("mousemove", scrollObject.sbar2, false);
}
var scrollObject = function (event) {
var current = 0;
var move = null;
function sbar1(event) {
console.log("mousedown");
event.preventDefault();
var foo = document.getElementById("scrolla");
current = event.clientY;
foo.addEventListener( "mousemove", sbar2, false);
}
function sbar2(event) {
// console.log(event.clientY);
event.preventDefault();
var pbody = document.getElementById("pbody");
var scroll = document.getElementById("scrolla");
move = event.clientY - current;
if (move != 78644 ) {
console.log(move);
scroll.style.marginTop = move + "px";
//current = move;
}
}
return {
sbar1: sbar1,
sbar2: sbar2,
}
}();