如何停止图像的幻灯片放映
How do I stop slideshow of Images?
function slideshow(){
var timer = 0;
if(++index > 6) index = 1;
$(".banner").attr("src", images[index-1]);
$(".textfield").text(captions[index-1]);
$(".num").text(index);
timer = setTimeout(slideshow,3000);
}
function stopShow(){
clearTimeout(timer);
}
$("#show").click(function(){
slideshow();
});
$("#stopshow").click(function(){
stopShow();
});
我有两个按钮分别用于开始和停止幻灯片。在上面的代码部分中,当我单击停止幻灯片按钮时,它会对 stopShow() 函数进行类化,但 setTimeout 循环不会在后台停止
我认为这与var timer
的范围有关。尝试将声明移到 slideshow()
函数之外。即:
var timer;
function slideshow(){
timer = 0;
....
}
function stopShow(){
clearTimeout(timer);
}
....
也许这会有所帮助?
var doc, IE, E, C, LameSlideshow; // outside onload scope for use on other pages, onload of course
var pre = onload; // if using this way change var pre for other onloads or too much recursion occurs
onload = function(){
if(pre)pre();
doc = document; IE = parseFloat(navigator.appVersion.split('MSIE')[1]);
E = function(id){
return doc.getElementById(id);
}
C = function(e){
return doc.createElement(e);
}
LameSlideshow = function(div, slidesArray, duration, fadeTime){
this.div = div; this.slidesArray = slidesArray; this.frame = 0; this.running = this.fade = false;
this.duration = duration || 1000;
this.fadeTime = fadeTime || 1000;
this.div.style.backgroundImage = "url('"+slidesArray[1]+"')"; this.div.style.backgroundRepeat = 'no-repeat'; this.imgElement = C('img');
this.imgElement.alt = this.imgElement.src = slidesArray[0]; this.div.appendChild(this.imgElement);
this.start = function(){
var x = this;
function loop(){
var t, l = x.slidesArray.length, o = 1, b;
if(++x.frame === l)x.frame = 0;
b = x.frame+1;
if(b === l){
b = 0;
}
x.div.style.backgroundImage = "url('"+x.slidesArray[b]+"')";
;
if(IE && IE < 9){
x.imgElement.style.filter = 'alpha(opacity=1)';
}
else{
x.imgElement.style.opacity = 1;
}
x.imgElement.alt = x.imgElement.src = x.slidesArray[x.frame];
if(x.fadeTime !== false){
clearInterval(x.running);
x.fade = setInterval(function(){
o = +((o-0.01).toFixed(2));
if(o >= 0){
if(IE && IE < 9){
x.imgElement.style.filter = 'alpha(opacity='+(o*100)+')';
}
else{
x.imgElement.style.opacity = o;
}
}
else{
clearInterval(x.fade); x.fadeIn = x.duration; x.running = setInterval(loop, x.duration);
}
}, (x.fadeTime/100));
}
}
this.running = setInterval(loop, this.duration);
}
this.stop = function(){
clearInterval(this.running); clearInterval(this.fade);
if(this.fadeTime !== false){
if(IE && IE < 9){
this.imgElement.style.filter = 'alpha(opacity=1)';
}
else{
this.imgElement.style.opacity = 1;
}
}
}
}
var slideshow = new LameSlideshow(E('outputDiv'), ['one.png', 'two.jpg', 'three.gif'], 2000, 1500);
var run = 0;
E('startStopToggleId').onclick = function(){
if(run === 0){
slideshow.start(); run = 1;
}
else{
slideshow.stop(); run = 0;
}
}
}
相关文章:
- 通过幻灯片更改事件停止使用jquery的音频
- jQuery图像幻灯片,在停止后淡出到第一张图像
- jm按下停止步骤进入滚动的下一张幻灯片
- 当我放入一个库时,幻灯片就停止工作了
- 如何在最后一张幻灯片上停止动画
- 结束时停止推特引导旋转木马's幻灯片
- 需要帮助停止使用jQuery的幻灯片放映
- 我在一页上有两个简单的javascript幻灯片,但第二个停止了第一个工作
- 如何停止幻灯片放映隐藏文本和图像跳到顶部,而下一张幻灯片进来
- 如何启动/停止响应式幻灯片.js
- jQuery幻灯片停止按钮
- 如何让狭缝滑块在幻灯片完成后停止
- 专门用于文本的幻灯片/不断播放/但带有“后退”,“停止”和“前进”按钮的图像文件
- 停止和启动带有 onclick 事件的幻灯片放映
- 如何在jQuery中停止循环幻灯片
- 如何停止图像的幻灯片放映
- jQuery幻灯片和停止问题:动画元素随着鼠标的快速移动而冻结
- 如何在最后一张幻灯片后停止动画
- 自动幻灯片不停止时,我使用javascript事件
- 鼠标悬停时的简单幻灯片显示,鼠标离开时幻灯片显示停止