英文:
JS - Display div every 5 seconds with fade effect and close button
问题
我现在已经在许多其他论坛上寻找解决方案,但我找不到合适的,也许这里有人可以帮助我
我已经在我的网站左下角编写了一个通知弹出窗口,如下所示:
我现在想每隔5秒使用fadeIn()
和fadeOut()
效果显示我的div
类(通知弹出窗口),并显示5秒钟,然后重复相同的操作,不停止。
我的JS代码:
var div = document.getElementById("show-div");
var myIntv = setInterval(function() {
div.style.display = 'block';
}, 5 * 1000);
但它只在5秒后显示我的div
,然后不重复。有没有人有任何想法,我该如何做到这一点?
我的第二个问题
我的弹出窗口中还有一个小的关闭span
类。
如果第一个代码起作用,我如何阻止通知弹出窗口每秒都出现,并在用户当前会话中,如果他点击关闭按钮,则使div
类display:none;
?
我尝试过以下方法:
$('#close-div').click(function() {
clearInterval(myIntv);
});
但不起作用。
是使用JS还是必须使用PHP会话
?
英文:
I have now looked for a solution on many other forums, but I can't find the right one, maybe someone here can help me
I have coded an notification popup on the left bottom of my site like:
I want now display my div
class (notification popup) every 5 seconds with fadeIn()
and fadeOut()
effect and show it for 5 seconds, then the same again without stopping.
My Js Code:
var div = document.getElementById("show-div");
var myIntv = setInterval(function() {
div.style.display = 'block';
}, 5 * 1000);
But it display my div
just after 5 seconds and dont repeat it. Any one an Idea how can i do this?
My Second Question
I have also an little close span
class in my popup window.
How can I stop the notification popup from appearing every second (If the first code works) and make the div
class display:none;
for the users current Session, if he clicked the close button?
I have tried it with:
$('#close-div').click(function() {
clearInterval(myIntv);
});
But dont work.
Works with JS or must i use PHP Session
?
答案1
得分: 0
First Question:
var div = document.getElementById("show-div");
var showFlag = true;
var myIntv = setInterval(function() {
if(showFlag){
div.style.display = 'block';
showFlag = false;
}
else{
div.style.display = 'none';
showFlag = true;
}
}, 5 * 1000);
Second Question:
$('#close-div').click(function() {
clearInterval(myIntv);
});
英文:
First Question :
var div = document.getElementById("show-div");
var showFlag = true;
var myIntv = setInterval(function() {
if(showFlag){
div.style.display = 'block';
showFlag = false;
}
else{
div.style.display = 'none';
showFlag = true;
}
}, 5 * 1000);
Second Question: Clear Interval should work.
$('#close-div').click(function() {
clearInterval(myIntv);
});
答案2
得分: -1
尝试这个:
var div = document.getElementById("show-div");
var isShown = false;
var myIntv = setInterval(function() {
if(isShown){
div.style.display = 'none';
}
else{
div.style.display = 'block';
}
isShown = !isShown;
}, 5 * 1000);
英文:
Try this:
var div = document.getElementById("show-div");
var isShown = false;
var myIntv = setInterval(function() {
if(isShown){
div.style.display = 'none';
}
else{
div.style.display = 'block';
}
isShown = !isShown;
}, 5 * 1000);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论