我试图在没有JQuery的情况下编写一个水平滑块,用于我自己练习/享受的项目。
以下是相关代码:
function moveit() {
"use strict";
document.getElementById("position").style.left = window.event.clientX + "px";
}
window.onload = function () {
"use strict";
findtime();
document.getElementById("scrollbar").style.width = document.getElementById("thevideo").offsetWidth + "px";
var mousemove;
document.getElementById("scrollbar").onclick = function () {
mousemove = window.setInterval("moveit()", 1000);
};
document.getElementById("scrollbar").mouseup = function () {
window.clearInterval(mousemove);
};
};
毋庸置疑,我遇到了问题。它会不断在Chrome,Firefox等上生成错误:
Uncaught TypeError: Cannot read property 'clientX' of undefined
现在,如果我运行以下代码,它可以工作(但是对于跟随鼠标位置没有用):
document.getElementById("position").style.left = 12 + "px";
HTML如下:
<?php include("header.php"); ?>
<div>
<video id="thevideo">
<source src="movie.ogv" type="video/ogg" />
</video>
</div>
<div>
<span id="currenttime" contenteditable="true">0:00</span> / <span id="totaltime"></span>
</div>
<div id="scrollbar">
<div id="position" draggable="true"></div>
</div>
<?php include("footer.php"); ?>
答案 0 :(得分:0)
这是我回过头来做的事情,也许你可以根据自己的计划进行调整。
var scrollTimer;
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}
function scroll(down) {
var scrollframe = document.getElementById("contentframe");
var curY = document.all?
scrollframe.contentWindow.document.body.scrollTop
: scrollframe.contentWindow.window.pageYOffset;
var delta = 5;
var newY = down? curY+delta : curY-delta;
scrollframe.contentWindow.scrollTo(0,newY);
}
function autoscroll(down) {
scroll(down);
scrollTimer = new Timer(function() {
autoscroll(down);
}, 20);
}
function stopscroll() {
scrollTimer.pause();
}
函数滚动(布尔向下)导致单个delta
增量向上或向下滚动到iframe contentframe
。计时器用于重复该操作,以下是您将如何使用它:
<a href=#>
<img src="scroller/arrowTop.png" title="Scroll Up"
onMouseOver="autoscroll(false);" onMouseOut="stopscroll();"/>
</a>
希望这有帮助。
答案 1 :(得分:0)
你几乎就在那里 - 标准JS中没有window.event
对象。所以使用DOM Event Interface:
function moveit(e) {
"use strict";
document.getElementById("position").style.left = e.clientX + "px";
}
window.onload = function (e) {
"use strict";
findtime();
document.getElementById("scrollbar").style.width = document.getElementById("thevideo").offsetWidth + "px";
var mousemove;
document.getElementById("scrollbar").onclick = function () {
mousemove = window.setInterval(moveit, 1000);
};
document.getElementById("scrollbar").mouseup = function () {
window.clearInterval(mousemove);
};
};