我試圖在我的網站上移動圖像。當你點擊圖片時,它應該移動到左邊。現在,這不會發生,有什麼想法?如果可能的話,在純Javascript和沒有Jquery;)onClick事件在圖像上移動時已經移動了
此外,如果您再次單擊該圖像,它應該再次移動到右側。每連續一次,圖像應該移動到另一邊。我想這對於循環來說是最好的?
<script type"text/javascript">
window.onload = init;
var winWidth = window.innerWidth - movingblockobject.scrollWidth; //get width of window
var movingblock = null //object
movingblock.onclick = moveBlockLeft();
function init() {
movingblock = document.getElementById('movingblockobject'); //get object
movingblock.style.left = '0px'; //initial position
moveBlockRight(); //start animiation
}
function moveBlockRight() {
if (parseInt(movingblock.style.left) < winWidth) { // stop function if element touches max window width
movingblock.style.left = parseInt(movingblock.style.left) + 10 + 'px'; //move right by 10px
setTimeout(moveBlockRight,20); //call moveBlockRight in 20 msec
} else {
return;
}
}
function moveBlockLeft() {
movingblock.style.right = parseInt(movingblock.style.right) + 10 + 'px'
}
</script>
非常感謝,我正在尋找的東西! 我現在在大學只有1周的JS,這可能就是爲什麼我有一些基本的錯誤。盡我所能,謝謝你的幫助:) – Loek 2014-09-24 14:02:43