JS – 每5秒显示一个带淡入效果和关闭按钮的div。

huangapple go评论54阅读模式
英文:

JS - Display div every 5 seconds with fade effect and close button

问题

我现在已经在许多其他论坛上寻找解决方案,但我找不到合适的,也许这里有人可以帮助我 JS – 每5秒显示一个带淡入效果和关闭按钮的div。

我已经在我的网站左下角编写了一个通知弹出窗口,如下所示:

JS – 每5秒显示一个带淡入效果和关闭按钮的div。

我现在想每隔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类。

如果第一个代码起作用,我如何阻止通知弹出窗口每秒都出现,并在用户当前会话中,如果他点击关闭按钮,则使divdisplay: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 JS – 每5秒显示一个带淡入效果和关闭按钮的div。

I have coded an notification popup on the left bottom of my site like:

JS – 每5秒显示一个带淡入效果和关闭按钮的div。

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);

huangapple
  • 本文由 发表于 2020年1月6日 18:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定