0

我正在尝试为我正在为自己的练习/享受而做的项目编写一个没有 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"); ?>
4

2 回答 2

0

你快到了 -window.event标准 JS 中没有对象。所以使用DOM 事件接口

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);
    };
};
于 2012-04-20T21:01:10.407 回答
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函数 scroll(boolean down) 导致向上或向下滚动到 iframe的单个增量contentframe。计时器用于重复该操作,以下是您将如何使用它:

<a href=#>
<img src="scroller/arrowTop.png" title="Scroll Up"
    onMouseOver="autoscroll(false);" onMouseOut="stopscroll();"/>
</a>

希望这可以帮助。

于 2012-04-20T20:58:26.223 回答