1

我在 Magento 中找到了这个脚本,它正在从某个目录旋转图像。这工作正常。但是我需要在“1-6”之后添加一个“暂停”按钮。我不是 javascript 专家,所以不知道该怎么做。有人可以帮我吗?

编码:

<?php
$speed = 5000;//miliseconds
?>


<script type="text/javascript">
var timeoutID;

homeTileCount = 1;
$$('.home-tile-container img').each(function(e){
    $(e).writeAttribute('id','home-tile-' + homeTileCount);
    $(e).addClassName('home-tile');
    homeTileCount++;
});

homeTileCount--;

var homeTileRemote = $$('.home-tile-remote')[0];
for (i=homeTileCount;i>=1;i--)
    homeTileRemote.insert('<div id="home-tile-remote-'+i+'" class="overflow"><a href="#" onclick="switchTile('+i+');return false">'+i+'</a></div>');

function switchTile(n)
{
    //console.log(n);
    clearTimeout(timeoutID);
    $$('.home-tile-container img').each(function(e){
        e.removeClassName('home-tile-active');
    });

    $$('.home-tile-remote > div').each(function(e){
        e.removeClassName('home-tile-remote-active');
    });

    $('home-tile-remote-'+n).addClassName('home-tile-remote-active');
    $('home-tile-'+n).addClassName('home-tile-active');
    next = n+1;
    if (next > homeTileCount)
        next = 1;
    timeoutID = setTimeout('switchTile('+next+')', <?=$speed?>);
}

switchTile(1);
setTimeout('switchTile(2)', <?=$speed?>);
</script>

这就是轮播的样子: 在此处输入图像描述

4

1 回答 1

1

也许是这样:

<?php
$speed = 5000;//miliseconds
?>


<script type="text/javascript">
var paused=false;
var timeoutID;

homeTileCount = 1;
$$('.home-tile-container img').each(function(e){
$(e).writeAttribute('id','home-tile-' + homeTileCount);
$(e).addClassName('home-tile');
homeTileCount++;
});

homeTileCount--;

var homeTileRemote = $$('.home-tile-remote')[0];
for (i=homeTileCount;i>=1;i--)
    homeTileRemote.insert('<div id="home-tile-remote-'+i+'" class="overflow"><a href="#" onclick="switchTile('+i+');return false">'+i+'</a></div>');

function switchTile(n)
{
if (!paused) {
    //console.log(n);
    clearTimeout(timeoutID);
    $$('.home-tile-container img').each(function(e){
        e.removeClassName('home-tile-active');
    });

    $$('.home-tile-remote > div').each(function(e){
        e.removeClassName('home-tile-remote-active');
    });

    $('home-tile-remote-'+n).addClassName('home-tile-remote-active');
    $('home-tile-'+n).addClassName('home-tile-active');
    next = n+1;
    if (next > homeTileCount)
        next = 1;
    timeoutID = setTimeout('switchTile('+next+')', <?=$speed?>);
    }
    }

switchTile(1);
setTimeout('switchTile(2)', <?=$speed?>);
</script>
<button type="button" onclick="paused=true;">Pause</button>
于 2012-10-17T06:22:06.827 回答