2

I have a group of divs that share the same class (optionsclass). The display is set to block. When a user clicks them the following javascript function is executed the display is changed to none.

function hideBlockElementsByClass(className)
{
    var elements = document.getElementsByClassName(className);
    for(i in elements)
    {
        elements[i].style.display = "none";
    }
}

The transition between display block and none is quite rough and I would like to make a smoother transition. What's the best strategy to accomplish this?

8
  • 3
    You could try transitioning the opacity property rather than display (and obviously change the JS accordingly too). Commented Oct 31, 2013 at 12:10
  • Have you consider jQuery? Commented Oct 31, 2013 at 12:11
  • $('#elements[i].id').fadeOut('slow'); Commented Oct 31, 2013 at 12:13
  • 1
    People are forgetting how Javascript works, with all these simple Jquery solutions. In my opinion learn Javascript first before JQuery, so let's give him a javascript answer he asks ofr ;) Commented Oct 31, 2013 at 12:14
  • 1
    I need pure javascript/css solution for now. With jQuery is just $(".optionsclass").fadeOut(1000); Commented Oct 31, 2013 at 12:15

5 Answers 5

6

Use CSS3 :

.className { 
 opacity: 0; 
 visibility: hidden;
 -webkit-transition: visibility 0.2s linear,
 opacity 0.2s linear;
 -moz-transition: visibility 0.2s linear,
 opacity 0.2s linear;
 -o-transition: visibility 0.2s linear,
 opacity 0.2s linear; 
}

.className:hover { 
visibility: visible; 
opacity: 1; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Simple yet effective! Thanks. Just needed to replace elements[i].style.display = "none"; to elements[i].className += " className"; and add the class to the css and works fine.
1

While Sridhar gives a nice CSS3 solution and other mention Jquery.

Here you can find a pure javascript/CSS solution:

https://codereview.stackexchange.com/questions/7315/fade-in-and-fade-out-in-pure-javascript

1 Comment

For pure javascript the link you gave its great.Thanks
0

Try this method

HTML

<div class = "image"  onclick = "eff()"></div>

CSS

    .transition {
        -moz-transition: 2s width;
        width: 150px;
        height: 100px;

    }

Script

function eff() {
    var k = document.getElementsByClassName("image"); 
    k[0].className = "transition";
}

Comments

0

try this in plain javascript:(Will work on IE10, chrome, firefox, safari, android, ios)

<script>
       function hideBlockElementsByClass(className) {
            var elements = document.getElementsByClassName(className);
            console.log(elements.length)
            for (var i = 0; i < elements.length; i++) {
                (function (a) {
                    elements[a].addEventListener('webkitTransitionEnd', function () {
                        elements[a].style.display = "none";
                    }, false);
                    elements[a].addEventListener('mozTransitionEnd', function () {
                        elements[a].style.display = "none";
                    }, false);
                    elements[a].addEventListener('oTransitionEnd', function () {
                        elements[a].style.display = "none";
                    }, false);
                    elements[a].addEventListener('transitionend', function () {
                        elements[a].style.display = "none";
                    }, false);
                })(i);
                elements[i].style.webkitTransitionDuration = "1s";
                elements[i].style.mozTransitionDuration = "1s";
                elements[i].style.oTransitionDuration = "1s";
                elements[i].style.transitionDuration = "1s";
                elements[i].style.opacity = "0";
            }
        }
    </script>

Comments

0

You can combine CSS transitioning with your Javascript events:

const block=document.querySelector('.block');

block.addEventListener('click',function(){
this.classList.add('hidden');
})
.container{
  position:retlative
}
.block{
  transition: visibility 0.2s linear,opacity 0.3s linear;
  cursor:pointer;
}
.hidden{
  position: absolute;
  top:0;
  left: 0;
  opacity: 0;
  visibility:hidden;
  }
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
      <div class='container'>
        <div class='block' title='click me'>
        click me
        </div>
      </div>
   </body> 
</html>  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.