Hide item with delay

In this case, the element is hidden in units. I canโ€™t understand why it doesnโ€™t show the โ€œPโ€ tag at first and then slowly hides it. Please help me solve the problem.

var step = 0.1;
var delay = 90000;
var displayMe = function() {
  if (element.style.opacity < 1) {
    element.style.opacity += step;
    setTimeout('displayMe()', delay);
  }
}

var hideMe = function() {
  var elem = document.getElementById('regform');
  if (elem.style.opacity >= 0) {
    elem.style.opacity -= step;
    setTimeout('hideMe ()', delay);
  }
}

hideMe();
<p id="regform">aaaaaaaaaaaaaaaaa</p>
Run code
+4
source share
3 answers

Element.style.propwill only read inline styles. Define style='opacity:1'for an item <p>.

var step = 0.1;
var delay = 1000;

var displayMe = function() {
  if (element.style.opacity < 1) {
    element.style.opacity += step;
    setTimeout(displayMe, delay);
  }
}

var hideMe = function() {
  var elem = document.getElementById('regform');
  if (elem.style.opacity >= 0) {
    elem.style.opacity -= step;
    setTimeout(hideMe, delay);
  }
}

hideMe();
<p id="regform" style='opacity:1'>aaaaaaaaaaaaaaaaa</p>
Run code
+2
source

Try

 document.getElementById('regform').style.opacity=1;
 var hideMe = function()
 {
    var elem = document.getElementById('regform');
    if(elem.style.opacity>0)
    {
        elem.style.opacity-= step;
        setTimeout(hideMe, delay);
    }
 }
 hideMe();

Fiddle

0
source

Try it with jquery :

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"> 
      $(document).ready( function() {
        $('#regform').delay(1000).fadeOut(2000);
      });
</script>

<p id="regform" style='opacity:1'>aaaaaaaaaaaaaaaaa</p>
Run code

Documentation .fadeOut ()

0
source

All Articles