1
<script language="javascript" >
  var speed=25; //speed
  var num=0;
  var photos = document.getElementById('head_image');
  function scrollBG() {
    num++;
    photos.style.backgroundPosition="0"+num;
  }
  setInterval('scrollBG()',speed);
</script>

这是有问题的网站:www.theorymarine.com

4

1 回答 1

2

照片.style.backgroundPosition="0"+num;

您需要一个 CSS 长度单位。

photos.style.backgroundPosition= num+'px 0';

您可能还希望将动画基于时间,以便它移动的速率不依赖于“速度”或浏览器性能。例如。:

<script type="text/javascript">
    var photos= document.getElementById('head_image');
    var begin= new Date().getTime();
    setInterval(function() {
        var x= Math.floor((new Date().getTime()-begin)/25);
        photos.style.backgroundPosition= x+'px 0';
    }, 25);
</script>
于 2009-09-23T20:54:00.490 回答