gpt4 book ai didi

javascript - jQuery 使用 click 停止函数,该函数在 doc ready 上运行 - 然后在再次单击时启动函数

转载 作者:行者123 更新时间:2023-11-28 11:35:40 25 4
gpt4 key购买 nike

我有一个页面有 2 个功能,这些功能从文档准备就绪开始。一个开始淡入和淡出图像的幻灯片,另一个开始慢慢淡入和淡出背景图像。我实现了一个开关,单击该开关会淡入新的背景图像和整体背景颜色。我正在尝试弄清楚如何让此开关同时停止在文档准备就绪时运行的功能(或完全禁用它们以使幻灯片不会静止不动),但在再次单击时也重新启动它们。所以基本上每次点击都会切换功能。我模拟了一个页面,其中包含图像循环和一个用于更改图像和背景颜色的按钮(我省略了我提到的其他功能,以使其更简单、更简洁)。任何帮助将不胜感激。我已经研究过添加一个全局变量,然后是函数的 if 语句,然后单击将全局变量更改为无效,然后我尝试查看切换函数但没有运气。我对这一切还很陌生,所以如果我的代码困惑或令人困惑,我深表歉意

http://jsfiddle.net/timtim123/d6xn8/2/

<body>
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/background_zps3f866162.png" id="backimg" />
<div id="switch">
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/darkswitch_zpsc7190818.png" width="46" height="275" border="0" />
</div>
<div class="fadein">
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/slide1_zps169c4a26.png" width="394" height="630" border="0" />
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/slide2_zps72fbcc61.png" width="394" height="630" border="0" />
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/slide3_zpsaf2fb393.png" width="394" height="630" border="0" />
<img src="http://i1331.photobucket.com/albums/w600/timtim123454/slide4_zps9544ea88.png" width="394" height="630" border="0" />
</div>
</body>

body {
background:black;
transition:background 0.2s ease;
}
.clicked {
background:white;
}
#backimg {
position:absolute;
top: 0px;
left: 0px;
z-index: 2;
}
#backimg2 {
position:absolute;
top: 0px;
left: 0px;
z-index: 2;
}
#switch {
top: 0px;
left: 100px;
top: 300px;
height:275px;
position:absolute;
z-index: 7;
}
.fadein {
position:absolute;
width:500px;
height:630px;
top: 0px;
left: 500px;
}
.fadein img {
position:absolute;
top: 0px;
left: 500px;
}


//cycle through slides
$(document).ready(function cycle() {
timer = $('.fadein img:gt(0)').hide();
setInterval(function () {
$('.fadein :first-child').fadeOut(2000)
.next('img').fadeIn(2000)
.end().appendTo('.fadein');
}, 2000);

//switch functionality
$(document).ready(function () {
$("#switch").click(function () {
var src = $("#backimg").attr("src");
$("body").delay(2000).queue(function () {
$("body").toggleClass("clicked");
$("body").dequeue();
});


if (src == "http://i1331.photobucket.com/albums/w600/timtim123454/background_zps3f866162.png") {
$("#backimg").fadeOut(2000, (function () {

$("#backimg").fadeIn(2000).attr("src", "http://i1331.photobucket.com/albums/w600/timtim123454/background2_zps36c1126d.png");
}));


} else if (src == "http://i1331.photobucket.com/albums/w600/timtim123454/background2_zps36c1126d.png") {
$("#backimg").fadeOut(500, (function () {
$("#backimg").delay(5000).fadeIn(5000).attr("src", "http://i1331.photobucket.com/albums/w600/timtim123454/background_zps3f866162.png");
}));
}
});
});
});

最佳答案

1) 尝试使用 clearInterval 执行此操作,如本页顶部所示的示例 http://www.w3schools.com/jsref/met_win_clearinterval.asp .这将停止 setInterval 函数。

2) 要隐藏幻灯片/图像,您需要使用 jQuery hide。停止函数不会更改 DOM 中的元素。

3) 这是我发送给您的链接中代码的详细说明,其中包括停止和启动功能。

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<button onclick="myStopFunction()">Stop time</button>
<button onclick="myStartFunction()">Stop time</button>

<script>
var myVar = setInterval(function(){myTimer()},1000);

function myTimer()
{
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}

function myStopFunction()
{
clearInterval(myVar);
}

function myStartFunction()
{
myVar = setInterval(function(){myTimer()},1000);
}
</script>

</body>
</html>

如果您需要更多信息,请告诉我。

仅供引用 - 你不需要 2 '$(document).ready' 只需将第二个代码放在第一个代码下面即可。

关于javascript - jQuery 使用 click 停止函数,该函数在 doc ready 上运行 - 然后在再次单击时启动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20938736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com