2012-04-20 9 views
0

私は自分の練習/楽しみのためにやっているプロジェクトのためにJQueryを使わずに水平スライダを書きたいと思っています。私はそれで問題を抱えていると言うためにJavaScript水平スライダ

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); 
    }; 
}; 

言うまでもなく:ここ

は、関連するコードです。私はしかし、それが動作する次のコードを実行します(ただし、マウスの位置を以下のために有用ではありません)場合は常に今:

Uncaught TypeError: Cannot read property 'clientX' of undefined 

など、クロム、Firefoxの上のエラーを生成します。

document.getElementById("position").style.left = 12 + "px"; 

HTMLは次のようになります。

答えて

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(); 
} 

関数スクロールは(ダウンブール値)IFRAME contentframeに単一delta増分上方または下方にスクロールさせます。タイマーは、そのアクションを繰り返すために使用されます。ここではその使用方法を示します。

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

希望します。

+0

注:これは垂直スクロールです。 window.pageYOffset'と 'body.scrollTop'の代わりに' window.pageXOffset'と 'body.scrollLeft'を変更して水平面で動作させるように変更する必要があります。 'contentWindow.scrollTo(newX、0)'を忘れないでください – Ozzy

+0

うん、私はこのコードを理解しているか分からない:document.all? scrollframe.contentWindow.document.body.scrollTop :scrollframe.contentWindow.window.pageYOffset ;.なぜ疑問符となぜ結腸ですか? –

+0

@ user798080これは短期的な条件文で、これは次のように書かれています: 'var curY; if(document.all)curY = scrollframe.contentWindow.document.body.scrollTop; else curY = scrollframe.contentWindow.windiw.pageYOffset; '。 'x =(condition)?a:b;'は 'xを代入する(条件が真なら返り、そうでなければBを返します)' – Ozzy

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); 
    }; 
}; 
+0

このコードはまだ同じエラー= /を返します。 –