首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当尝试淡出元素时,setTimeout不工作

当尝试淡出元素时,setTimeout不工作
EN

Stack Overflow用户
提问于 2019-04-25 17:59:26
回答 1查看 253关注 0票数 1

我正在调用一个tooltip,并试图在9秒之后隐藏它,但是它对我不起作用。

这里发生的事情是,“工具提示”只是在加载之后才被隐藏,但是当我第二次单击"Touch Me“按钮时,它并没有响应。

“触摸我”按钮是#pop-regalo

我的剧本做错了什么?

您可以在下面找到一个演示:

代码语言:javascript
复制
$(function() {
  $('#pop-regalo').hide();
  setTimeout(function() {
    $('#pop-regalo').fadeIn('slow');
  }, 1000);
});

//--------------------

$(document).ready(function() {
  $("#pop-regalo").click(function() {
    $("#hola,.layer-2,.tooltip").show();
  });
  $("#hola").click(function() {
    $("#hola,.layer-2").hide();
  });
});

// --------------------

$(function() {
  $('.tooltip');
  setTimeout(function() {
    $('.tooltip').fadeOut('slow');
  }, 9000);
});
代码语言:javascript
复制
html,
body {
  left: 0;
  top: 0;
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif
}

.note {
  width: 50%;
  margin: 70px auto 0
}

#pop-regalo {
  position: fixed;
  left: 15px;
  top: 15px;
  padding: 10px 15px;
  color: white;
  background: red;
  cursor: pointer
}

#hola {
  display: none;
  position: absolute;
  width: 200px;
  height: 200px;
  padding: 15px 15px 8px;
  text-align: center;
  font-size: 2em;
  line-height: 200px;
  background: #999;
  -webkit-animation: fadein 1s;
  animation: fadein .75s;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  text-align: center;
  border-radius: 5px;
  -webkit-box-shadow: 0 3px 8px rgba(0, 0, 0, 0.7);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.7);
  z-index: 9999
}

@keyframes fadein {
  0% {
    opacity: 0
  }
  50% {
    opacity: 0
  }
  75% {
    opacity: 0
  }
  100% {
    opacity: 1
  }
}

@-webkit-keyframes fadein {
  0% {
    opacity: 0
  }
  50% {
    opacity: 0
  }
  75% {
    opacity: 0
  }
  100% {
    opacity: 1
  }
}

.layer-2 {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  -webkit-animation: fadein .2s;
  animation: fadein .2s;
  z-index: 999
}

.tooltip {
  display: none;
  position: fixed;
  top: 140px;
  left: 100%;
  margin-left: -80px
}

.tooltip .tooltiptext {
  width: 120px;
  background: #000;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 8px 12px;
  position: absolute;
  z-index: 1;
  top: -5px;
  right: 110%;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 30%;
  left: 100%;
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent transparent #000
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="pop-regalo">Touch Me
  <!--<img src="https://i.pinimg.com/originals/3b/4f/ff/3b4fff9dfb958a180863c56d7fcf6326.jpg">-->
</div>

<div class="note">What is happening here that the "tooltip" is only hidden after loading, but it does not respond the second time we click on the red button named Touch Me...?</div>

<div id="hola">Close</div>

<div class="tooltip">
  <span class="tooltiptext">A gift for you</span>
</div>

<div class="layer-2"></div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 18:07:02

您必须在显示工具提示的时刻触发超时。在原始代码中,您只是在页面加载时触发超时,因此,当您在9秒之后第一次单击红色按钮时,工具提示即使在第一次迭代时也不会隐藏。

您还可能希望在触发新实例之前保存对超时值实例的引用,并取消此实例,以便在工具提示仍在显示时关闭覆盖并再次单击按钮时不会过早隐藏工具提示。

代码语言:javascript
复制
var tooltipTimeout;

$(function(){
    $('#pop-regalo').hide();
    setTimeout(function(){
        $('#pop-regalo').fadeIn('slow');
    },1000);
});

//--------------------

$(document).ready(function(){
  $("#pop-regalo").click(function(){
    $("#hola,.layer-2,.tooltip").show();
    // clear any old timeout
    window.clearTimeout(tooltipTimeout);
    // you have to start the timeout after showing the tooltip
    tooltipTimeout = setTimeout(function(){
        $('.tooltip').fadeOut('slow');
    },9000);
  });
    $("#hola").click(function(){
      $("#hola,.layer-2").hide();
  });
});

// --------------------

$(function(){
    // the code in here is only called once at page load
    // $('.tooltip'); // <= this doesn't do anything
    // setTimeout(function(){
    //     $('.tooltip').fadeOut('slow');
    // },9000);
});
代码语言:javascript
复制
html,body {left:0;top:0;margin:0;padding:0;font-family:Arial,sans-serif}

.note {width:50%;margin:70px auto 0}

#pop-regalo {
  position:fixed;
  left:15px;
  top:15px;
  padding:10px 15px;
  color:white;
  background:red;
  cursor:pointer
}

#hola {
    display:none;
    position:absolute;
    width:200px;
    height:200px;
    padding:15px 15px 8px;
    text-align:center;
    font-size:2em;
    line-height:200px;
    background:#999;
    -webkit-animation:fadein 1s;
    animation:fadein .75s;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
    background:#fff;
    text-align:center;
    border-radius:5px;
    -webkit-box-shadow:0 3px 8px rgba(0,0,0,0.7);
    box-shadow:0 3px 8px rgba(0,0,0,0.7);
    z-index:9999
}

@keyframes fadein{0%{opacity:0}50%{opacity:0}75%{opacity:0}100%{opacity:1}}
@-webkit-keyframes fadein{0%{opacity:0}50%{opacity:0}75%{opacity:0}100%{opacity:1}}

.layer-2 {
    display:none;
    position:absolute;
    top:0;
    left:0;
    width:100vw;
    height:100vh;
    background:rgba(0,0,0,0.5);
    -webkit-animation:fadein .2s;
    animation:fadein .2s;
    z-index:999
}

.tooltip {
  display:none;
  position: fixed;
  top:140px;
  left:100%;
  margin-left:-80px
}

.tooltip .tooltiptext {
  width: 120px;
  background:#000;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 8px 12px;
  position: absolute;
  z-index: 1;
  top: -5px;
  right: 110%;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 30%;
  left: 100%;
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent transparent #000
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="pop-regalo">Touch Me
<!--<img src="https://i.pinimg.com/originals/3b/4f/ff/3b4fff9dfb958a180863c56d7fcf6326.jpg">-->
</div>

<div class="note">The tooltip will show upon button click and then hide after 9 seconds.</div>

<div id="hola">Close</div>

<div class="tooltip">
  <span class="tooltiptext">A gift for you</span>
</div>

<div class="layer-2"></div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55855167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档