2012-04-20 3 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 

을 :

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를 대입하면 (조건이 true이면 A를 반환하고 그렇지 않으면 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

이 코드는 여전히 같은 오류 = /를 반환합니다. –